實(shí)例講解iOS音樂播放器DOUAudioStreamer用法
好久沒有寫東西了,最近加班太嚴(yán)重,今天抽空把用到的音樂播放器DOUAudioStreamer整理一下,由于項(xiàng)目之前用的是AVPlayer,這個(gè)也可以,但是就是要先緩存一段時(shí)間再播放,老板看了之后要求,要變緩存變播放(有網(wǎng)時(shí),點(diǎn)擊播放按鈕就立刻播放),怎么不早說!怎么不早說!怎么不早說!還能怎樣?只能原諒他,繼續(xù)敲代碼。。。。。。(還是直接上代碼吧)
一、導(dǎo)入三方庫
pod 'DOUAudioStreamer'
或者GitHup下載地址:https://github.com/douban/DOUAudioStreamer
二、使用
1.從demo中獲取NAKPlaybackIndicatorView文件和MusicIndicator.h和MusicIndicator.m 文件,并導(dǎo)入頭文件
//音樂播放
#import "DOUAudioStreamer.h"
#import "NAKPlaybackIndicatorView.h"
#import "MusicIndicator.h"
#import "Track.h"
如圖:

2.創(chuàng)建一個(gè)Track類,用于音樂播放的URL存放

3.需要的界面.h中,添加DOUAudioStreamer,并用單利來初始化
+ (instancetype)sharedInstance ; @property (nonatomic, strong) DOUAudioStreamer *streamer;
如圖:

