Commit fa99d337 cgx

优化息屏播放

1 个父辈 000404e3
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#import "SubAudioModel.h" #import "SubAudioModel.h"
#import "CourseDetailController.h" #import "CourseDetailController.h"
#import <MediaPlayer/MediaPlayer.h> #import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
@interface MusicPlayerController () <MusicPlayerViewDelegate, CourseDetailControllerDelegate> @interface MusicPlayerController () <MusicPlayerViewDelegate, CourseDetailControllerDelegate>
@property (nonatomic, strong) MusicPlayerView *playerView; @property (nonatomic, strong) MusicPlayerView *playerView;
...@@ -110,6 +111,7 @@ ...@@ -110,6 +111,7 @@
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationDidBecomeActiveNotification object:nil];
[[NSNotificationCenter defaultCenter] removeObserver:self name:AVAudioSessionInterruptionNotification object:nil];
[[UIApplication sharedApplication] endReceivingRemoteControlEvents]; [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
MPRemoteCommandCenter *center = [MPRemoteCommandCenter sharedCommandCenter]; MPRemoteCommandCenter *center = [MPRemoteCommandCenter sharedCommandCenter];
...@@ -125,6 +127,11 @@ ...@@ -125,6 +127,11 @@
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillResignActive) name:UIApplicationWillResignActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerWillResignActive) name:UIApplicationWillResignActiveNotification object:nil];
// 已经进入前台 // 已经进入前台
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidEnterForeground) name:UIApplicationDidBecomeActiveNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerDidEnterForeground) name:UIApplicationDidBecomeActiveNotification object:nil];
// 监听播放器被打断(别的软件播放音乐,来电话)
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playerAudioBeInterrupted:)
name:AVAudioSessionInterruptionNotification
object:[AVAudioSession sharedInstance]];
} }
- (void)playerWillResignActive { - (void)playerWillResignActive {
...@@ -135,6 +142,17 @@ ...@@ -135,6 +142,17 @@
_isBackground = NO; _isBackground = NO;
} }
- (void)playerAudioBeInterrupted:(NSNotification *)notification {
NSDictionary *userInfo = notification.userInfo;
if ([userInfo[AVAudioSessionInterruptionTypeKey] integerValue] == 1) { // 打断开始
[self pause];
} else { // 打断结束
if ([userInfo[AVAudioSessionInterruptionOptionKey] unsignedIntegerValue] == 1) {
[self play];
}
}
}
#pragma mark - 息屏播放(通知栏、锁屏界面) #pragma mark - 息屏播放(通知栏、锁屏界面)
- (void)addPlayingCenterInfo { - (void)addPlayingCenterInfo {
_remoteInfoDictionary = [NSMutableDictionary dictionary]; _remoteInfoDictionary = [NSMutableDictionary dictionary];
...@@ -154,7 +172,7 @@ ...@@ -154,7 +172,7 @@
} }
- (void)updatePlayingCenterInfo { - (void)updatePlayingCenterInfo {
if (!_isBackground) {return;} if (!_isBackground) { return; }
_remoteInfoDictionary[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithDouble:self.audioStream.currentTimePlayed.playbackTimeInSeconds]; _remoteInfoDictionary[MPNowPlayingInfoPropertyElapsedPlaybackTime] = [NSNumber numberWithDouble:self.audioStream.currentTimePlayed.playbackTimeInSeconds];
_remoteInfoDictionary[MPMediaItemPropertyPlaybackDuration] = [NSNumber numberWithDouble:self.audioStream.duration.playbackTimeInSeconds]; _remoteInfoDictionary[MPMediaItemPropertyPlaybackDuration] = [NSNumber numberWithDouble:self.audioStream.duration.playbackTimeInSeconds];
[MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = _remoteInfoDictionary; [MPNowPlayingInfoCenter defaultCenter].nowPlayingInfo = _remoteInfoDictionary;
......
支持 Markdown 格式
你添加了 0 到此讨论。请谨慎行事。
Finish editing this message first!