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];
}