iOS开发-音乐篇

1.后台播放

这是一段很神奇的代码,除了实现后台播放,还顺带实现了静音播放,GET

myApp-Info.plist中添加UIBackgroundModes键值,添加子键值为audio。

然后在程序中添加入下代码,最好在AppDelegate里面加入:

AVAudioSession*session=[AVAudioSessionsharedInstance];

[sessionsetActive:YESerror:nil];

[sessionsetCategory:AVAudioSessionCategoryPlaybackerror:nil];

————引用作者:Tony  来源:https://itony.me/305.html


2.突发事件,中断播放(打电话)

-->在通知中心注册一个事件中断的通知:

//回调两次,开始和结束,最好写在AppDelegate里面

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleInterreption:) name:AVAudioSessionInterruptionNotification object:[AVAudioSession sharedInstance]];

-->实现接收到中断通知时的方法

//处理中断事件

-(void)handleInterreption:(NSNotification *)sender

{

if(_played)

{

//操作一

}

else

{

//操作二

}

}


你可能感兴趣的:(iOS开发-音乐篇)