NSURL * url = [NSURL URLWithString:self.currentSong.url]; AVPlayerItem * songItem = [[AVPlayerItem alloc]initWithURL:url]; AVPlayer * player = [[AVPlayer alloc]initWithPlayerItem:songItem];
AVPlayer * player = [[AVPlayer alloc] initWithURL:url];
AVPlayerItem * songItem = player.currentItem;
[player play];
[player pause];
[player replaceCurrentItemWithPlayerItem:songItem];
NSArray * items = @[item1, item2, item3 ....]; AVQueuePlayer * queuePlayer = [[AVQueuePlayer alloc]initWithItems:items];
id timeObserve = [self.player addPeriodicTimeObserverForInterval:CMTimeMake(1.0, 1.0) queue:dispatch_get_main_queue() usingBlock:^(CMTime time) {
float current = CMTimeGetSeconds(time);
float total = CMTimeGetSeconds(songItem.duration);
if (current) {
weakSelf.progress = current / total;
weakSelf.playTime = [NSString stringWithFormat:@"%.f",current];
weakSelf.playDuration = [NSString stringWithFormat:@"%.2f",total]; }
}];
if (timeObserve) {
[player removeTimeObserver:_timeObserve];
timeObserve = nil;
}
[songItem addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
if ([keyPath isEqualToString:@"status"]) {
switch (self.player.status) {
case AVPlayerStatusUnknown:
BASE_INFO_FUN(@"KVO:未知状态,此时不能播放");
break;
case AVPlayerStatusReadyToPlay:
self.status = SUPlayStatusReadyToPlay;
BASE_INFO_FUN(@"KVO:准备完毕,可以播放");
break;
case AVPlayerStatusFailed:
BASE_INFO_FUN(@"KVO:加载失败,网络或者服务器出现问题");
break;
default:
break;
}
}
}
[songItem removeObserver:self forKeyPath:@"status"];
[songItem addObserver:self forKeyPath:@"loadedTimeRanges" options:NSKeyValueObservingOptionNew context:nil];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
AVPlayerItem * songItem = object;
if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
NSArray * array = songItem.loadedTimeRanges;
CMTimeRange timeRange = [array.firstObject CMTimeRangeValue]; //本次缓冲的时间范围
NSTimeInterval totalBuffer = CMTimeGetSeconds(timeRange.start) + CMTimeGetSeconds(timeRange.duration); //缓冲总长度
SuLog(@"共缓冲%.2f",totalBuffer);
}
}
[songItem removeObserver:self forKeyPath:@" loadedTimeRanges"];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:AVPlayerItemDidPlayToEndTimeNotification object:songItem];
- (void)playbackFinished:(NSNotification *)notice {
BASE_INFO_FUN(@"播放完成");
[self playNext];
}
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
- (BOOL)canBecomeFirstResponder {
return YES;
}
- (void)remoteControlReceivedWithEvent:(UIEvent *)event {
switch (event.subtype) {
case UIEventSubtypeRemoteControlPlay:
[self.player startPlay];
BASE_INFO_FUN(@“remote_播放");
break;
case UIEventSubtypeRemoteControlPause:
[self.player pausePlay];
BASE_INFO_FUN(@"remote_暂停");
break;
case UIEventSubtypeRemoteControlNextTrack:
[self.player playNextSong];
BASE_INFO_FUN(@"remote_下一首");
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
self.player.isPlaying ? [self.player pausePlay] : [self.player startPlay];
BASE_INFO_FUN(@“remote_耳机的播放/暂停");
break;
default:
break; }
}
- (void)configNowPlayingCenter { BASE_INFO_FUN(@"配置NowPlayingCenter");
NSMutableDictionary * info = [NSMutableDictionary dictionary];
//音乐的标题
[info setObject:_player.currentSong.title forKey:MPMediaItemPropertyTitle];
//音乐的艺术家
[info setObject:_player.currentSong.artist forKey:MPMediaItemPropertyArtist];
//音乐的播放时间
[info setObject:@(self.player.playTime.intValue) forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
//音乐的播放速度
[info setObject:@(1) forKey:MPNowPlayingInfoPropertyPlaybackRate];
//音乐的总时间
[info setObject:@(self.player.playDuration.intValue) forKey:MPMediaItemPropertyPlaybackDuration];
//音乐的封面
MPMediaItemArtwork * artwork = [[MPMediaItemArtwork alloc] initWithImage:_player.coverImg];
[info setObject:artwork forKey:MPMediaItemPropertyArtwork];
//完成设置
[[MPNowPlayingInfoCenter defaultCenter]setNowPlayingInfo:info];
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有