iOS用MPMoviePlayerViewController 播放MP4视频

2021-10-29 10:36:30 浏览数 (1)

1.新建single view工程,导入MediaPlayer库,导入一个视频文件test1.mp4

2.ViewController.h

代码语言:javascript复制
    #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> 

0 人点赞