ios 视频播放器MPMoviePlayerController

这个东西和之前的音频播放差不多, 也是先需要导入系统框架MediaPlayer.framework 才能使用到MPMoviePlayerController 的文件中导入相应的头文件

初始化:这里就有些不一样了MPMoviePlayerController是可以通过远程url初始化的, 例如:

MPMoviePlayerController *moviePlayer = [ [ MPMoviePlayerController alloc]initWithContentURL:[NSURL urlWithString:@"URL"] ];

接下来是控制器样式设置:

moviePlayer.moviewControlMode = MPMovieControlModeHidden;  

  

可以使用下列样式:
MPMovieControlModeDefault            显示播放/暂停、音量和时间控制
MPMovieControlModeVolumeOnly         只显示音量控制
MPMovieControlModeHidden             没有控制器

通常情况下, 我们一般都是自己来定义视频播放器, 所以大多数还是选择最后没有控制器的那个.

屏幕宽高比例:

moviePlayer.scallingMode = MPMovieScallingModeNone;

MPMovieScallingModeNone            不做任何缩放
MPMovieScallingModeAspectFit       适应屏幕大小,保持宽高比
MPMovieScallingModeAspectFill      适应屏幕大小,保持宽高比,可裁剪
MPMovieScallingModeFill            充满屏幕,不保持宽高比

[ 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按钮后发出。

这里有个小实例:自定义视频之用手势来控制视频进度和音量大小

 

#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

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。