2011-08-28 48 views
0

我有10個圖像文件和聲音文件,當我顯示「a.png」時,我想播放「a.mp3」。然後當我點擊「下一步」按鈕時,顯示「b.png」並播放「b.mp3」。我該如何解決這個問題?用顯示的圖像逐個播放聲音

回答

0

要播放聲音,請使用此 在.h文件中輸入以下內容: AVAudioPlayer * theaudio;

這個在你的.m文件中,當你想播放音樂時 NSString * path = [[NSBundle mainBundle] pathForResource:@「nameofmusicfile」ofType:@「mp3」]; theaudio = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:NULL]; [路徑發佈]; [theaudio play]; 停止它,使用[theaudio停止];

顯示圖像使用UIImageView * i;

i = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@「image.png」]];

把它放在 - [view addSubview:i]; 移動它i.center = CGPointMake(0.0,0.0);

+0

感謝您的回答,但我認爲這是一組工作... – ersin

1
-(void) viewDidLoad;{ 


sounds = [[NSArray alloc]initWithObjects:@"a.mp3", @"b.mp3",@"c.mp3", nil]; 


imageArray = [[NSArray arrayWithObjects: 
       [UIImage imageNamed:@"a.jpg"], 
       [UIImage imageNamed:@"b.jpg"], 
       [UIImage imageNamed:@"c.jpg"], 
       nil] retain]; 
[super viewDidLoad];} 
-(IBAction) next; { 
currentSound++; 
if (currentSound >= sounds.count) currentSound = 0; 
//what kind of code play this sound////images work perpectly but sound not// 


currentImage++; 
if (currentImage >= imageArray.count) currentImage = 0; 
UIImage *img = [imageArray objectAtIndex:currentImage]; 
[imageView setImage:img];}