iOS后台播放网络音乐
2015-07-30 22:09
633 查看
第一步:
第二步:
in the didFinishLaunchingWithOptions.
[b]
[b]UIDevice *thisDevice = [UIDevice
currentDevice[/b]];[/b]
[b]
[b]if([thisDevice
respondsToSelector:@selector[/b](isMultitaskingSupported)][/b]
[b] && thisDevice.[b]multitaskingSupported[/b])
{[/b]
[b]
[b]UIBackgroundTaskIdentifier backgroundTask = [application
beginBackgroundTaskWithExpirationHandler[/b]:^{[/b]
[b]
[b]/* just fail if this happens. */[/b][/b]
[b]
[b]NSLog(@"BackgroundTask
Expiration Handler is called"[/b]);[/b]
[b] [application
[b]endBackgroundTask[/b]:backgroundTask];[/b]
}];
}
第三步:
在播放的时候添加以下代码:
AVAudioSession *session = [AVAudioSessionsharedInstance];
[session setActive:YESerror:nil];
[session
setCategory[b]:AVAudioSessionCategoryPlaybackerror:nil];[/b]
只需要三步就能在后台播放网络音频文件。
如果需要设置锁屏播放信息,添加代码:
NSMutableDictionary *dict = [[NSMutableDictionary
alloc]
init];
dict[MPMediaItemPropertyTitle] =
self.audioTrackEntity.title;//歌曲名设置
dict[MPMediaItemPropertyArtist] =
@"";//歌手名设置
dict[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork
alloc] initWithImage:nil];//专辑图片设置
[[MPNowPlayingInfoCenter
defaultCenter]
setNowPlayingInfo:dict];
这个时候屏幕上会出现正在播放的音乐信息,如果需要监听“上一首”,“下一首”,“暂停”,“播放”
in the didFinishLaunchingWithOptions. 添加代码:
//告诉系统,我们要接受远程控制事件
[[UIApplication
sharedApplication]
beginReceivingRemoteControlEvents];
在AppDelegate.m 添加函数
//响应远程音乐播放控制消息
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
if (receivedEvent.type
== UIEventTypeRemoteControl) {
switch (receivedEvent.subtype)
{
case
UIEventSubtypeRemoteControlPause:
NSLog(@"RemoteControlEvents: pause");
break;
case
UIEventSubtypeRemoteControlPlay:
NSLog(@"RemoteControlEvents:
play");
break;
case
UIEventSubtypeRemoteControlNextTrack:
NSLog(@"RemoteControlEvents: playNext");
break;
case
UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"RemoteControlEvents:
playPrev");
break;
}
}
}
第二步:
in the didFinishLaunchingWithOptions.
[b]
[b]UIDevice *thisDevice = [UIDevice
currentDevice[/b]];[/b]
[b]
[b]if([thisDevice
respondsToSelector:@selector[/b](isMultitaskingSupported)][/b]
[b] && thisDevice.[b]multitaskingSupported[/b])
{[/b]
[b]
[b]UIBackgroundTaskIdentifier backgroundTask = [application
beginBackgroundTaskWithExpirationHandler[/b]:^{[/b]
[b]
[b]/* just fail if this happens. */[/b][/b]
[b]
[b]NSLog(@"BackgroundTask
Expiration Handler is called"[/b]);[/b]
[b] [application
[b]endBackgroundTask[/b]:backgroundTask];[/b]
}];
}
第三步:
在播放的时候添加以下代码:
AVAudioSession *session = [AVAudioSessionsharedInstance];
[session setActive:YESerror:nil];
[session
setCategory[b]:AVAudioSessionCategoryPlaybackerror:nil];[/b]
只需要三步就能在后台播放网络音频文件。
如果需要设置锁屏播放信息,添加代码:
NSMutableDictionary *dict = [[NSMutableDictionary
alloc]
init];
dict[MPMediaItemPropertyTitle] =
self.audioTrackEntity.title;//歌曲名设置
dict[MPMediaItemPropertyArtist] =
@"";//歌手名设置
dict[MPMediaItemPropertyArtwork] = [[MPMediaItemArtwork
alloc] initWithImage:nil];//专辑图片设置
[[MPNowPlayingInfoCenter
defaultCenter]
setNowPlayingInfo:dict];
这个时候屏幕上会出现正在播放的音乐信息,如果需要监听“上一首”,“下一首”,“暂停”,“播放”
in the didFinishLaunchingWithOptions. 添加代码:
//告诉系统,我们要接受远程控制事件
[[UIApplication
sharedApplication]
beginReceivingRemoteControlEvents];
在AppDelegate.m 添加函数
//响应远程音乐播放控制消息
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent
{
if (receivedEvent.type
== UIEventTypeRemoteControl) {
switch (receivedEvent.subtype)
{
case
UIEventSubtypeRemoteControlPause:
NSLog(@"RemoteControlEvents: pause");
break;
case
UIEventSubtypeRemoteControlPlay:
NSLog(@"RemoteControlEvents:
play");
break;
case
UIEventSubtypeRemoteControlNextTrack:
NSLog(@"RemoteControlEvents: playNext");
break;
case
UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"RemoteControlEvents:
playPrev");
break;
}
}
}
相关文章推荐
- 会话cookie中缺少HttpOnly属性 解决
- 阿里笔试题之Android网络优化
- OGG-01031 由于网络中断导致datapump进程ABENDED的恢复方法
- 黑马程序员——网络编程
- 来谈谈网络安全,关于Session冒名顶替和cookie防篡改的问题
- Android网络传输中必用的两个加密算法:MD5 和 RSA
- Unix网络编程-----阅读笔记(1)
- Android5.0以上系统的移动网络开关
- C++基于TCP/IP简单的客户端、服务器通信程序实例
- RTP 与RTCP 解释. 含同步时间戳
- dotNet使用HttpWebRequest模拟浏览器
- 云计算和大数据时代网络技术揭秘(十九)万兆革命 (完)
- 归并排序(转载http://www.cnblogs.com/jillzhang/archive/2007/09/16/894936.html)
- 本学习笔记TCP/IP传输协议
- 云计算和大数据时代网络技术揭秘(十八)刀片服务器的网络
- TCP/IP四层结构
- HTTP学习要点(三)
- Http访问网络(AsyncTask 异步加载和使用HttpURLConnection)
- HDU 4275 Color the Tree 树的Hash 2012年长春网络赛I题
- http get请求获取服务器返回的应答数据