iOS用MPMoviePlayerViewController 播放MP4視頻

ccwv 9年前發布 | 2K 次閱讀 Objective-C IOS

1.新建single view工程,導入MediaPlayer庫,導入一個視頻文件test1.mp4

2.ViewController.h

    #import <UIKit/UIKit.h>

#import <MediaPlayer/MediaPlayer.h>  

@interface ViewController : UIViewController {  
    MPMoviePlayerViewController *_playerVC;  
}  
@end  </pre> 


2.ViewController.m

    #import "ViewController.h"

@implementation ViewController  

- (void)viewDidLoad  
{  
    [super viewDidLoad];  

    NSString *path = [[NSBundle mainBundle] pathForResource:@"test1" ofType:@"mp4"];  
    NSLog(@"%@", path);  
    NSURL *url = [NSURL fileURLWithPath:path];  
    _playerVC = [[MPMoviePlayerViewController alloc] initWithContentURL:url];  

    UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];  
    button.frame = CGRectMake(100, 100, 100, 40);  
    [button addTarget:self action:@selector(play) forControlEvents:UIControlEventTouchUpInside];  
    [self.view addSubview:button];  
}  

- (void)play  
{  
    [self presentMoviePlayerViewControllerAnimated:_playerVC];  
    [_playerVC.moviePlayer play];  
}  

@end  </pre> 


 本文由用戶 ccwv 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!