IOS处理音乐播放中断实例

作者:朱克锋

邮箱:[email protected]

转载请注明出处:http://blog.csdn.net/linux_zkf

此实例在打开关闭应用时候引发中断


- (BOOL) prepAudio

{

NSError *error;

NSString *path = [[NSBundle mainBundle] pathForResource:@"test" ofType:@"mp3"];

if (![[NSFileManager defaultManager] fileExistsAtPath:path]) return NO;

self.player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];

self.player.delegate = self;

if (!self.player)

{

NSLog(@"Error: %@", [error localizedDescription]);

return NO;

}

[self.player prepareToPlay];


return YES;

}

//音乐播放完毕调用

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag

{

[self.player play];

}

//中断到来调用

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player

{

[[NSUserDefaults standardUserDefaults] setFloat:[self.player currentTime] forKey:@"Interruption"];

}

//中断结束调用

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player

{

[self.player play];

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Interruption"];

}

- (void) viewDidLoad

{

[self prepAudio];

//开始播放时查看是否有中断信息

if ([[NSUserDefaults standardUserDefaults] objectForKey:@"Interruption"])

{

self.player.currentTime = [[NSUserDefaults standardUserDefaults] floatForKey:@"Interruption"];

[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"Interruption"];

}

[self.player play];

}


你可能感兴趣的:(ios,音乐,Path)