
效果如下:
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>