zoukankan      html  css  js  c++  java
  • iOS 视频开发

    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

  • 相关阅读:
    Myflight航班查询系统
    《java语法实例2~15章》
    第九章
    第八章
    第六章
    第五章
    第四章
    php_mvc实现步骤十
    php_mvc实现步骤九(登录验证码,退出-登录标记)
    php_mvc实现步骤八
  • 原文地址:https://www.cnblogs.com/longyun/p/3700955.html
Copyright © 2011-2022 走看看