Iphone 使用AVAudioPlayer播放后停止音频
大家好,希望你们能帮我解决这个问题 我想做的是当用户离开视图B到视图A时,音频播放,当用户离开视图A到视图B时,音频停止 因此,当用户将视图B(从superview中删除视图B)返回到主视图(即视图A)时,将调用playIT和hahaPlay方法。当用户从视图A转到视图B(视图A添加子视图B)时,将调用stopPlaying方法。但我的问题是,即使调用stopPlaying方法,音频也不会停止。我意识到警报已显示,但出于某种原因,声明>Iphone 使用AVAudioPlayer播放后停止音频,iphone,avaudioplayer,Iphone,Avaudioplayer,大家好,希望你们能帮我解决这个问题 我想做的是当用户离开视图B到视图A时,音频播放,当用户离开视图A到视图B时,音频停止 因此,当用户将视图B(从superview中删除视图B)返回到主视图(即视图A)时,将调用playIT和hahaPlay方法。当用户从视图A转到视图B(视图A添加子视图B)时,将调用stopPlaying方法。但我的问题是,即使调用stopPlaying方法,音频也不会停止。我意识到警报已显示,但出于某种原因,声明>[audioPlayer stop]不起作用 我已经在头文件
[audioPlayer stop]代码>不起作用
我已经在头文件中定义了音频播放器,仅供参考
以下所有方法都位于名为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];
}
可能是您错误地丢失了某个引用,请尝试在停止呼叫附近设置断点,并检查audioPlayer
,以防audioPlayer
为nil
。只是说,因为通常这是我遇到这些问题的原因
下面的工作也可以吗
[audioPlayer pause]
audioPlayer.currentTime = 0;