视频播放 MediaPlayer.framework

MPMoviePlayerViewController VS MPMoviePlayerController

MPMoviePlayerViewController

MPMoviePlayerController

版本支持

Available in iOS 3.2 and later.

Available in iOS 2.0 and later.(多数属性支持3.2后)

大小

只支持全屏播放  如果addsubview 不支持横竖屏

可全屏也可自己设置frame

调用

presentMoviePlayerViewControllerAnimated:

dismissMoviePlayerViewControllerAnimated

addsubview:

属性

moviePlayer

[mMPVC. moviePlayer play];

BOOL shouldAutoplay

NSTimInterval initialPlaybackTime

NSTimeInterval duration

MPMovieControlStyle controlStyle

函数

initWithContentURL

shouldAutorotateToInterfaceOrientation

initWithContentURL

requestThumbnailImagesAtTimes:timeOption

thumbnailImageAtTime:timeOption

timedMetadata (4.0)

notification

MPMoviePlayerPlaybackDidFinishNotification

播放完成

MPMovieMediaTypesAvailableNotification

视频开始播放 (载入完成)

MPMoviePlayerNowPlayingMovieDidChangeNotification

视频开播 (开始载入)

MPMoviePlayerPlaybackStateDidChangeNotification

播放状态变化

判断 mediaPlayer.playbackState

MPMoviePlayerDidEnterFullscreenNotification

全屏 相关

另外 UIWebview播放方式 方便 但是对一些视频不支持 经测试有的流媒体的 使用 MPMoviePlayerController 可以播放 但 UIWebview不支持.

因 MPMoviePlayerController 为单例4.0之后 可使用 AVPlayerLayer 的播放方式 addSubLayer实现多个视频同时播放

player1 = [AVPlayer playerWithURL:[NSURL fileURLWithPath:moviePath]];

player1.actionAtItemEnd = AVPlayerActionAtItemEndNone;

[[NSNotificationCenter defaultCenter] addObserver:self

selector:@selector(playerItemDidReachEnd:)

name:AVPlayerItemDidPlayToEndTimeNotification

object:[player1 currentItem]];

[player1 play];

playerLayer1 = [AVPlayerLayer playerLayerWithPlayer:player1];

playerLayer1.frame = self.bounds;

[self.layer addSublayer:playerLayer1];

参考:

MPMoviePlayerViewController

http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/mpmovieplayerviewcontroller_class/Reference/Reference.html

MPMoviePlayerController

http://developer.apple.com/library/ios/#documentation/mediaplayer/reference/MPMoviePlayerController_Class/Reference/Reference.html

音频播放

AVFoundation.framework

System Sound Services

AVAudioPlayer

MPMusicPlayerController

特点

播放短音效

播放任意长度音频

播放本地ipod音乐

版本

ios 2.0 or later

ios 2.2 or later

ios 3.0 later

属性

playing,duration,currentTime,

repeatMode,currentPlaybackTime,numberOfLoops

常用方法:

AudioServicesCreateSystemSoundID(CFURLRef                    inFileURL,                                  SystemSoundID     *outSystemSoundID)

AudioServicesPlaySystemSound(SystemSoundID inSystemSoundID)

- (id)initWithContentsOfURL:(NSURL *)url error:(NSError *)outError;

-  (id)initWithData:(NSData *)data error:(NSError *)outError;

-  (BOOL)play;

-  (void)pause;

- (void)stop;

applicationMusicPlayer;

-  (void)setQueueWithQuery:(MPMediaQuery *)query;

-(void)play;

-(void)pause;

-(void)stop;

一 各个播放器初始化方法:

1 System Sound Services

// 创建路径

NSString*dropMusicPath = [[NSBundle mainBundle] pathForResource:@"bird drop" ofType:@"wav"];

CFURLRefdropURL = (CFURLRef)[NSURL fileURLWithPath:dropMusicPath];

//创建系统声音

AudioServicesCreateSystemSoundID(dropURL, &birdDropID);

 

//播放音效

AudioServicesPlaySystemSound(birdDropID);

2 AVAudioPlayer

 

// 设置音乐文件路径

path = [[NSBundle mainBundle] pathForResource:@"InTheMood" ofType:@"mp3"];

// 设置 player    url为本地音频文件路径

player = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:path] error:&error];

在线播放用data初始化

player = [[AVAudioPlayer alloc] initWithData:receiveData error:&err];

[player play];

3 MPMusicPlayerController

 

 

player = [MPMusicPlayerController applicationMusicPlayer];

MPMediaItemCollection *_mediaCollection = [[MPMediaItemCollection alloc]initWithItems:SongList];

self.mediaCollection = _mediaCollection;

[_mediaCollection release];

[player setQueueWithItemCollection:mediaCollection];

[player setRepeatMode:MPMusicRepeatModeAll];

[player play];

二 音频后台播放:

(1) 设置 AVAudioSession 属性支持

NSError * err;

AVAudioSession*audioSession;

audioSession = [AVAudioSession sharedInstance];

[audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];

[audioSession setActive:YES error:nil];

(2)  设置工程文件plist属性

三 系统后台控制音频播放

(1)  重写方法 canBecomeFirstResponder 返回YES

- (BOOL)canBecomeFirstResponder

{

return YES;

}

