分享

iOS 视频开发

 叹落花 2015-06-24

MediaPlayer framework

播放电影文件:

    iOS sdk中可以使用MPMoviePlayerController来播放电影文件。但是在iOS设备上播放电影文件有严格的格式要求,只能播放下面两个格式的电影文件。

· H.264 (Baseline Profile Level 3.0)
· MPEG-4 Part 2 video (Simple Profile)

幸运的是你可以先使用iTunes将文件转换成上面两个格式。
MPMoviePlayerController还可以播放互联网上的视频文件。但是建议你先将视频文件下载到本地,然后播放。如果你不这样做,iOS可能会拒绝播放很大的视频文件。

这个类定义在MediaPlayer framework中。在你的应用程序中,先添加这个引用,然后修改MediaPlayerViewController.h文件。

#import <</span>MediaPlayer/MediaPlayer.h>
@interface MediaPlayerViewController : UIViewController<</span>AVAudioPlayerDelegate>
{
MPMoviePlayerController *moviePlayer;

下面我们使用这个类来播放一个.m4v 格式的视频文件。与前面的类似,需要一个url路径。

复制代码
- (id)init
{
self = [super initWithNibName:@"MediaPlayerViewController" bundle:nil];
if (self) {

NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Layers" 
ofType:@"m4v"];
if (moviePath) {
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
moviePlayer = [[MPMoviePlayerController alloc] 
initWithContentURL:movieURL];
}
复制代码

MPMoviePlayerController有一个视图来展示播放器控件,我们在viewDidLoad方法中,将这个播放器展示出来。

复制代码
- (void)viewDidLoad
{
[[self view] addSubview:[moviePlayer view]];
float halfHeight = [[self view] bounds].size.height / 2.0;
float width = [[self view] bounds].size.width;
[[moviePlayer view] setFrame:CGRectMake(0, halfHeight, width, halfHeight)];
}
复制代码

还有一个MPMoviePlayerViewController类,用于全屏播放视频文件,用法和MPMoviePlayerController一样。

MPMoviePlayerViewController *playerViewController =
[[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
[viewController presentMoviePlayerViewControllerAnimated:playerViewController];

   我们在听音乐的时候,可以用iphone做其他的事情,这个时候需要播放器在后台也能运行,我们只需要在应用程序中做个简单的设置就行了。

1、在Info property list中加一个 Required background modes节点,它是一个数组,将第一项设置成设置App plays audio。

2、在播放mp3的代码中加入下面代码:

复制代码
if (musicPath) {
NSURL *musicURL = [NSURL fileURLWithPath:musicPath];
[[AVAudioSession sharedInstance]
setCategory:AVAudioSessionCategoryPlayback error:nil];
audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:musicURL
error:nil];
[audioPlayer setDelegate:self];
}
复制代码

在后台运行的播放音乐的功能在模拟器中看不出来,只有在真机上看效果。

原文来自http://blog.sina.com.cn/s/blog_74e9d98d0101ar8e.html感谢博主的分享

相关连接:http://blog.sina.com.cn/s/blog_6f1a34260100tlvx.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多