183使用 MediaPlayer Framework 框架播放视频
效果如下:

ViewController.h
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h> @interface ViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIButton *btnPlayMovie;
@property (strong, nonatomic) MPMoviePlayerController *moviePlayerController; @end
ViewController.m
#import "ViewController.h" @interface ViewController ()
- (void)layoutUI;
@end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad]; [self layoutUI];
} - (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
} - (void)layoutUI { } - (IBAction)playMovie:(id)sender {
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Big-Buck-Bunny-Clip"
ofType:@"m4v"];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
//导入<MediaPlayer/MediaPlayer.h>包,必须在头文件声明对应的全局引用的电影播放器控制器对象示例属性,否则会出现黑屏无法播放,因为对象被回收了
//实现电影播放器控制器对象示例_moviePlayerController
_moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
_moviePlayerController.view.frame = CGRectMake(,
_btnPlayMovie.frame.origin.y,
self.view.frame.size.width,
_btnPlayMovie.frame.size.height); //从通知中心,添加电影播放器控制器对象示例(播放完成返回状态)的观察者
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(playMovieComplete:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:_moviePlayerController];
[self.view addSubview:_moviePlayerController.view];
[_moviePlayerController play];
} - (void)playMovieComplete:(NSNotification *)notification {
MPMoviePlayerController *moviePlayController = [notification object];
//从通知中心,移除电影播放器控制器对象示例(播放完成返回状态)的观察者
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayController];
//从它的父级视图,移除电影播放器控制器对象示例的视图
[moviePlayController.view removeFromSuperview];
} @end
Main.storyboard
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="7702" systemVersion="14D136" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" initialViewController="vXZ-lx-hvc">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/>
</dependencies>
<scenes>
<!--View Controller-->
<scene sceneID="ufC-wZ-h7g">
<objects>
<viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/>
<viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/>
</layoutGuides>
<view key="view" contentMode="scaleToFill" id="kh9-bI-dsS">
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="POH-zG-xLO">
<rect key="frame" x="140" y="185" width="320" height="230"/>
<state key="normal" image="Big-Buck-Bunny-Clip">
<color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/>
</state>
<connections>
<action selector="playMovie:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="RTL-0Q-0nR"/>
</connections>
</button>
</subviews>
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
<constraints>
<constraint firstItem="POH-zG-xLO" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="DhU-rM-rsz"/>
<constraint firstAttribute="width" secondItem="POH-zG-xLO" secondAttribute="width" id="Mzy-Q3-6CL"/>
<constraint firstItem="POH-zG-xLO" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" id="OPN-No-jC2"/>
</constraints>
<variation key="default">
<mask key="constraints">
<exclude reference="Mzy-Q3-6CL"/>
</mask>
</variation>
</view>
<connections>
<outlet property="btnPlayMovie" destination="POH-zG-xLO" id="kEF-6Y-YOE"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/>
</objects>
</scene>
</scenes>
<resources>
<image name="Big-Buck-Bunny-Clip" width="320" height="230"/>
</resources>
</document>
183使用 MediaPlayer Framework 框架播放视频的更多相关文章
- [Xcode 实际操作]六、媒体与动画-(17)使用MediaPlayer框架播放视频
		目录:[Swift]Xcode实际操作 本文将演示视频的播放功能. 在项目名称上点击鼠标右键,弹出右键菜单, 选择[Add Files to "DemoApp"],往项目中导入文件 ... 
- Android MediaPlayer和SurfaceView播放视频
		昨天介绍了VideoView播放视频,今天再介绍一种播放视频的方法MediaPlayer和SurfaceView,MediaPlayer播放音频,SurfaceView来显示图像,具体步骤如下: 1. ... 
- 使用MediaPlayer和SurfaceView播放视频
		使用VideoView播放视频简单.方便,丹有些早期的开发者更喜欢使用MediaPlayer来播放视频,但由于MediaPlayer主要用于播放音频,因此它没有提供图像输出界面,此时 需要借助于Sur ... 
