2011-02-18 108 views
0

大家好,希望大家能够帮助我解决这个问题。玩AVAudioPlayer后停止音频

我想要做的是当用户离开视图B查看A,音频播放,当用户离开视图A查看B时,音频停止。

因此,当用户离开视图B(从超级视图移除viewB)回到视图A的主视图时,方法playIT & haha​​Play将被调用。当用户离开视图A到视图B(视图A添加子视图B)时,将调用stopPlaying方法。但这是我的问题,即使调用stopPlaying方法,音频也不会停止。我意识到警报显示,但由于某种原因,>[audioPlayer stop];声明不起作用。

我已经在头文件中定义了音频播放器,只是fyi。

以下所有方法都在名为soundView的viewcontroller中。

对不起,我可能在做错整个事情,也许你们中的一些人可以启发我,纠正我的错误?

-(void)playIT{ 

    filePath = [[NSBundle mainBundle] pathForResource:@"6431" 
               ofType:@"aiff"]; 
    fileURL = [[NSURL alloc] initFileURLWithPath:filePath]; 
    audioPlayer = [[AVAudioPlayer alloc] 
        initWithContentsOfURL:fileURL error:nil]; 
} 

-(void)hahaPlay{ 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"playing" message:@" playing" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 
    [audioPlayer prepareToPlay]; 
    if(![audioPlayer isPlaying]){ 

     [audioPlayer play]; 
    } 

- (void)stopPlaying{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Stop playing" message:@"Stop playing" delegate:nil cancelButtonTitle:@"Yep, I did." otherButtonTitles:nil]; 
    [alert show]; 
    [alert release]; 

    [audioPlayer stop]; 

} 

回答

0

也许你错的地方失去了参考,可以尝试设置断点停止呼叫附近,正好在该audioPlayernil情况检查audioPlayer。只是说因为通常这是造成这些类型问题的原因......

还会有以下工作吗?

[audioPlayer pause] 
audioPlayer.currentTime = 0;