MediaPlayer

IOS系統的四層結構:核心操做系統層:Core OS,核心服務層:Core Services,媒體層:Media,Cocoa Touch層。框架

使用MediaPlayer:spa

1.引入MediaPlayer.frame框架,引入頭文件#import <MediaPlayer/MediaPlayer.h>操作系統

2.經過設置MPMoviePlayerViewController來設置全屏的播放:code

  MPMoviePlayerController *_moviePlayer;視頻

  MPMoviePlayerViewController *_mpMovieViewController;server

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"moxue" ofType:@"mp4"];//視頻文件路徑
    _mpMovieViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];//將路徑添加到MPMoviePlayerViewController
    [_mpMovieViewController.moviePlayer play];//設置moviePlayer播放
    [self.view addSubview:_mpMovieViewController.view];//將MPMoviePlayerViewController添加到view上。

3.經過設置MPMoviePlayerController在view上設置播放屏幕的大小:blog

NSString *filePath = [[NSBundle mainBundle]pathForResource:@"moxue" ofType:@"mp4"];//視頻文件路徑
    _mpMovieViewController = [[MPMoviePlayerViewController alloc]initWithContentURL:[NSURL fileURLWithPath:filePath]];//將路徑添加到MPMoviePlayerViewController
 _moviePlayer = [_mpMovieViewController moviePlayer];//設置播放器
    _moviePlayer.view.frame = CGRectMake(50, 100, 300, 200);//設置播放器大小
   _moviePlayer.view.backgroundColor = [UIColor redColor];//設置背景顏色
    [_moviePlayer play];//播放
    [self.view addSubview:_moviePlayer.view];//添加到視圖
    
    _moviePlayer.controlStyle = MPMovieControlStyleEmbedded;//控件選擇
    _moviePlayer.scalingMode = MPMovieScalingModeAspectFit;//屏幕縮放

4.設置播放器的一些監聽方法:it

 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationObserver:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];//添加進入全屏的監聽
}
-(void)notificationObserver:(NSNotification *)info
{
    NSLog(@"%@",info);
}
相關文章
相關標籤/搜索