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 ...
随机推荐
- Docker 容器更新,打包,上传到阿里云
上几章讲了镜像的拉取和运行. 这次来尝试如何将已经运行的容器打包,封装成镜像,并且上传到阿里云上,为了别的地方下载和使用更加的方便. 首先,进入镜像,如果不清楚地,可以看下上一章. [root@VM_ ...
- for语句查看js对象
for (var obj in o.curform) { document.write("<p>" + obj + " = " + o.curfor ...
- hbase源码系列(十二)Get、Scan在服务端是如何处理?
继上一篇讲了Put和Delete之后,这一篇我们讲Get和Scan, 因为我发现这两个操作几乎是一样的过程,就像之前的Put和Delete一样,上一篇我本来只打算写Put的,结果发现Delete也可以 ...
- 【基础练习】【区间DP】codevs1090 加分二叉树题解
2003 NOIP TG 题目描写叙述 Description 设一个n个节点的二叉树tree的中序遍历为(l,2,3,-,n),当中数字1,2,3,-,n为节点编号.每一个节点都有一个分数(均为正整 ...
- 【C】——const和volatile可以并用吗?
答案是肯定的,可以一起用. 因为很多人误解了const的真正含义,很多初学者认为const修饰的就是常量,而常量不会改变,而既然不会改变,那volatile就没有意义. 但是实际上这正是对const的 ...
- PCL库简要说明
PCL(PointCloudLibrary)是在吸收了前人点云相关研究基础上建立起来的大型跨平台开源C++编程库,它实现了大量点云相关的通用算法和高效数据结构,涉及到点云获取.滤波.分割.配准.检索. ...
- CentOS 7系统LAMP配置PHP-FPM的示例
CentOS 7 系统出来有一段时间了,今天我们来看在vps中的 CentOS 7 中给lamp环境配置php-fpm的过程,希望文章可以帮助到各位. CentOS 7 已经发布,并且采用 Apach ...
- 微信小程序 --- e.currentTarget.dataset.id 获取不到值
直接代码 wxml代码片段 <view class='ranksList' wx:for="{{ranksLb}}"> <view class='ranksLis ...
- node学习笔记6——自定义模块
自定义模块三大关键词: require——引入模块: exports——单个输出: module——批量输出. 从例子下手: 1.创建module.js: exports.a=22; exports. ...
- 给初学者的20个CSS实用建议
英文原文:20-useful-css-tips-for-beginners,编译:杨礼鑫 过去就连一个镜像站点,我们都依靠大量的开发人员和程序员进行维护.得益于CSS和它的灵活性使得样式能够从代码中被 ...