程式碼介面檔案
#import <Foundation/Foundation.h>
#import <AVFoundation/AVFoundation.h>
NS_ASSUME_NONNULL_BEGIN
// 當前播放器的播放形式
typedef NS_ENUM(NSInteger, MediaPlayType) {
MediaPlayTypeCycle, ///< 順序
MediaPlayTypeSingle, ///< 單曲迴圈
MediaPlayTypeRandom ///< 隨機播放
};
// 當前播放器的播放狀態
typedef NS_ENUM(NSInteger, MediaPlayStatus) {
MediaPlayStatusStop, ///< 停止播放
MediaPlayStatusPause, ///< 暫停播放
MediaPlayStatusPlaying ///< 正在播放
};
// 媒體載入狀態
typedef NS_ENUM(NSInteger, MediaLoadStatus) {
MediaLoadStatusReadyToPlay, ///< 準備播放
MediaLoadStatusUnknown, ///< 未知
MediaPlayStatusFailed ///< 失敗
};
@class MediaPlyerManager;
@protocol MediaPlyerManagerDelegate <NSObject>
@optional
// 資料載入狀態 根據狀態進行播放或其他操作
- (void)MediaPlayer:(MediaPlyerManager *)playerManager playerItemStatus:(MediaLoadStatus)status;
// 緩衝進度
- (void)MediaPlayer:(MediaPlyerManager *)playerManager netBufferValue:(CGFloat)value;
// 緩衝是否足夠播放
- (void)MediaPlayer:(MediaPlyerManager *)playerManager bufferHasEnough:(BOOL)enough;
// 當前播放的時間
- (void)MediaPlayer:(MediaPlyerManager *)playerManager currentPlayTime:(NSString *)time currentPlayTimeValue:(CGFloat)value;
// 播放總時間
- (void)MediaPlayer:(MediaPlyerManager *)playerManager mediaEndTime:(NSString *)time mediaEndTimeValue:(CGFloat)value;
// 播放結束
- (void)MediaPlayerCurrentMediaPlayFinish:(MediaPlyerManager *)playerManager;
// 播放狀態
- (void)MediaPlayer:(MediaPlyerManager *)playerManager playeStatus:(MediaPlayStatus)status;
// 獲取資料切換時獲取正在播放的URL和當前的index
- (void)MediaPlayer:(MediaPlyerManager *)playerManager currentUrl:(NSString *)url currentIndex:(NSInteger)index;
// 為了配合手機後臺播放 實時獲取播放的進度,總的時間,當前的index<通過index獲取圖片等資訊>
- (void)MediaPlayer:(MediaPlyerManager *)playerManager currentProgressValue:(CGFloat)value totalValue:(CGFloat)totalValue currentIndex:(NSInteger)index;
@end
typedef MediaPlyerManager *(^playerCurrentTime)(NSString *time);
@interface MediaPlyerManager : NSObject
@property (nonatomic, strong, readonly) AVPlayer *mediaPlayer; ///< 播放器
@property (nonatomic, strong, readonly) AVPlayerItem *meidaPlayerItem; ///< 播放器的CurrentItem
@property (nonatomic, strong, readonly) NSMutableArray<NSString*> *dataUrlArray; ///< 正在播放的列表資料
@property (nonatomic, assign, readonly) MediaPlayType playType; ///< 當前播放型別
@property (nonatomic, assign, readonly) MediaPlayStatus playStatus; ///< 當前播放狀態
@property (nonatomic, assign, readonly) NSInteger currentIndex; ///< 當前播放的索引
@property (nonatomic, assign, readonly) BOOL isPlaying; ///< 是否在播放
@property (nonatomic, assign, readonly) CGFloat curentPlayTimeValue; ///< 當前播放時間值
@property (nonatomic, copy, readonly) NSString *curentPlayTime; ///< 當前播放時間
@property (nonatomic, assign, readonly) CGFloat endPlayTimeValue; ///< 當前播放時間值
@property (nonatomic, copy, readonly) NSString *endPlayTime; ///< 當前播放時間
+ (instancetype)defaultManager;
/**
列表播放 ⚠️<預設不自動播放>
@param urls 檔案路徑陣列
@param delegate 回撥代理
@return MediaPlyerManager
*/
- (MediaPlyerManager *)playerWithUrls:(NSArray<NSString *> *)urls actionWithDelegate:(id<MediaPlyerManagerDelegate>)delegate;
/**
單個音視訊播放 ⚠️<預設不自動播放>
@param url 檔案路徑
@param delegate 回撥代理
@return MediaPlyerManager
*/
- (MediaPlyerManager *)playerWithUrl:(NSString *)url actionWithDelegate:(id<MediaPlyerManagerDelegate>)delegate;
/**
開始播放
*/
- (void)play;
/**
暫停播放
*/
- (void)pause;
/**
停止播放
*/
- (void)stop;
/**
下一曲
*/
- (void)next;
/**
上一曲
*/
- (void)previous;
/**
指定進度開始播放
@param progress 進度百分比
*/
- (void)setupPlayerSeekToProgress:(CGFloat)progress;
/**
制定播放型別
@param type 型別
*/
- (void)setupMediaPlayerType:(MediaPlayType)type;
/**
指定播放的index
@param index 索引
*/
- (void)setupPlayerIndex:(NSInteger)index;
/**
新增資料
@param files 檔案陣列
@param index 索引
*/
- (void)insertMediaFile:(NSArray<NSString *> *)files atIndex:(NSInteger)index;
/**
移除全部資料
*/
- (void)removeAllFiles;
/**
移除索引中的單個資料
@param index 索引
*/
- (void)removeObjectAtIndex:(NSInteger)index;
/**
設定鎖屏樣式
@param coverImage 專輯圖片
@param size 顯示大小
@param title 標題
@param author 專輯作者
@param album 專輯名稱
@param currentTime 當前播放時間
@param duration 播放總時長
*/
- (void)setupLockScreenPlayInfo:(UIImage *)coverImage
imageSize:(CGSize)size
title:(NSString *)title
ahthor:(NSString *)author
album:(NSString *)album
currentPlayTime:(CGFloat)currentTime
duration:(CGFloat)duration;
@end
NS_ASSUME_NONNULL_END
複製程式碼
程式碼實現檔案
#import "MediaPlyerManager.h"
#import <MediaPlayer/MediaPlayer.h>
@interface MediaPlyerManager ()
@property (nonatomic, strong, readwrite) NSMutableArray<NSString *> *dataUrlArray;
@property (nonatomic, strong, readwrite) AVPlayer *mediaPlayer;
@property (nonatomic, assign, readwrite) BOOL isPlaying;
@property (nonatomic, strong, readwrite) AVPlayerItem *meidaPlayerItem;
@property (nonatomic, assign, readwrite) MediaPlayType playeType;
@property (nonatomic, assign, readwrite) NSInteger currentIndex;
@property (nonatomic, assign, readwrite) MediaPlayStatus playStatus;
@property (nonatomic, assign, readwrite) CGFloat curentPlayTimeValue;
@property (nonatomic, copy, readwrite ) NSString *curentPlayTime;
@property (nonatomic, assign, readwrite) CGFloat endPlayTimeValue;
@property (nonatomic, copy, readwrite ) NSString *endPlayTime;
@property (nonatomic, weak ) id <MediaPlyerManagerDelegate> delegate;
@end
@implementation MediaPlyerManager
+ (instancetype)defaultManager {
static dispatch_once_t onceToken;
static MediaPlyerManager *manger;
dispatch_once(&onceToken, ^{
manger = [[MediaPlyerManager alloc] init];
});
return manger;
}
#pragma mark - 初始化
- (MediaPlyerManager *)playerWithUrl:(NSString *)url actionWithDelegate:(id<MediaPlyerManagerDelegate>)delegate {
[self playerWithUrls:@[url] actionWithDelegate:delegate];
return self;
}
- (MediaPlyerManager *)playerWithUrls:(NSArray<NSString *> *)urls actionWithDelegate:(id<MediaPlyerManagerDelegate>)delegate {
self.delegate = delegate;
self.currentIndex = 0;
self.dataUrlArray = [NSMutableArray array];
NSMutableArray *array = [NSMutableArray arrayWithCapacity:urls.count];
for (NSString *urlStr in urls) {
[array addObject:[self createPlayerItemWithUrl:urlStr]];
[self.dataUrlArray addObject:urlStr];
}
self.playeType = MediaPlayTypeCycle;
self.mediaPlayer = [[AVPlayer alloc] initWithPlayerItem:array.firstObject];
self.meidaPlayerItem = self.mediaPlayer.currentItem;
[self getCurrentIndex:self.currentIndex];
[self addObserver];
__weak typeof(self) weakself = self;
[self.mediaPlayer addPeriodicTimeObserverForInterval:CMTimeMake(1, 1) queue:NULL usingBlock:^(CMTime time) {
NSString *currentString = [weakself getStringFromCMTime:time];
weakself.curentPlayTime = currentString;
weakself.curentPlayTimeValue = (CGFloat)time.value/time.timescale;
#pragma mark - 獲取當前播放時間
if (weakself.delegate && [weakself.delegate respondsToSelector:@selector(MediaPlayer:currentPlayTime:currentPlayTimeValue:)]) {
[weakself.delegate MediaPlayer:weakself currentPlayTime:currentString currentPlayTimeValue:(CGFloat)time.value/time.timescale];
}
#pragma mark - 實時獲取播放資訊
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:currentProgressValue:totalValue:currentIndex:)]) {
[weakself.delegate MediaPlayer:weakself currentProgressValue:weakself.curentPlayTimeValue totalValue:weakself.endPlayTimeValue currentIndex:weakself.currentIndex];
}
}];
return self;
}
#pragma mark - 播放結束
- (void)playFinish:(NSNotification *)notification {
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayerCurrentMediaPlayFinish:)]) {
[self.delegate MediaPlayerCurrentMediaPlayFinish:self];
}
if (self.playeType == MediaPlayTypeSingle) {
[self.mediaPlayer seekToTime:kCMTimeZero];
[self play];
} else {
if (self.currentIndex < self.dataUrlArray.count - 1) {
self.currentIndex += 1;
} else {
self.currentIndex = 0;
}
[self.mediaPlayer replaceCurrentItemWithPlayerItem:[self createPlayerItemWithUrl:self.dataUrlArray[self.currentIndex]]];
self.meidaPlayerItem = self.mediaPlayer.currentItem;
[self getCurrentIndex:self.currentIndex];
[self play];
}
}
#pragma mark - KVO
- (void)observeValueForKeyPath:(NSString *)keyPath
ofObject:(id)object
change:(NSDictionary<NSKeyValueChangeKey,id> *)change
context:(void *)context {
AVPlayerItem *playerItem = object;
if ([keyPath isEqualToString:@"status"]) {
MediaLoadStatus status = [change[@"new"] integerValue];
#pragma mark - 獲取媒體載入狀態
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:playerItemStatus:)]) {
[self.delegate MediaPlayer:self playerItemStatus:status];
}
} else if ([keyPath isEqualToString:@"loadedTimeRanges"]) {
NSArray * timeRanges = playerItem.loadedTimeRanges;
CMTimeRange timeRange = [timeRanges.firstObject CMTimeRangeValue];
NSTimeInterval totalLoadTime = CMTimeGetSeconds(timeRange.start) \
+ CMTimeGetSeconds(timeRange.duration);
NSTimeInterval duration = CMTimeGetSeconds(playerItem.duration);
NSTimeInterval scale = totalLoadTime/duration;
#pragma mark - 獲取媒體總時間
if ((CGFloat)duration/scale >= 0) {
self.endPlayTime = [self getStringFromCMTime:playerItem.duration];
self.endPlayTimeValue = (CGFloat)duration/scale;
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:mediaEndTime:mediaEndTimeValue:)]) {
[self.delegate MediaPlayer:self mediaEndTime:[self getStringFromCMTime:playerItem.duration] mediaEndTimeValue:(CGFloat)duration/scale];
}
}
#pragma mark - 緩衝百分比
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:netBufferValue:)]) {
[self.delegate MediaPlayer:self netBufferValue:scale];
}
} else if ([keyPath isEqualToString:@"playbackBufferEmpty"]) {
#pragma mark - 緩衝不足夠播放
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:bufferHasEnough:)]) {
[self.delegate MediaPlayer:self bufferHasEnough:false];
}
} else if ([keyPath isEqualToString:@"playbackLikelyToKeepUp"]) {
#pragma mark - 緩衝足夠播放
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:bufferHasEnough:)]) {
[self.delegate MediaPlayer:self bufferHasEnough:true];
}
}
}
#pragma mark - 開始播放
- (void)play {
[self.mediaPlayer play];
[self getPlayStatus:MediaPlayStatusPlaying];
}
#pragma mark - 暫停播放
- (void)pause {
[self.mediaPlayer pause];
[self getPlayStatus:MediaPlayStatusPause];
}
#pragma mark - 停止播放
- (void)stop {
[self.mediaPlayer replaceCurrentItemWithPlayerItem:nil];
[self getPlayStatus:MediaPlayStatusStop];
[self removeObserver];
}
#pragma mark - 下一個
- (void)next {
if (self.playeType == MediaPlayTypeRandom) {
self.currentIndex = (NSInteger)arc4random_uniform((int32_t)(self.dataUrlArray.count - 1));
} else {
if (self.currentIndex == self.dataUrlArray.count - 1) {
self.currentIndex = 0;
} else {
self.currentIndex += 1;
}
}
[self.mediaPlayer replaceCurrentItemWithPlayerItem:[self createPlayerItemWithUrl:self.dataUrlArray[self.currentIndex]]];
self.meidaPlayerItem = self.mediaPlayer.currentItem;
[self getCurrentIndex:self.currentIndex];
[self addObserver];
}
#pragma mark - 上一個
- (void)previous {
if (self.playeType == MediaPlayTypeRandom) {
self.currentIndex = (NSInteger)arc4random_uniform((int32_t)(self.dataUrlArray.count - 1));
} else {
if (self.currentIndex == 0) {
self.currentIndex = self.dataUrlArray.count - 1;
} else {
self.currentIndex -= 1;
}
}
[self.mediaPlayer replaceCurrentItemWithPlayerItem:[self createPlayerItemWithUrl:self.dataUrlArray[self.currentIndex]]];
self.meidaPlayerItem = self.mediaPlayer.currentItem;
[self getCurrentIndex:self.currentIndex];
[self addObserver];
}
#pragma mark - 播放狀態
- (void)getPlayStatus:(MediaPlayStatus)status {
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:playeStatus:)]) {
[self.delegate MediaPlayer:self playeStatus:status];
}
self.playStatus = status;
if (status == MediaPlayStatusPlaying) {
self.isPlaying = true;
} else {
self.isPlaying = false;
}
}
#pragma mark - 根據index進行回撥
- (void)getCurrentIndex:(NSInteger)index {
if (self.delegate && [self.delegate respondsToSelector:@selector(MediaPlayer:currentUrl:currentIndex:)]) {
[self.delegate MediaPlayer:self currentUrl:self.dataUrlArray[index] currentIndex:index];
}
}
#pragma mark - 設定播放進度百分比
- (void)setupPlayerSeekToProgress:(CGFloat)progress {
float timeValue = progress * CMTimeGetSeconds(self.mediaPlayer.currentItem.duration);
[self.mediaPlayer seekToTime:CMTimeMake(timeValue, 1)];
}
#pragma mark - 設定播放形式
- (void)setupMediaPlayerType:(MediaPlayType)type {
self.playeType = type;
}
#pragma mark - 播放指定index的媒體
- (void)setupPlayerIndex:(NSInteger)index {
if (index > (self.dataUrlArray.count - 1)) {
@throw [NSException exceptionWithName:@"越界錯誤" reason:@"index 不能超出URL陣列的長度" userInfo:nil];
return;
}
self.currentIndex = index;
[self.mediaPlayer replaceCurrentItemWithPlayerItem:[self createPlayerItemWithUrl:self.dataUrlArray[self.currentIndex]]];
self.meidaPlayerItem = self.mediaPlayer.currentItem;
[self getCurrentIndex:self.currentIndex];
}
#pragma mark - 插入資料
- (void)insertMediaFile:(NSArray<NSString *> *)files atIndex:(NSInteger)index {
for (NSString *urlStr in files) {
NSInteger i = [files indexOfObject:urlStr];
[self.dataUrlArray insertObject:urlStr atIndex:index + i];
}
if (index < self.currentIndex) {
self.currentIndex += 1;
}
}
#pragma mark - 刪除資料
- (void)removeAllFiles {
[self stop];
[self.dataUrlArray removeAllObjects];
self.dataUrlArray = [NSMutableArray array];
self.currentIndex = 0;
}
- (void)removeObjectAtIndex:(NSInteger)index {
if (self.dataUrlArray.count == 1) {
[self removeAllFiles];
} else {
[self.dataUrlArray removeObjectAtIndex:index];
if (index == self.currentIndex) {
if (index == 0) {
self.currentIndex = 0;
[self next];
} else {
self.currentIndex -= 1;
}
} else {
if (self.currentIndex > index) {
self.currentIndex -= 1;
}
}
}
}
#pragma mark - Utils
- (NSString *)getStringFromCMTime:(CMTime)time {
float currentTimeValue = (CGFloat)time.value/time.timescale;
NSDate * currentDate = [NSDate dateWithTimeIntervalSince1970:currentTimeValue];
NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
NSInteger unitFlags = NSCalendarUnitHour | NSCalendarUnitMinute | NSCalendarUnitSecond;
NSDateComponents *components = [calendar components:unitFlags fromDate:currentDate];
if (currentTimeValue >= 3600 ) {
return [NSString stringWithFormat:@"%02ld:%02ld:%02ld", (long)components.hour, (long)components.minute, (long)components.second];
} else {
return [NSString stringWithFormat:@"%02ld:%02ld", (long)components.minute, (long)components.second];
}
}
- (void)addObserver {
// 監控狀態屬性
[self.meidaPlayerItem addObserver:self
forKeyPath:@"status"
options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
context:nil];
// 監控緩衝載入情況屬性
[self.meidaPlayerItem addObserver:self
forKeyPath:@"loadedTimeRanges"
options:(NSKeyValueObservingOptionOld|NSKeyValueObservingOptionNew)
context:nil];
// 監聽緩衝不足夠播放
[self.meidaPlayerItem addObserver:self
forKeyPath:@"playbackBufferEmpty"
options:NSKeyValueObservingOptionNew
context:nil];
// 監聽緩衝足夠播放
[self.meidaPlayerItem addObserver:self
forKeyPath:@"playbackLikelyToKeepUp"
options:NSKeyValueObservingOptionNew
context:nil];
// 獲取是否播放結束
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playFinish:)
name:AVPlayerItemDidPlayToEndTimeNotification
object:self.meidaPlayerItem];
}
- (void)removeObserver {
[[NSNotificationCenter defaultCenter] removeObserver:self];
@try {
[self.meidaPlayerItem removeObserver:self forKeyPath:@"loadedTimeRanges"];
[self.meidaPlayerItem removeObserver:self forKeyPath:@"playbackBufferEmpty"];
[self.meidaPlayerItem removeObserver:self forKeyPath:@"playbackLikelyToKeepUp"];
[self.meidaPlayerItem removeObserver:self forKeyPath:@"status"];
}
@catch(NSException *exception) {
NSLog(@"%@", exception);
}
}
#pragma mark - 設定鎖屏樣式
- (void)setupLockScreenPlayInfo:(UIImage *)coverImage
imageSize:(CGSize)size
title:(NSString *)title
ahthor:(NSString *)author
album:(NSString *)album
currentPlayTime:(CGFloat)currentTime
duration:(CGFloat)duration {
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithBoundsSize:size requestHandler:^UIImage * _Nonnull(CGSize size) {
return coverImage;
}];
[songInfo setObject:title forKey:MPMediaItemPropertyTitle];
[songInfo setObject:author forKey:MPMediaItemPropertyArtist];
[songInfo setObject:album forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[songInfo setObject:[NSNumber numberWithDouble:currentTime] forKey:MPNowPlayingInfoPropertyElapsedPlaybackTime];
[songInfo setObject:[NSNumber numberWithDouble:duration] forKey:MPMediaItemPropertyPlaybackDuration];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
}
- (AVPlayerItem *)createPlayerItemWithUrl:(NSString *)url {
return [AVPlayerItem playerItemWithURL:[NSURL URLWithString:url]];
}
@end
複製程式碼
提問
本來相同使用AVQueuePlayer來進行列表播放的,但是當做單曲迴圈的時候遇到問題:通過通知監聽播放完成,在通知的方法裡進行具體操作,但是設定無效,直接播放的還是下一個檔案。如果有人知道如何解決,幫忙回覆一下。