Commit bfdcabe8 cgx

完成舒眠课程和助眠音乐息屏播放功能

1 个父辈 859a366c
......@@ -41,6 +41,10 @@ FOUNDATION_EXTERN NSString * const ExitCoaxSleep;
FOUNDATION_EXTERN NSString * const NeedUpdateAICoach;
// 更新是否开启AI通知
FOUNDATION_EXTERN NSString * const NeedUpdateStartAI;
// 解锁音频成功通知
FOUNDATION_EXTERN NSString * const UnlockAudioSuccess;
// 监听息屏播放页面点击播放、上一首、下一首通知
FOUNDATION_EXTERN NSString * const RemoteCommandCenterDidClick;
// 用户基础信息
FOUNDATION_EXTERN NSString * const UserBasicInfo;
......
......@@ -27,6 +27,8 @@ NSString * const NoiseTimingDidClick = @"NoiseTimingDidClickNoti";
NSString * const ExitCoaxSleep = @"ExitCoaxSleepNoti";
NSString * const NeedUpdateAICoach = @"NeedUpdateAICoachNoti";
NSString * const NeedUpdateStartAI = @"NeedUpdateStartAINoti";
NSString * const UnlockAudioSuccess = @"UnlockAudioSuccessNoti";
NSString * const RemoteCommandCenterDidClick = @"RemoteCommandCenterDidClickNoti";
NSString * const UserBasicInfo = @"UserBasicInfo";
......
......@@ -45,9 +45,12 @@
if (self.isFromPlayer == NO) {
// 给音频列表页面添加通知(从播放页面跳转到音频列表),通知发送时机:音频列表(从播放页面跳转到音频列表)登录成功后
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOriAudioList) name:@"UnlockAudioSuccessNoti" object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshOriAudioList) name:UnlockAudioSuccess object:nil];
}
// 监听息屏页面点击播放、上一首、下一首点击通知,用于更新当前播放列表正在播放的音频UI状态
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateListNoti:) name:RemoteCommandCenterDidClick object:nil];
NSString *eventID = self.courseType == CourseTypeSafe ? Meditation_Click : Relax_Music_Click;
NSString *entranceName = self.courseType == CourseTypeSafe ? @"首页-舒眠课程" : @"首页-助眠音乐";
if (self.entrance == EntranceCourseList) {
......@@ -60,8 +63,10 @@
- (void)dealloc {
if (self.isFromPlayer == NO) {
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"UnlockAudioSuccessNoti" object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UnlockAudioSuccess object:nil];
}
[[NSNotificationCenter defaultCenter] removeObserver:self name:RemoteCommandCenterDidClick object:nil];
}
- (void)getAudiosData {
......@@ -73,6 +78,12 @@
}];
}
- (void)updateListNoti:(NSNotification *)noti {
NSInteger playingIndex = [noti.userInfo[@"playingIndex"] integerValue];
self.playingIndex = playingIndex;
[self.audioListView reloadData];
}
#pragma mark - Actions
- (void)backAction {
[super backAction];
......@@ -122,7 +133,7 @@
[self.unlockBtn setTitle:@"开始第一节" forState:UIControlStateNormal];
[self getAudiosData];
if (self.isFromPlayer) { [[NSNotificationCenter defaultCenter] postNotificationName:@"UnlockAudioSuccessNoti" object:nil]; }
if (self.isFromPlayer) { [[NSNotificationCenter defaultCenter] postNotificationName:UnlockAudioSuccess object:nil]; }
}
#pragma mark - 刷新最开始的音频列表
......
......@@ -10,6 +10,7 @@
#import <FSAudioController.h>
#import "SubAudioModel.h"
#import "CourseDetailController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface MusicPlayerController () <MusicPlayerViewDelegate, CourseDetailControllerDelegate>
@property (nonatomic, strong) MusicPlayerView *playerView;
......@@ -23,7 +24,12 @@
@property (nonatomic, assign) FSAudioStreamState streamState;
@end
@implementation MusicPlayerController
@implementation MusicPlayerController {
// 控制中心信息
NSMutableDictionary *_remoteInfoDictionary;
// 是否进入后台
BOOL _isBackground;
}
- (void)loadView {
self.view = self.playerView;
......@@ -64,6 +70,7 @@
case kFsAudioStreamPlaying:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
DSLog(@"播放ing...");
[weakSelf didClickRemoteCommandCenter];
break;
case kFsAudioStreamFailed:
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
......@@ -92,11 +99,129 @@
TimerProxy *proxy = [TimerProxy proxyWithTarget:self];
self.progressTimer = [CADisplayLink displayLinkWithTarget:proxy selector:@selector(updateProgress)];
[self.progressTimer addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSRunLoopCommonModes];
[self addPlayerObserver];
[self addRemoteControlHandler];
}
- (void)dealloc {
[self.progressTimer invalidate];
self.progressTimer = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents];
MPRemoteCommandCenter *center = [MPRemoteCommandCenter sharedCommandCenter];
[[center playCommand] removeTarget:self];
[[center pauseCommand] removeTarget:self];
[[center nextTrackCommand] removeTarget:self];
[[center previousTrackCommand] removeTarget:self];
[center.changePlaybackPositionCommand removeTarget:self];
}
- (void)addPlayerObserver {
// 将要进入后台
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
// 已经进入前台
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidEnterForeground) name:UIApplicationDidBecomeActiveNotification object:nil];
}
- (void)playerWillResignActive {
_isBackground = YES;
}
- (void)playerDidEnterForeground {
_isBackground = NO;
}
#pragma mark - 息屏播放(通知栏、锁屏界面)
- (void)addPlayingCenterInfo {
_remoteInfoDictionary = [NSMutableDictionary dictionary];
SubAudioModel *audioModel = self.playAudios[self.currentIndex];
_remoteInfoDictionary[MPMediaItemPropertyTitle] = audioModel.audio_name;
_remoteInfoDictionary[MPMediaItemPropertyAlbumTitle] = @"小梦睡眠";
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:audioModel.audio_pic]];
UIImage *artworkImg = [UIImage imageWithData:imageData];
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithBoundsSize:artworkImg.size requestHandler:^UIImage * _Nonnull(CGSize size) {
return artworkImg;
}];
_remoteInfoDictionary[MPMediaItemPropertyArtwork] = artwork;
_remoteInfoDictionary[MPNowPlayingInfoPropertyPlaybackRate] = [NSNumber numberWithFloat:1.0];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = _remoteInfoDictionary;
}
- (void)updatePlayingCenterInfo {
if (!_isBackground) {return;}
_remoteInfoDictionary[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithDouble:self.audioStream.currentTimePlayed.playbackTimeInSeconds];
_remoteInfoDictionary[MPMediaItemPropertyPlaybackDuration] = [NSNumber numberWithDouble:self.audioStream.duration.playbackTimeInSeconds];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = _remoteInfoDictionary;
}
- (void)addRemoteControlHandler {
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
MPRemoteCommandCenter *center = [MPRemoteCommandCenter sharedCommandCenter];
[self addRemoteCommand:center.playCommand selector:@selector(play)];
[self addRemoteCommand:center.pauseCommand selector:@selector(pause)];
[self addRemoteCommand:center.previousTrackCommand selector:@selector(last)];
[self addRemoteCommand:center.nextTrackCommand selector:@selector(next)];
}
- (void)addRemoteCommand:(MPRemoteCommand *)command selector:(SEL)selector {
WS(weaksSelf);
[command addTargetWithHandler:^MPRemoteCommandHandlerStatus(MPRemoteCommandEvent * _Nonnull event) {
if ([weaksSelf respondsToSelector:selector]) {
IMP imp = [weaksSelf methodForSelector:selector];
void (*func)(id, SEL) = (void *)imp;
func(weaksSelf, selector);
}
return MPRemoteCommandHandlerStatusSuccess;
}];
}
- (void)play {
if (self.streamState == kFsAudioStreamStopped || self.streamState == kFsAudioStreamRetrievingURL) {
[self.audioStream play];
} else if (self.streamState == kFsAudioStreamPaused) {
// 恢复播放
[self.audioStream pause];
}
[self didClickRemoteCommandCenter];
}
- (void)pause {
if (self.streamState == kFsAudioStreamPlaying) {
// 暂停播放
[self.audioStream pause];
}
[self didClickRemoteCommandCenter];
}
- (void)last {
if (self.currentIndex - 1 < 0) {
[DSProgressHUD showToast:@"没有上一首了"];
return;
}
self.currentIndex--;
[self launchPlayer];
}
- (void)next {
if (self.currentIndex + 1 >= self.playAudios.count) {
[DSProgressHUD showToast:@"没有下一首了"];
return;
}
self.currentIndex++;
[self launchPlayer];
}
- (void)didClickRemoteCommandCenter {
NSInteger playingIndex = self.audioStream.isPlaying ? self.currentIndex : -1;
[[NSNotificationCenter defaultCenter] postNotificationName:RemoteCommandCenterDidClick object:nil userInfo:@{@"playingIndex":@(playingIndex)}];
}
#pragma mark - private
......@@ -108,6 +233,8 @@
// 更新音频播放进度、音频当前播放时间、音频总时间
[self.playerView updateProgress:cur.position currentTime:[NSString stringWithFormat:@"%02i:%02i", cur.minute, cur.second] totalTime:[NSString stringWithFormat:@"%02i:%02i", end.minute, end.second]];
[self updatePlayingCenterInfo];
}
- (void)audioStreamPlaybackCompleted {
......@@ -137,13 +264,15 @@
}
- (void)launchPlayer {
// 不是正在播放的状态需要先停止
if (self.audioStream.isPlaying == NO) { [self.audioStream stop]; }
[self.audioStream stop];
if (self.currentIndex >= self.playAudios.count) { return; }
SubAudioModel *audioModel = self.playAudios[self.currentIndex];
[self.playerView updatePlayerView:audioModel];
[self.audioStream playFromURL:[NSURL URLWithString:audioModel.audio_url]];
// 设置音频锁屏界面信息
[self addPlayingCenterInfo];
}
#pragma mark - MusicPlayerViewDelegate
......@@ -170,12 +299,7 @@
switch (item.tag) {
case 1: // 上一首
{
if (self.currentIndex - 1 < 0) {
[DSProgressHUD showToast:@"没有上一首了"];
return;
}
self.currentIndex--;
[self launchPlayer];
[self last];
}
break;
case 2: // 播放暂停
......@@ -193,12 +317,7 @@
break;
case 3: // 下一首
{
if (self.currentIndex + 1 >= self.playAudios.count) {
[DSProgressHUD showToast:@"没有下一首了"];
return;
}
self.currentIndex++;
[self launchPlayer];
[self next];
}
break;
default:
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!