效果如下:

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 框架播放视频的更多相关文章

  1. [Xcode 实际操作]六、媒体与动画-(17)使用MediaPlayer框架播放视频

    目录:[Swift]Xcode实际操作 本文将演示视频的播放功能. 在项目名称上点击鼠标右键,弹出右键菜单, 选择[Add Files to "DemoApp"],往项目中导入文件 ...

  2. Android MediaPlayer和SurfaceView播放视频

    昨天介绍了VideoView播放视频,今天再介绍一种播放视频的方法MediaPlayer和SurfaceView,MediaPlayer播放音频,SurfaceView来显示图像,具体步骤如下: 1. ...

  3. 使用MediaPlayer和SurfaceView播放视频

    使用VideoView播放视频简单.方便,丹有些早期的开发者更喜欢使用MediaPlayer来播放视频,但由于MediaPlayer主要用于播放音频,因此它没有提供图像输出界面,此时 需要借助于Sur ...

  4. android中使用MediaPlayer和SurfaceView播放视频

    package com.test.video; import java.io.IOException; import android.media.AudioManager; import androi ...

  5. 运用surfaceView与MediaPlayer实现播放视频的功能

    该程序运用了surfaceView与MediaPlayer结合,实现播放视频,surfaceView详情请见 SurfaceView的使用 使用了第三方包Volly里面的方法StringQueue下载 ...

  6. android中使用surfaceview+MediaPlayer播放视频

    Android中播放视频主要有两种方式: 使用其自带的播放器.指定Action为ACTION_VIEW,Data为Uri,Type为其MIME类型 使用android自带的VideoView,这种方法 ...

  7. 使用MediaPlayer类和SurfaceView来播放视频

    MediaPlayer可以播放视频,只需需要SurfaceView的配合,SurfaceView主要用于显示MediaPlayer播放的视频流媒体的画面渲染. SurfaceView是配合MediaP ...

  8. Android开发 MediaPlayer入门_播放本地视频

    前言 MediaPlayer,可以播放视频/音频,并且它支持本地和网络文件的播放.本片博客作为入门教程,先以最通俗的方式解释播放文件本地视频.(如果你嫌MediaPlayer还是太麻烦可以试试选择Vi ...

  9. iOS中 MediaPlayer framework实现视频播放 韩俊强的博客

    iOS开发中播放音乐可以使用MPMusicPlayerController类来实现,播放视频可以使用MPMoviePlayerController和MPMoviePlayerViewControlle ...

随机推荐

  1. JD 题目1040:Prime Number (筛法求素数)

    OJ题目:click here~~ 题目分析:输出第k个素数 贴这么简单的题目,目的不清纯 用筛法求素数的基本思想是:把从1開始的.某一范围内的正整数从小到大顺序排列, 1不是素数,首先把它筛掉.剩下 ...

  2. Creating a Physical Standby Database 11g

    1.Environment Item Primary database standby database Platform Redhat 5.4 Redhat 5.4 Hostname gc1 gc2 ...

  3. 使用WPF Application Framework (WAF)框架

    Visual Studio新建WAF项目的模板:https://marketplace.visualstudio.com/items?itemName=jbe2277.WAFProjectTempla ...

  4. AJAX 简单例程示例

    index.html <html> <head> <script> function showHint(str) { if (str.length==0) { re ...

  5. android开发(29) 自定义曲线,可拖动,无限加载

    项目需要 做一个曲线,该曲线的数据时不断加载的.如下图,当不断向左拖动时,图形曲线要随着拖动移动,并在拖动到边界时需要加载更多数据. 先看步骤: 1.在Activity里放一个surfaceView ...

  6. 放弃winform的窗体吧,改用html作界面,桌面应用程序UI的新的开发方式。

    做过很多winform项目,都为winform控件头疼不已.想实现一些漂亮的样子总是很难.我这里列举几个缺点: 1.winform控件大多是 绝对布局 ,你需要给出准确的坐标.那么在实现居中效果就会很 ...

  7. python多线程生产消费

    #!/usr/bin/env python# -*- coding: utf-8 -*- from threading import Threadfrom Queue import Queueimpo ...

  8. mysql ACID与四种隔离级别归纳总结

    关于数据库的ACID特性已经有很多的介绍,这里再重新归纳总结一下:   A(atomicity)原子性: 即事务要么全部做完,要么全部不做,不会出现只做一部分的情形,如A给B转帐,不会出现A的钱少了, ...

  9. Excel相同内容如何设置相同的背景色

    有这样一个需求就是实现EXCEL的相同内容的背景色相同.并且内容不同的时候达到隔行变色的效果,记录下实现的效果,如果大家有什么更好的办法请给我指点一下.具体操作如下: 首先将是比较的列"20 ...

  10. 前端最全的 API 集锦

    window.getComputedStyle(el,':after')[attrName]------------------------------------------------------ ...