在.m中實(shí)現(xiàn):
static void *kStatusKVOKey = &kStatusKVOKey;
static void *kDurationKVOKey = &kDurationKVOKey;
static void *kBufferingRatioKVOKey = &kBufferingRatioKVOKey;
@property (strong, nonatomic) MusicIndicator *musicIndicator;
@property (nonatomic, strong) Track *audioTrack;
+ (instancetype)sharedInstance {
static HYNEntertainmentController *_sharedMusicVC = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_sharedMusicVC = [[HYNEntertainmentController alloc] init];
_sharedMusicVC.streamer = [[DOUAudioStreamer alloc] init];
});
return _sharedMusicVC;
}
播放按鈕事件
#pragma mark ---音樂播放按鈕
-(void)playMusicStart:(UIButton *)sender
{
//通過按鈕獲取cell
MusicCollectionViewCell *musicCell = (MusicCollectionViewCell *)[[sender superview] superview];
if(_playFirst == 0){//_playFirst == 0首次播放,其他為暫停
NSURL *url = [NSURL URLWithString:HttpImgUrl(musicCell.model.musicUrl)];
_audioTrack.audioFileURL = url;
@try {
[self removeStreamerObserver];
} @catch(id anException){
}
//在DOUAudioStreamer進(jìn)行播放時(shí),必須先置為nil
_streamer = nil;
_streamer = [DOUAudioStreamer streamerWithAudioFile:_audioTrack];
[self addStreamerObserver];
[_streamer play];
}
if([_streamer status] == DOUAudioStreamerPaused ||
[_streamer status] == DOUAudioStreamerIdle){
[sender setBackgroundImage:[UIImage imageNamed:@"music_play_icon"] forState:UIControlStateNormal];
[_streamer play];
}else{
[sender setBackgroundImage:[UIImage imageNamed:@"music_stop_icon"] forState:UIControlStateNormal];
[_streamer pause];
}
_playFirst++;
}
對添加監(jiān)聽
- (void)addStreamerObserver {
[_streamer addObserver:self forKeyPath:@"status" options:NSKeyValueObservingOptionNew context:kStatusKVOKey];
[_streamer addObserver:self forKeyPath:@"duration" options:NSKeyValueObservingOptionNew context:kDurationKVOKey];
[_streamer addObserver:self forKeyPath:@"bufferingRatio" options:NSKeyValueObservingOptionNew context:kBufferingRatioKVOKey];
}
/// 播放器銷毀
- (void)dealloc{
if (_streamer !=nil) {
[_streamer pause];
[_streamer removeObserver:self forKeyPath:@"status" context:kStatusKVOKey];
[_streamer removeObserver:self forKeyPath:@"duration" context:kDurationKVOKey];
[_streamer removeObserver:self forKeyPath:@"bufferingRatio" context:kBufferingRatioKVOKey];
_streamer =nil;
}
}
- (void)removeStreamerObserver {
[_streamer removeObserver:self forKeyPath:@"status"];
[_streamer removeObserver:self forKeyPath:@"duration"];
[_streamer removeObserver:self forKeyPath:@"bufferingRatio"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == kStatusKVOKey) {
[self performSelector:@selector(updateStatus)
onThread:[NSThread mainThread]
withObject:nil
waitUntilDone:NO];
} else if (context == kDurationKVOKey) {
[self performSelector:@selector(updateSliderValue:)
onThread:[NSThread mainThread]
withObject:nil
waitUntilDone:NO];
} else if (context == kBufferingRatioKVOKey) {
[self performSelector:@selector(updateBufferingStatus)
onThread:[NSThread mainThread]
withObject:nil
waitUntilDone:NO];
} else {
[super observeValueForKeyPath:keyPath ofObject:object change:change context:context];
}
}
- (void)updateSliderValue:(id)timer {
}
-(void)updateBufferingStatus
{
}
- (void)updateStatus {
//self.musicIsPlaying = NO;
_musicIndicator.state = NAKPlaybackIndicatorViewStateStopped;
switch ([_streamer status]) {
case DOUAudioStreamerPlaying:
// self.musicIsPlaying = YES;
_musicIndicator.state = NAKPlaybackIndicatorViewStatePlaying;
break;
case DOUAudioStreamerPaused:
break;
case DOUAudioStreamerIdle:
break;
case DOUAudioStreamerFinished:
break;
case DOUAudioStreamerBuffering:
_musicIndicator.state = NAKPlaybackIndicatorViewStatePlaying;
break;
case DOUAudioStreamerError:
break;
}
}
這樣就能播放了。
鎖屏?xí)r的音樂顯示、拔出耳機(jī)后暫停播放、監(jiān)聽音頻打斷事件
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
//接受遠(yuǎn)程控制
[self becomeFirstResponder];
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
//這個(gè)不能忘記了
-(BOOL)canBecomeFirstResponder{
return YES;
}
- (void)viewDidLoad {
[super viewDidLoad];
//音樂播放器
[self initPlayer];
}
#pragma mark =========================音樂播放==============================
//音樂播放器
-(void)initPlayer
{
_audioTrack = [[Track alloc] init];
AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayback error:nil];
//讓app支持接受遠(yuǎn)程控制事件
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
//添加通知,拔出耳機(jī)后暫停播放
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(routeChange:) name:AVAudioSessionRouteChangeNotification object:nil];
// 監(jiān)聽音頻打斷事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionWasInterrupted:) name:AVAudioSessionInterruptionNotification object:session];
}
// 監(jiān)聽音頻打斷事件
- (void)audioSessionWasInterrupted:(NSNotification *)notification
{
//被打斷時(shí)
if (AVAudioSessionInterruptionTypeBegan == [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue])
{
[_streamer pause];
UIButton *btn = (UIButton *)[self.view viewWithTag:2000];
[btn setBackgroundImage:[UIImage imageNamed:@"music_stop_icon"] forState:UIControlStateNormal];
}
else if (AVAudioSessionInterruptionTypeEnded == [notification.userInfo[AVAudioSessionInterruptionTypeKey] intValue])
{
}
}
// 拔出耳機(jī)后暫停播放
-(void)routeChange:(NSNotification *)notification{
NSDictionary *dic=notification.userInfo;
int changeReason= [dic[AVAudioSessionRouteChangeReasonKey] intValue];
//等于AVAudioSessionRouteChangeReasonOldDeviceUnavailable表示舊輸出不可用
if (changeReason==AVAudioSessionRouteChangeReasonOldDeviceUnavailable) {
AVAudioSessionRouteDescription *routeDescription=dic[AVAudioSessionRouteChangePreviousRouteKey];
AVAudioSessionPortDescription *portDescription= [routeDescription.outputs firstObject];
//原設(shè)備為耳機(jī)則暫停
if ([portDescription.portType isEqualToString:@"Headphones"]) {
[_streamer pause];
UIButton *btn = (UIButton *)[self.view viewWithTag:2000];
[btn setBackgroundImage:[UIImage imageNamed:@"music_stop_icon"] forState:UIControlStateNormal];
}
}
}
//鎖屏?xí)r音樂顯示(這個(gè)方法可以在點(diǎn)擊播放時(shí),調(diào)用傳值)
- (void)setupLockScreenInfoWithSing:(NSString *)sign WithSigner:(NSString *)signer WithImage:(UIImage *)image
{
// 1.獲取鎖屏中心
MPNowPlayingInfoCenter *playingInfoCenter = [MPNowPlayingInfoCenter defaultCenter];
//初始化一個(gè)存放音樂信息的字典
NSMutableDictionary *playingInfoDict = [NSMutableDictionary dictionary];
// 2、設(shè)置歌曲名
if (sign) {
[playingInfoDict setObject:sign forKey:MPMediaItemPropertyAlbumTitle];
}
// 設(shè)置歌手名
if (signer) {
[playingInfoDict setObject:signer forKey:MPMediaItemPropertyArtist];
}
// 3設(shè)置封面的圖片
//UIImage *image = [self getMusicImageWithMusicId:self.currentModel];
if (image) {
MPMediaItemArtwork *artwork = [[MPMediaItemArtwork alloc] initWithImage:image];
[playingInfoDict setObject:artwork forKey:MPMediaItemPropertyArtwork];
}
// 4設(shè)置歌曲的總時(shí)長
//[playingInfoDict setObject:self.currentModel.detailDuration forKey:MPMediaItemPropertyPlaybackDuration];
//音樂信息賦值給獲取鎖屏中心的nowPlayingInfo屬性
playingInfoCenter.nowPlayingInfo = playingInfoDict;
// 5.開啟遠(yuǎn)程交互
[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
}
//鎖屏?xí)r操作
- (void)remoteControlReceivedWithEvent:(UIEvent *)receivedEvent {
if (receivedEvent.type == UIEventTypeRemoteControl) {
UIButton *sender = (UIButton *)[self.view viewWithTag:2000];
switch (receivedEvent.subtype) {//判斷是否為遠(yuǎn)程控制
case UIEventSubtypeRemoteControlPause:
[[HYNEntertainmentController sharedInstance].streamer pause];
[sender setBackgroundImage:[UIImage imageNamed:@"music_stop_icon"] forState:UIControlStateNormal];
break;
case UIEventSubtypeRemoteControlStop:
break;
case UIEventSubtypeRemoteControlPlay:
[[HYNEntertainmentController sharedInstance].streamer play];
[sender setBackgroundImage:[UIImage imageNamed:@"music_play_icon"] forState:UIControlStateNormal];
break;
case UIEventSubtypeRemoteControlTogglePlayPause:
break;
case UIEventSubtypeRemoteControlNextTrack:
break;
case UIEventSubtypeRemoteControlPreviousTrack:
break;
default:
break;
}
}
}
整體圖片:

上圖為未播放

上圖為播放中

上圖為鎖屏?xí)r狀態(tài)
應(yīng)該沒有什么要添加的了,暫時(shí)告一段落,有不足之處,可以在下方的留言區(qū)討論,感謝對腳本之家的支持。
- ios 流媒體播放器實(shí)現(xiàn)流程及FreeStreamer的使用的示例
- iOS之基于FreeStreamer的簡單音樂播放器示例
- 運(yùn)用iOS教你輕松制作音樂播放器
- ios開發(fā):一個(gè)音樂播放器的設(shè)計(jì)與實(shí)現(xiàn)案例
- iOS中視頻播放器的簡單封裝詳解
- iOS中的音頻服務(wù)和音頻AVAudioPlayer音頻播放器使用指南
- 實(shí)例解析iOS中音樂播放器應(yīng)用開發(fā)的基本要點(diǎn)
- iOS開發(fā)中音頻工具類的封裝以及音樂播放器的細(xì)節(jié)控制
- iOS音樂播放器實(shí)現(xiàn)代碼完整版
相關(guān)文章
IOS 實(shí)現(xiàn)微信自動(dòng)搶紅包(非越獄IPhone)
這篇文章主要介紹了IOS 實(shí)現(xiàn)微信自動(dòng)搶紅包(非越獄IPhone)的相關(guān)資料,這里對實(shí)現(xiàn)自動(dòng)搶紅包做一個(gè)詳細(xì)的實(shí)現(xiàn)步驟,需要的朋友可以參考下2016-11-11
IOS開發(fā)筆記整理49之詳解定位CLLocation
在項(xiàng)目功能中有一個(gè)定位CLLocation的需求,遇到了一些知識難點(diǎn),經(jīng)過各位大俠的幫助,問題解決,特此分享供大家學(xué)習(xí),希望大家共同學(xué)習(xí)進(jìn)步2015-11-11
iOS中.a和.framework靜態(tài)庫的創(chuàng)建與.bundle資源包的使用詳解
這篇文章主要給大家介紹了關(guān)于在iOS中.a和.framework靜態(tài)庫的創(chuàng)建與.bundle資源包的使用的相關(guān)資料,文中介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
ios獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例
下面小編就為大家?guī)硪黄猧os獲取數(shù)據(jù)之encodeURI和decodeURI的實(shí)例。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11
iOS之Https自簽名證書認(rèn)證及數(shù)據(jù)請求的封裝原理
本篇文章主要介紹了iOS之Https自簽名證書認(rèn)證及數(shù)據(jù)請求的封裝原理,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02
HTTP/2 協(xié)議用于 iOS 推送提醒服務(wù) (APNS)
基于JSON的請求和響應(yīng)對于每個(gè)通知,如果成功響應(yīng),將會(huì)返回200標(biāo)識 - 不用再去猜測通知是否被接收到響應(yīng)錯(cuò)誤將會(huì)以JSON字符消息的長度從2048個(gè)字節(jié)增加到4096個(gè)字節(jié)連接狀態(tài)可以通過HTTP/2的ping框架來進(jìn)行檢查.2016-04-04
iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實(shí)例代碼
這篇文章主要介紹了 iOS中關(guān)于音樂鎖屏控制音樂(鎖屏信息設(shè)置)的實(shí)例代碼,需要的朋友可以參考下2017-01-01

