• iOS 6编程使用MPMoviePlayerController类实现视频播放器


    MPMoviePlayerController类可以播放多媒体文件,视频文件可以位于App文件系统中,或者远程URL处。下面是基于MPMoviePlayerController类实现的一个视频播放器App,项目名称 VideoPlayer。

    开发环境:Xcode 4.5 + iOS 6 iPhone 模拟器

    视频播放器VideoPlayer 运行界面:

    首先在项目中需要引入Media Player 框架,并在相应的类中添加接口文件的引用:

    #import <MediaPlayer/MediaPlayer.h>

    本示例项目全部源代码如下,代码中详细的注释。

    VideoPlayerViewController.h 头文件代码:

    #import <UIKit/UIKit.h>
    #import <MediaPlayer/MediaPlayer.h>

    @interface VideoPlayerViewController : UIViewController

    @property (strong, nonatomic) MPMoviePlayerController *moviePlayer;
    - (IBAction)playVideo:(id)sender;

    @end

    VideoPlayerViewController.m 实现文件代码:

    #import "VideoPlayerViewController.h"

    @interface VideoPlayerViewController ()

    @end

    @implementation VideoPlayerViewController

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 获取视频文件路径

    NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"Mr-Taxi" ofType:@"mp4"];
    // 设置视频播放器
    self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]];
    self.moviePlayer.allowsAirPlay = YES;
    [self.moviePlayer.view setFrame:CGRectMake(0, 0, 320, 380)];
    }

    - (void)didReceiveMemoryWarning
    {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    }

    - (IBAction)playVideo:(id)sender {
    // 将moviePlayer的视图添加到当前视图中
    [self.view addSubview:self.moviePlayer.view];
    // 播放完视频之后,MPMoviePlayerController 将发送
    // MPMoviePlayerPlaybackDidFinishNotification 消息
    // 登记该通知,接到该通知后,调用playVideoFinished:方法

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playVideoFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];

    [self.moviePlayer play];
    }

    - (void)playVideoFinished:(NSNotification *)theNotification{
    // 取消监听
    [[NSNotificationCenter defaultCenter]
    removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
    // 将视频视图从父视图中删除
    [self.moviePlayer.view removeFromSuperview];
    }
    @end

  • 相关阅读:
    C#:反射
    静态和非静态类
    数据的存入取出(注册机方式)
    退出unity运行
    网络流基础
    欧拉回路
    博弈论问题
    洛谷P5304 [GXOI/GZOI2019] 旅行者
    [ZJOI2006]物流运输
    POJ3278 Catch that cow
  • 原文地址:https://www.cnblogs.com/tuncaysanli/p/2727994.html
Copyright © 2020-2023  润新知