2011-02-11 79 views
1

我正在播放声音,当用户点击尊敬的按钮。如何一次只播放一个声音与AVAudioplayer

我有10个音频文件的按钮。

我正在使用AVAudioPlayer

AVAudioPlayer *myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[sounds objectAtIndex:button.tag] ofType:@"mp3"]]error:NULL]; 
[myaudio play]; 

播放选定的声音现在我有一个问题,如果我打以前的声音那么这两个声音被混合并获得噪声完成前点击另一个按钮。

我需要在完成第一个声音后播放第二个声音。

我的意思是在同一时间只会播放一个声音。

我该怎么办,任何人都可以帮助我。

让我添加评论,如果这个问题是不可理解的。

谢谢你提前。

回答

1

我解决我的问题是这样

if (soundplay == 0) { 
     myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[sounds objectAtIndex:x] ofType:@"mp3"]]error:NULL]; 
     myaudio.delegate = self; 
     soundplay = 1; 
     [myaudio play]; 
    } 

- (void) audioPlayerDidFinishPlaying: (AVAudioPlayer *)myaudio successfully:(BOOL)flag { 
    soundplay = 0; 
    NSLog(@"sound fnished"); 
} 

其中soundplay是整型变量

0

@Mahesh你可以创建一个AVAudioPlayer类变量,并在IBAction为功能,您可以做这样的事情:

if([myaudio playing]) 
    [myaudio stop]; 
myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle]pathForResource:[sounds objectAtIndex:button.tag] ofType:@"mp3"]]error:NULL]; 
[myaudio play]; 
0

不创建AVAudioPlayer的不同的对象。为AVAudioPlayer创建单线对象。并使用此对象只需检查该对象玩家是否在玩。

if([myaudio playing]) 
{ 
    [myaudio stop]; 
    myaudio=[[AVAudioPlayer alloc]initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle  mainBundle]pathForResource:[sounds objectAtIndex:button.tag] ofType:@"mp3"]]error:NULL]; 
    [myaudio play]; 
} 
+0

我收到警告:“AVAudioPlayer”不能为“-playing”和响应应用程序退出 – MaheshBabu 2011-02-11 07:29:41

+0

看样从iOS参考库代码avTouch以供参考。 – 2011-02-11 07:36:24