#import "RootViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
@interface RootViewController ()
@end
@implementation RootViewController
{
AVPlayer *player;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
AVAudioSession *sesstion = [AVAudioSession sharedInstance];
//设置音频会话支持后台播放
[sesstion setCategory:AVAudioSessionCategoryPlayback error:nil];
[sesstion setActive:YES error:nil];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"第一夫人" ofType:@"mp3"];
NSURL *url = [NSURL fileURLWithPath:filePath];
player = [[AVPlayer alloc] initWithURL:url];
[player play];
//配置封面
[self configPlayingInfo];
}
- (BOOL)becomeFirstResponder
{
return YES;
}
//配置封面
- (void)configPlayingInfo
{
if (NSClassFromString(@"MPNowPlayingInfoCenter")) {
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
//设置歌曲名
[dict setObject:@"第一夫人" forKey:MPMediaItemPropertyTitle];
//设置歌手
[dict setObject:@"张杰" forKey:MPMediaItemPropertyArtist];
[dict setObject:[[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"mp3.jpg"]] forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:dict];
}
}
//视图已经出现
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
//设置应用程序接受远程控制器事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//当前控制器作为第一响应者
[self becomeFirstResponder];
}
//视图已经消失
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
//停止接受远程控制器事件
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
[self resignFirstResponder];
}
//已经接收到了远程控制事件,重写父类的方法
- (void)remoteControlReceivedWithEvent:(UIEvent *)event
{
if (event.type == UIEventTypeRemoteControl) {
switch (event.subtype) {
case UIEventSubtypeRemoteControlTogglePlayPause:
// 切换播放、暂停按钮
{
NSLog(@"暂停");
[player pause];
}
break;
case UIEventSubtypeRemoteControlPreviousTrack:
// 播放上一曲按钮
{
NSLog(@"上一首");
}
break;
case UIEventSubtypeRemoteControlNextTrack:
// 播放下一曲按钮
{
NSLog(@"下一首");
}
break;
default:
break;
}
}
}
@end
设置通知栏菜单
最新推荐文章于 2021-12-01 15:18:54 发布