- android中使用MediaPlayer和SurfaceView播放视频
		package com.test.video; import java.io.IOException; import android.media.AudioManager; import androi ... 
- 运用surfaceView与MediaPlayer实现播放视频的功能
		该程序运用了surfaceView与MediaPlayer结合,实现播放视频,surfaceView详情请见 SurfaceView的使用 使用了第三方包Volly里面的方法StringQueue下载 ... 
- android中使用surfaceview+MediaPlayer播放视频
		Android中播放视频主要有两种方式: 使用其自带的播放器.指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型 使用android自带的VideoView,这种方法 ... 
- 使用MediaPlayer类和SurfaceView来播放视频
		MediaPlayer可以播放视频,只需需要SurfaceView的配合,SurfaceView主要用于显示MediaPlayer播放的视频流媒体的画面渲染. SurfaceView是配合MediaP ... 
- Android开发 MediaPlayer入门_播放本地视频
		前言 MediaPlayer,可以播放视频/音频,并且它支持本地和网络文件的播放.本片博客作为入门教程,先以最通俗的方式解释播放文件本地视频.(如果你嫌MediaPlayer还是太麻烦可以试试选择Vi ... 
- iOS中 MediaPlayer framework实现视频播放 韩俊强的博客
		iOS开发中播放音乐可以使用MPMusicPlayerController类来实现,播放视频可以使用MPMoviePlayerController和MPMoviePlayerViewControlle ... 
随机推荐
- C#学习笔记(21)——C#获取文件夹下的所有文件的文件名
			说明(2017-7-30 23:11:59): 1. 文件处理老是忘,学的不扎实. 2. 路径用Directory,文件名用file. 3. 我也推荐用第二种方法,可以拿到文件的所有信息,比如扩展名什 ... 
- C#学习笔记(13)——传统方法读取XML
			说明(2017-7-12 16:32:47): 1. LINQ的方法虽然很简单,但是有点不好理解,sql的写法总感觉很诡异,而且LINQ的本质也是用的传统方法,所以还是采用老办法吧! 2. 注释里的代 ... 
- 4. 文本相似度计算-CNN-DSSM算法
			1. 文本相似度计算-文本向量化 2. 文本相似度计算-距离的度量 3. 文本相似度计算-DSSM算法 4. 文本相似度计算-CNN-DSSM算法 1. 前言 之前介绍了DSSM算法,它主要是用了DN ... 
- ISO8601时间格式
			格式解析 R2/2015-06-04T19:25:16.828696-07:00/P1DT10S 上面的字符串通过"/"分为了三部分即: 重复次数/开始时间/运行间隔 重复次数 R ... 
- 微服务之springCloud和docker-provide(二)
			简介 通过上一节我们已经通过docker-compose在swarm中部署了有三个实例的高可用eureka服务注册中心,本节我们讨论一下,怎么在创建一个spring cloud生产者,并用docker ... 
- 封装篇——图片模块(Glide)
			如今市面上差点儿全部的app都用到了图片,图片模块的开发是app开发中不可缺少的一块工作, 开源的力量是强大的.好多优秀的第三方项目能够任君使用,帮助我们提高效率.而且不须要反复造轮子,这边我採用的是 ... 
- CAD技巧之001——如何将单个字合并起来
			CAD技巧之001——如何将单个字合并起来 很多同志如果遇到奇葩的单个文字合并,怎么办,重新书写?复制粘贴?如果工作量很大,怎么办呢. 今天九天就教您一个好方法! 废话不多说,上动画片 
- 最美应用API接口分析
			最美应用API接口分析html, body {overflow-x: initial !important;}.CodeMirror { height: auto; } .CodeMirror-scr ... 
- C语言  ·  新生舞会
			算法训练 新生舞会 时间限制:1.0s 内存限制:512.0MB 问题描述 新生舞会开始了.n名新生每人有三个属性:姓名.学号.性别.其中,姓名用长度不超过20的仅由大小写字母构成的 ... 
- openwrt MT7620 固件编译记录
			下载,安装相关软件 git clone git@github.com:openwrt-mirror/openwrt.git sudo apt-get install gcc g++ binutils ... 
