這個東西和以前的音頻播放差很少, 也是先須要導入系統框架MediaPlayer.framework 才能使用到MPMoviePlayerController 的文件中導入相應的頭文件windows
初始化:這裏就有些不同了MPMoviePlayerController是能夠經過遠程url初始化的, 例如:app
MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc]initWithContentURL:[NSURL urlWithString:@"URL"] ];
接下來是控制器樣式設置:框架
moviePlayer.moviewControlMode = MPMovieControlModeHidden;
可使用下列樣式:
MPMovieControlModeDefault 顯示播放/暫停、音量和時間控制
MPMovieControlModeVolumeOnly 只顯示音量控制
MPMovieControlModeHidden 沒有控制器url
一般狀況下, 咱們通常都是本身來定義視頻播放器, 因此大多數仍是選擇最後沒有控制器的那個..net
屏幕寬高比例:視頻
moviePlayer.scallingMode = MPMovieScallingModeNone;
MPMovieScallingModeNone 不作任何縮放
MPMovieScallingModeAspectFit 適應屏幕大小,保持寬高比
MPMovieScallingModeAspectFill 適應屏幕大小,保持寬高比,可裁剪
MPMovieScallingModeFill 充滿屏幕,不保持寬高比server
[ moviePlayer play ]; // 開始播放 [ moviePlayer stop ]; // 中止播放
註冊一個通知 你的程序能夠配置電影播放器在什麼時候候發送通知,包括結束加載內容、技術播放、改變寬高比等。電影播放器會將事件發送到 Cocoa 的通知中心,你能夠對其進行配置,指定將這些事件轉發到你的應用程序的一個對象。要接收這些通知,須要使用 NSNotificationCenter 類,爲電影播放器添加一個觀察者(observer):對象
NSNotificationCenter* notificationCenter = [NSNotificationCenter defaultCenter]; [ notificationCenter addObserver:self selector:@selector(moviePlayerPreloadFinish:) name:MPMoviePlayerContentPreloadDidFinishNotification object:moviePlayer ]; // 通知會發到你指定的委託類和目標方法。通知參數讓你能夠知道是哪一個事件觸發了委託方法: -(void)moviePlayerPreloadDidFinish:(NSNotification*)notification{ //添加你的處理代碼 }
你會觀察到如下通知:
MPMoviePlayerContentPreloadDidFinishNotification
當電影播放器結束對內容的預加載後發出。由於內容能夠在僅加載了一部分的狀況下播放,因此這個通知可能在已經播放後才發出。
MPMoviePlayerScallingModeDidChangedNotification
當用戶改變了電影的縮放模式後發出。用戶能夠點觸縮放圖標,在全屏播放和窗口播放之間切換。
MPMoviePlayerPlaybackDidFinishNotification
當電影播放完畢或者用戶按下了Done按鈕後發出。blog
這裏有個小實例:自定義視頻之用手勢來控制視頻進度和音量大小事件
#import <MediaPlayer/MediaPlayer.h> @interface KKBMoviePlayerController : MPMoviePlayerController<UIGestureRecognizerDelegate> @end
#import "KKBMoviePlayerController.h" #import "AppDelegate.h" @interface KKBMoviePlayerController(){ BOOL _inFullScreen; UIPanGestureRecognizer *_pan; CGPoint _lastPoint; BOOL _startChange; BOOL _changeVolume; } @end @implementation KKBMoviePlayerController - (id)initWithContentURL:(NSURL *)url{ self =[super initWithContentURL:url]; if (self) { self.view.backgroundColor = [UIColor clearColor]; self.initialPlaybackTime = -1; self.endPlaybackTime = -1; [self prepareToPlay]; [self play]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enterFullScreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(leaveFullScreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil]; } return self; } #pragma mark - full screen controller - (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{ return YES; } - (void)handlePan:(UIPanGestureRecognizer*)rec{ if (_inFullScreen) { if (rec.state == UIGestureRecognizerStateBegan) { _lastPoint = [rec locationInView:self.view]; } else if (rec.state == UIGestureRecognizerStateChanged) { CGPoint nowPoint = [rec locationInView:self.view]; if (_startChange == NO) { if (fabs(nowPoint.y - _lastPoint.y) > fabs(nowPoint.x - _lastPoint.x)) { _changeVolume = NO; } else { _changeVolume = YES; } _startChange = YES; } else { if (_changeVolume) { //change volume float volume = [[MPMusicPlayerController applicationMusicPlayer] volume]; float newVolume = volume; if (nowPoint.x == _lastPoint.x) { } else { if (nowPoint.x < _lastPoint.x) { newVolume += 0.01; } else { newVolume -= 0.01; } } if (newVolume < 0) { newVolume = 0; } else if (newVolume > 1.0) { newVolume = 1.0; } [[MPMusicPlayerController applicationMusicPlayer] setVolume:newVolume]; } else { //change playback state if (self.playbackState != MPMoviePlaybackStateSeekingForward && self.playbackState != MPMoviePlaybackStateSeekingBackward) { if (nowPoint.y == _lastPoint.y) { } else { if (nowPoint.y < _lastPoint.y) { [self beginSeekingForward]; } else { [self beginSeekingBackward]; } } _lastPoint = nowPoint; } } } } else if (rec.state == UIGestureRecognizerStateCancelled || rec.state == UIGestureRecognizerStateEnded || rec.state == UIGestureRecognizerStateFailed){ _startChange = NO; [self endSeeking]; } } } - (void)enterFullScreen:(NSNotification*)notification{ _inFullScreen = YES; _pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handlePan:)]; _pan.delegate = self; [[[[UIApplication sharedApplication] windows] objectAtIndex:0] addGestureRecognizer:_pan]; } - (void)leaveFullScreen:(NSNotification*)notification{ _inFullScreen = NO; [[[[UIApplication sharedApplication] windows] objectAtIndex:0] removeGestureRecognizer:_pan]; } @end
原文: http://blog.csdn.net/u013561113/article/details/21457903