标签:
效果如下:
ViewController.h
1 #import <UIKit/UIKit.h> 2 #import <MediaPlayer/MediaPlayer.h> 3 4 @interface ViewController : UIViewController 5 @property (strong, nonatomic) IBOutlet UIButton *btnPlayMovie; 6 @property (strong, nonatomic) MPMoviePlayerController *moviePlayerController; 7 8 @end
ViewController.m
1 #import "ViewController.h" 2 3 @interface ViewController () 4 - (void)layoutUI; 5 @end 6 7 @implementation ViewController 8 9 - (void)viewDidLoad { 10 [super viewDidLoad]; 11 12 [self layoutUI]; 13 } 14 15 - (void)didReceiveMemoryWarning { 16 [super didReceiveMemoryWarning]; 17 // Dispose of any resources that can be recreated. 18 } 19 20 - (void)layoutUI { 21 22 } 23 24 - (IBAction)playMovie:(id)sender { 25 NSString *filePath = [[NSBundle mainBundle] pathForResource:@"Big-Buck-Bunny-Clip" 26 ofType:@"m4v"]; 27 NSURL *fileURL = [NSURL fileURLWithPath:filePath]; 28 //导入<MediaPlayer/MediaPlayer.h>包,必须在头文件声明对应的全局引用的电影播放器控制器对象示例属性,否则会出现黑屏无法播放,因为对象被回收了 29 //实现电影播放器控制器对象示例_moviePlayerController 30 _moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL]; 31 _moviePlayerController.view.frame = CGRectMake(0, 32 _btnPlayMovie.frame.origin.y, 33 self.view.frame.size.width, 34 _btnPlayMovie.frame.size.height); 35 36 //从通知中心,添加电影播放器控制器对象示例(播放完成返回状态)的观察者 37 [[NSNotificationCenter defaultCenter] addObserver:self 38 selector:@selector(playMovieComplete:) 39 name:MPMoviePlayerPlaybackDidFinishNotification 40 object:_moviePlayerController]; 41 [self.view addSubview:_moviePlayerController.view]; 42 [_moviePlayerController play]; 43 } 44 45 - (void)playMovieComplete:(NSNotification *)notification { 46 MPMoviePlayerController *moviePlayController = [notification object]; 47 //从通知中心,移除电影播放器控制器对象示例(播放完成返回状态)的观察者 48 [[NSNotificationCenter defaultCenter] removeObserver:self 49 name:MPMoviePlayerPlaybackDidFinishNotification 50 object:moviePlayController]; 51 //从它的父级视图,移除电影播放器控制器对象示例的视图 52 [moviePlayController.view removeFromSuperview]; 53 } 54 55 @end
Main.storyboard
1 <?xml version="1.0" encoding="UTF-8" standalone="no"?> 2 <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"> 3 <dependencies> 4 <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="7701"/> 5 </dependencies> 6 <scenes> 7 <!--View Controller--> 8 <scene sceneID="ufC-wZ-h7g"> 9 <objects> 10 <viewController id="vXZ-lx-hvc" customClass="ViewController" sceneMemberID="viewController"> 11 <layoutGuides> 12 <viewControllerLayoutGuide type="top" id="jyV-Pf-zRb"/> 13 <viewControllerLayoutGuide type="bottom" id="2fi-mo-0CV"/> 14 </layoutGuides> 15 <view key="view" contentMode="scaleToFill" id="kh9-bI-dsS"> 16 <rect key="frame" x="0.0" y="0.0" width="600" height="600"/> 17 <autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/> 18 <subviews> 19 <button opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="POH-zG-xLO"> 20 <rect key="frame" x="140" y="185" width="320" height="230"/> 21 <state key="normal" image="Big-Buck-Bunny-Clip"> 22 <color key="titleShadowColor" white="0.5" alpha="1" colorSpace="calibratedWhite"/> 23 </state> 24 <connections> 25 <action selector="playMovie:" destination="vXZ-lx-hvc" eventType="touchUpInside" id="RTL-0Q-0nR"/> 26 </connections> 27 </button> 28 </subviews> 29 <color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/> 30 <constraints> 31 <constraint firstItem="POH-zG-xLO" firstAttribute="centerX" secondItem="kh9-bI-dsS" secondAttribute="centerX" id="DhU-rM-rsz"/> 32 <constraint firstAttribute="width" secondItem="POH-zG-xLO" secondAttribute="width" id="Mzy-Q3-6CL"/> 33 <constraint firstItem="POH-zG-xLO" firstAttribute="centerY" secondItem="kh9-bI-dsS" secondAttribute="centerY" id="OPN-No-jC2"/> 34 </constraints> 35 <variation key="default"> 36 <mask key="constraints"> 37 <exclude reference="Mzy-Q3-6CL"/> 38 </mask> 39 </variation> 40 </view> 41 <connections> 42 <outlet property="btnPlayMovie" destination="POH-zG-xLO" id="kEF-6Y-YOE"/> 43 </connections> 44 </viewController> 45 <placeholder placeholderIdentifier="IBFirstResponder" id="x5A-6p-PRh" sceneMemberID="firstResponder"/> 46 </objects> 47 </scene> 48 </scenes> 49 <resources> 50 <image name="Big-Buck-Bunny-Clip" width="320" height="230"/> 51 </resources> 52 </document>
183使用 MediaPlayer Framework 框架播放视频
标签:
原文地址:http://www.cnblogs.com/huangjianwu/p/4584049.html