- (void)viewDidLoad {

[super viewDidLoad];

[self canBecomeFirstResponder];

}

(2) 实现接收RemoteControlEvents方法

- (void)viewDidAppear:(BOOL)animated {

[super viewDidAppear:animated];

[self becomeFirstResponder];

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents];

}

- (void)viewWillDisappear:(BOOL)animated {

[super viewWillDisappear:animated];

[[UIApplication sharedApplication] endReceivingRemoteControlEvents];

[self resignFirstResponder];

}

(3)  在回调方法做相应处理

- (void) remoteControlReceivedWithEvent: (UIEvent *) receivedEvent {

if (receivedEvent.type == UIEventTypeRemoteControl)

{

switch (receivedEvent.subtype)

{

case UIEventSubtypeRemoteControlTogglePlayPause:

break;

case UIEventSubtypeRemoteControlPlay:

break;

case UIEventSubtypeRemoteControlPause:

break;

case UIEventSubtypeRemoteControlPreviousTrack:

break;

case UIEventSubtypeRemoteControlNextTrack:

break;

default:

break;

}

}

}

 

IOS音频视频的更多相关文章

  1. iOS 音频视频图像合成那点事

    代码地址如下:http://www.demodashi.com/demo/13420.html 人而无信不知其可 前言 很久很久没有写点什么了,只因为最近事情太多了,这几天终于闲下来了,趁此机会,记录 ...

  2. 《转》iOS音频视频初级开发

    代码改变世界 Posts - 73, Articles - 0, Comments - 1539 Cnblogs Dashboard Logout HOME CONTACT GALLERY RSS   ...

  3. ios音频视频资料--备用

    视频播放 MediaPlayer.framework MPMoviePlayerViewController VS MPMoviePlayerController MPMoviePlayerViewC ...

  4. iOS 音频视频制作

    --iOS多媒体 概览 随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制, ...

  5. iOS 音频/视频 学习目录

    参考 iOS原生API  音/视频录制 编辑 https://www.cnblogs.com/kenshincui/p/4186022.html#summary iOS视频编解码常用库比较 http: ...

  6. iOS 直播-获取音频(视频)数据

    iOS 直播-获取音频(视频)数据 // // ViewController.m // capture-test // // Created by caoxu on 16/6/3. // Copyri ...

  7. iOS音频AAC视频H264编码 推流最佳方案

    iOS音频AAC视频H264编码 推流最佳方案 项目都是个人的调研与实验,可能很多不好或者不对的地方请多包涵. 1    功能概况 *  实现音视频的数据的采集 *  实现音视频数据的编码,视频编码成 ...

  8. iOS 微信 音频 视频自动播放

    iOS 微信 音频 视频自动播放 http://www.w3ctech.com/topic/1165

  9. iOS WKWebView 退出后停止播放音频/视频

    带有<video>或者<audio>标签的H5网页在播放音频视频时,退出webview后不会自动停止播放,手动处理一下. 1.注入使网页停止音频.视频播放的JS代码(Swift ...

随机推荐

  1. 2.5 Hive中外部表的讲解

    一.外部表 1.hive中表的类型 管理表 托管表(外部表) #内部表 >内部表也称之为MANAGED_TABLE: >默认存储在/user/hive/warehouse下,也可以通过lo ...

  2. 【eclipse插件开发实战】 Eclipse插件开发5——时间插件Timer开发实例详解

    Eclipse插件开发5--时间插件Timer开发实例详解 这里做的TimeHelper插件设定为在菜单栏.工具栏提供快捷方式,需要在相应地方设置扩展点,最后弹出窗体显示时间. 在上一篇文章里创建好了 ...

  3. java集合框架之几种set(HashSet LinkedHashSet TreeSet )

    参考http://how2j.cn/k/collection/collection-sets/691.html#nowhere HashSet LinkedHashSet TreeSet HashSe ...

  4. 带着问题看 react-redux 源码实现

    前言 Redux作为通用的状态管理器,可以搭配任意界面框架.所以并搭配react使用的话就要借助redux官方提供的React绑定库react-redux,以高效灵活的在react中使用redux.下 ...

  5. angularjs 切换tab页的一个方法

    tab条的 css: .floor-tab-li { float: left; padding: 6px 12px; font-size: 14px; font-weight: normal; lin ...

  6. ACM心理过程

    ACM 都已经一个学期过去了,然后还是很菜很菜,比起别人真的是差到一种境界好么???因为自身的太多原因,因为学的不够精,因为懒惰,因为学习的方法,因为自身对未来的不渴望.因为自满,因为自己的自甘堕落, ...

  7. 阿里云物联网 .NET Core 客户端 | CZGL.AliIoTClient:8. 委托事件

    文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...

  8. CSS之html元素与body元素的范围

  9. ACM之路

    从10月我刚接触到acm竞赛,到现在2017年2.20接近4个月的时间,我才刷到200道题.在刷题的过程中,我曾遇到困难,我也从一次性就a过,但是有时候会想到放弃.不过既然已经踏进来一只脚,还不如就好 ...

  10. 排错:expected unqualified-id before string constant

    一个低级但是不好定位的编译错误,常见的问题是: 1. 语句的 { 括号不匹配. 2. 缺少 : , 特别是类的定义或声明,枚举的定义. 3. 变量名或函数名使用了保留字.