一、音频播放
1.音效播放(短时间的音频文件)
1> AudioServicesCreateSystemSoundID
2> AudioServicesPlaySystemSound
2.音乐播放(长时间的音频文件)
1> AVAudioPlayer
只能播放本地的音频文件
2> AVPlayer
能播放本地、远程的音频、视频文件
基于Layer显示,得自己去编写控制面板
3> MPMoviePlayerController
能播放本地、远程的音频、视频文件
自带播放控制面板(暂停、播放、播放进度、是否要全屏)
4> MPMoviewPlayerViewController
能播放本地、远程的音频、视频文件
内部是封装了MPMoviePlayerController
播放界面默认就是全屏的
如果播放功能比较简单,仅仅是简单地播放远程、本地的视频文件,建议用这个
实例:AVPlayer MPMoviePlayerController, MPMoviewPlayerViewController 本地视频播放
5> DOUAudioStreamer
能播放远程、本地的音频文件
监听缓冲进度、下载速度、下载进度
#import "HMViewController.h" #import "HMAudioFile.h" #import "UIView+Extension.h" #define MJStatusProp @"status" #define MJBufferingRatioProp @"bufferingRatio" #import "DOUAudioStreamer.h" @interface HMViewController () /** 播放器 */ @property (weak, nonatomic) IBOutlet UILabel *infoLabel; @property (nonatomic, strong) DOUAudioStreamer *streamer; @property (weak, nonatomic) IBOutlet UIView *positionProgressView; @property (weak, nonatomic) IBOutlet UIView *downloadProgressView; @property (weak, nonatomic) IBOutlet UIView *progressBg; @property (strong, nonatomic) NSTimer *currentTimeTimer; @end @implementation HMViewController #pragma mark - 定时器 - (void)addTimer { self.currentTimeTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateCurrentTime) userInfo:nil repeats:YES]; [[NSRunLoop mainRunLoop] addTimer:self.currentTimeTimer forMode:NSRunLoopCommonModes]; } - (void)removeTimer { [self.currentTimeTimer invalidate]; self.currentTimeTimer = nil; } - (void)viewDidLoad { [super viewDidLoad]; // 创建音频文件模型(提供音频文件路径) HMAudioFile *file = [[HMAudioFile alloc] init]; file.audioFileURL = [NSURL URLWithString:@"http://y1.eoews.com/assets/ringtones/2012/5/18/34045/hi4dwfmrxm2citwjcc5841z3tiqaeeoczhbtfoex.mp3"]; // 创建播放器 self.streamer = [DOUAudioStreamer streamerWithAudioFile:file]; // KVO监听streamer的属性(Key value Observing) [self.streamer addObserver:self forKeyPath:MJStatusProp options:NSKeyValueObservingOptionOld context:nil]; [self.streamer addObserver:self forKeyPath:MJBufferingRatioProp options:NSKeyValueObservingOptionOld context:nil]; // 播放 [self.streamer play]; [self addTimer]; UIWebView *webView; webView.scalesPageToFit = YES; } - (void)dealloc { [self.streamer removeObserver:self forKeyPath:MJStatusProp]; [self.streamer removeObserver:self forKeyPath:MJBufferingRatioProp]; } /** 利用KVO监听的属性值改变了,就会调用 */ - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { dispatch_async(dispatch_get_main_queue(), ^{ if ([keyPath isEqualToString:MJStatusProp]) { // 监听到播放器状态改变了 // NSLog(@"状态改变了----%d", self.streamer.status); } else if ([keyPath isEqualToString:MJBufferingRatioProp]) { // 监听到缓冲比例改变 double unit = 1000.0; // 总长度 double expectedLength = self.streamer.expectedLength / unit / unit; // 已经下载长度 double receivedLength = self.streamer.receivedLength / unit / unit; // 下载速度 double downloadSpeed = self.streamer.downloadSpeed / unit; self.infoLabel.text = [NSString stringWithFormat:@"缓冲:%.2fMB/%.2fMB(%.0f%%)\n下载速度:%.2fKB/s", receivedLength, expectedLength, (receivedLength/ expectedLength) * 100, downloadSpeed]; self.downloadProgressView.width = self.progressBg.width * (receivedLength/ expectedLength); } }); } - (void)updateCurrentTime { self.positionProgressView.width = self.progressBg.width * (self.streamer.currentTime / self.streamer.duration); } // AVAudioPlayer : 只能播放本地的音频文件 // AVPlayer : 能播放远程\本地的音频、视频文件 // MPMoviePlayerController : 能播放远程\本地的音频、视频文件 // MPMoviePlayerViewController : 能播放远程\本地的音频、视频文件 // self.player = [AVPlayer playerWithURL:[NSURL URLWithString:@"http://file.qianqian.com/data2/music/42275287/42275287.mp3?xcode=e41a36a1198cf7f07c498ac14eafd4a5398370073e4f3405"]]; @end