本範例為點擊按鈕播放影片,步驟如下
1. 建立專案後加入MediaPlayer.framework
2. storyboard加入一顆按鈕來觸發影片播放
3. 將要播放的影片copy進專案中
4. 建立影片與程式的關聯性, 再建立一個player來播放影片, 接著代入播放語法即完成
程式碼如下:
ViewController.m
#import "ViewController.h"
#import <MediaPlayer/MediaPlayer.h>
@interface ViewController ()
- (IBAction)play:(id)sender;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)play:(id)sender {
NSURL *videoURL =[NSURL fileURLWithPath:[[NSBundle mainBundle]
pathForResource:@"sample_iTunes" ofType:@"mov"]];
MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc]initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:moviePlayer];
}
@end
文章標籤
全站熱搜
留言列表