音樂播放

  • AVAudioPlayer :播放音樂, 只能播放本地
  • AVPlayer :播放音樂 ,本地、遠程通吃, 并且還可以播放視頻
  • MPMoviePlayerController: 播放音樂,本地、遠程通吃, 并且還可以播放視頻
  • MPMoviePlayerViewController:播放音樂,本地、遠程通吃, 并且還可以播放視頻

一、AVAudioPlayer

1.導入頭文件
#import <AVFoundation/AVFoundation.h>

2.播放音樂

- (IBAction)play {
   if (![self.player isPlaying]) {  
        [self.player play];
    }

    // 創建播放器
    NSURL*url = [[NSBundle mainBundle] URLForResource:@"一東.mp3"withExtension:nil];

    AVAudioPlayer*player = [[AVAudioPlayeralloc] initWithContentsOfURL:url error:nil];

    // 是否允許快進
    player.enableRate = YES;

    // 快進速度
    player.rate = 3;
    self.player = player;

    // 準備播放
   if ([player prepareToPlay]) {
    // 播放音樂    
      [player play];
    }
}

- (IBAction)pause {
    [self.player pause];
}

- (IBAction)stop {
    [self.player stop];

    // 停止播放后從內存清空
    self.player =nil;
}

二、AVPlayer(本地音樂/在線音樂)

  1. 本地音樂
#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/**
 *  播放器
 */
@property (nonatomic, strong) AVPlayer *player;

@end

@implementation ViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // 初始化播放器
    NSURL*url = [[NSBundlemainBundle] URLForResource:@"Background.caf"withExtension:nil];

    self.player = [AVPlayer playerWithURL:url];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.player play];
}
@end

2.在線音樂:

#import <AVFoundation/AVFoundation.h>
@interface ViewController ()
/**
*  播放器
*/
@property (nonatomic, strong) AVPlayer *player;
@end

@implementation ViewController
- (void)viewDidLoad {
   [super viewDidLoad];

   // 初始化播放器
   NSURL*url = [NSURLURLWithString:@"http://y1.eoews.com/assets/ringtones/2012/5/18/34049/oiuxsvnbtxks7a0tg6xpdo66exdhi8h0bplp7twp.mp3"];

   self.player = [AVPlayer playerWithURL:url];
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
   [self.player play];
}
@end

注意:
AVPlayer不能獲得播放進度,想要獲取在線音樂下載進度等信息,需要第三方框架。

三、第三方框架播放流媒體(在線)音樂(DOUAudioStreamer)

蘋果自帶的AVPlayer和MPMoviePlayerController能播放流媒體音頻, 但是提供的功能接口較少, 無法實現過于復雜和個性化的功能。如果想實現一些個性化\復雜的操作, 可以借助一些第三方框架輕易實現。

下面介紹DOUAudioStreamer的使用

  1. 導入src文件夾下面的所有源代碼

  2. 導入依賴的框架

  3. 包含主頭文件

    #import "DOUAudioStreamer.h"
    

4.新建一個遵守協議的模型類,用于提供音頻文件的遠程路徑

#import "DOUAudioStreamer.h"
@interface CJAudioFile : NSObject<DOUAudioFile>
/**
 *  音頻文件路徑
 */
@property(strong, nonatomic) NSURL*audioFileURL;
@end

5.DOUAudioStreamer類常見屬性

// 文件的總大小
@property (nonatomic, readonly) NSUIntegerexpectedLength;

// 目前已下載的文件大小
@property (nonatomic, readonly) NSUIntegerreceivedLength;

// 下載速度
@property (nonatomic, readonly) NSUIntegerdownloadSpeed;

// 緩沖比例
@property (nonatomic, assign, readonly) doublebufferingRatio;

// 音量
@property (nonatomic, assign) doublevolume;

// 緩存路徑(在沙盒的tmp目錄, 隨時會被刪除)
@property (nonatomic, readonly) NSString*cachedPath;

// 歌曲的總時長
@property (nonatomic, assign, readonly) NSTimeIntervalduration;

// 歌曲的當前播放時長
@property (nonatomic, assign) NSTimeIntervalcurrentTime;

// 播放器狀態
@property (assign, readonly) DOUAudioStreamerStatusstatus;

7.播放器狀態

typedefNS_ENUM(NSUInteger, DOUAudioStreamerStatus) {

 DOUAudioStreamerPlaying, // 正在播放

 DOUAudioStreamerPaused,  // 暫停

 DOUAudioStreamerIdle, // 停止播放

 DOUAudioStreamerFinished, // 播放完畢

 DOUAudioStreamerBuffering, // 正在緩沖

 DOUAudioStreamerError // 播放錯誤
};

8.傳入模型,開始播放音樂

self.audioStreamer=[DOUAudioStreamerstreamerWithAudioFile:file];

[self.audioStreamer play];
 

9.可以通過KVO監聽播放器的狀態

[self.audioStreamer addObserver:self forKeyPath:CJStatusProp options:NSKeyValueObservingOptionNewcontext:nil];

[self.audioStreamer addObserver:self forKeyPath:CJDurationProp options:NSKeyValueObservingOptionNewcontext:nil];

[self.audioStreamer addObserver:self forKeyPath:CJBufferingRatioProp options:NSKeyValueObservingOptionNewcontext:nil];
 

10.暫停\停止播放

[self.audioStreamer pause];

[self.audioStreamer stop];

11.移除KVO監聽

[self.audioStreamer removeObserver:self forKeyPath:CJStatusProp];

[self.audioStreamer removeObserver:self forKeyPath:CJDurationProp];

[self.audioStreamer removeObserver:self forKeyPath:CJBufferingRatioProp];

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容