0
我正在开发电子书阅读器。有些音频文件需要点击图标才能播放。音频第一次播放的很好。但是当我按下应用程序的主页按钮并返回到音频播放器时,音频将播放两次。每次按Home按钮并返回到音频播放器时,该数字都会增加1。使用调试器我发现这个循环运行多次。任何人都可以解释为什么它的行为如此?音频播放器循环运行多次
-(void)StartPlayingFileWithNotification:(NSNotification *)notifcation{
// Load the array with the sample file
NSString *f = [[notifcation userInfo] valueForKey:@"audiopath"];
NSURL *fileURL = [[NSURL alloc] initFileURLWithPath: f];
if(fileURL == nil){
GLogError(@"%@",@"File Not Found");
return;
}else {
GLogInfo(@"%@",@"Got audio file");
}
if (self.player != nil) {
if (self.player.playing) {
[self.player stop];
}
[player release];
player = nil;
}
self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:fileURL error:nil];
if (self.player)
{
fileName.text = [NSString stringWithFormat: @"%@ (%d ch.)", [[player.url relativePath] lastPathComponent], player.numberOfChannels, nil];
NSLog(@"The audio file is %@",fileName.text);
NSLog(@"The file URL is %@",[fileURL description]);
[self updateViewForPlayerInfo:player];
player.numberOfLoops = 0;
player.delegate = self;
[player play];
[self updateViewForPlayerState:player];
}
[fileURL release];
}