大纲
这次简单实现了:
在线音乐播放(AVPlayer)、
监听进度(addPeriodicTimeObserverForInterval)、
设置控制中心的音乐信息(MPNowPlayingInfoCenter)、
监听远程控制中心的操作(remoteControlReceivedWithEvent上一曲、下一曲等)
还未涉及到的知识有:
AVAudioSession 等,慢慢完善。
效果图
Screen Shot 2018-01-30 at 下午4.26.10.png IMG_2141.PNG IMG_2142.PNG代码
//
// ViewController.m
// LearnAVPlayer
//
// Created by zhiyunyu on 2018/1/29.
// Copyright © 2018年 zhiyunyu. All rights reserved.
//
#import "ViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <MediaPlayer/MediaPlayer.h>
#define MP3_URL @"http://techslides.com/demos/samples/sample.mp3" //文件来源于 http://techslides.com/sample-files-for-development
@interface ViewController ()
@property(nonatomic, strong) UIButton *startPlayBtn;
@property(nonatomic, strong) UIProgressView *musicProgressView;
@property(nonatomic, strong) AVPlayer *player;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor blackColor];
self.startPlayBtn = ({
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(100, [UIScreen mainScreen].bounds.size.height - 200,[UIScreen mainScreen].bounds.size.width - 200, 48)];
btn.layer.borderWidth = 1;
btn.layer.cornerRadius = 24;
btn.layer.borderColor = [UIColor whiteColor].CGColor;
[btn setTitle:@"播放" forState:UIControlStateNormal];
[btn addTarget:self action:@selector(startPlay:) forControlEvents:UIControlEventTouchUpInside];
btn;
});
[self.view addSubview:self.startPlayBtn];
self.musicProgressView = ({
UIProgressView *progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(24, [UIScreen mainScreen].bounds.size.height - 250,[UIScreen mainScreen].bounds.size.width - 48, 20)];
progressView.backgroundColor = [UIColor whiteColor];
progressView;
});
[self.view addSubview:self.musicProgressView];
[self setMusicInfo];
}
-(void)setMusicInfo {
NSURL * url = [NSURL URLWithString:MP3_URL];
AVPlayerItem * songItem = [[AVPlayerItem alloc]initWithURL:url];
self.player = [[AVPlayer alloc]initWithPlayerItem:songItem];
// @weakify(self); 我目前没有添加对应的库,正式工程中一定要加上@weakify(self);
id observer = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(10.0, 10.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
// @strongify(self);
CGFloat currentTime = CMTimeGetSeconds(time);
self.musicProgressView.progress = currentTime / CMTimeGetSeconds(self.player.currentItem.duration);
[self updateNowPlayingInfoCenter];
}];
//告诉系统,我们要接受远程控制事件(如果这里不设置,即使设置了MPNowPlayingInfoCenter,控制中心也不会显示歌曲信息)
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
[self becomeFirstResponder];
}
-(void)startPlay:(id)sender {
[self.player play];
}
//设置控制中心的歌曲信息
-(void)updateNowPlayingInfoCenter {
NSDictionary *info = @{MPMediaItemPropertyArtist:@"俞志云",
MPMediaItemPropertyAlbumTitle:@"一首歌的名字",
MPMediaItemPropertyPlaybackDuration:@(CMTimeGetSeconds(self.player.currentItem.duration)),
MPNowPlayingInfoPropertyElapsedPlaybackTime:@(CMTimeGetSeconds(self.player.currentTime))
};
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:info];
}
//响应远程音乐播放控制消息
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
switch (receivedEvent.subtype) {
case UIEventSubtypeRemoteControlPlay:
NSLog(@"暂停播放");
break;
case UIEventSubtypeRemoteControlPause:
NSLog(@"继续播放");
break;
case UIEventSubtypeRemoteControlNextTrack:
NSLog(@"下一曲");
break;
case UIEventSubtypeRemoteControlPreviousTrack:
NSLog(@"上一曲");
break;
default:
break;
}
}
}
@end
关于background modes
要支持后台播放得设置background modes;
在控制中心显示歌曲信息不需要设置background modes ,但是歌曲播放完毕信息就不再显示;如果要歌曲信息一直驻留在控制中心,就得设置background modes ;
设置支持音频后台播放:选中项目》capability > background modes 〉勾选第一项audio (这是基本设置,可能还要多加一些操作,等我再确认下)
网友评论