MPMoviePlayerController  …

Iphone中已经自定义好了视频播放类 MPMoviePlayerController,我们只需调用既可,我自己封装好了一个视频播放类,下面就直接给大家上代码吧。


#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>          //导入视频播放库

@interface CustomMoviePlayerViewCon troller : UIViewController
{
      MPMoviePlayerController *mp;
      NSURL *movieURL;                                    //视频地址
      UIActivityIndicatorView *loadingAni;      //加载动画
      UILabel *label;                                          //加载提醒
}
@property (nonatomic,retain) NSURL *movieURL;

//准备播放
- (void)readyPlayer;

@end



#import "CustomMoviePlayerViewCon troller.h"

#pragma mark -
#pragma mark Compiler Directives & Static Variables

@implementation CustomMoviePlayerViewCon troller

@synthesize movieURL;

- (void)viewDidLoad
{
      [super viewDidLoad];
     
      loadingAni = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(140, 150, 37, 37)];
      loadingAni.activityIndicatorViewSty le = UIActivityIndicatorViewS tyleWhiteLarge;     
      [self.view addSubview:loadingAni];
     
      label = [[UILabel alloc] initWithFrame:CGRectMake(130, 190, 80, 40)];
      label.text = @"加载中...";
      label.textColor = [UIColor whiteColor];
      label.backgroundColor = [UIColor clearColor];
     
      [[self view] setBackgroundColor:[UIColor blackColor]];
     
      [loadingAni startAnimating];
      [self.view addSubview:label];
}


- (void) moviePlayerLoadStateChan ged:(NSNotification*)notification
{
      [loadingAni stopAnimating];
      [label removeFromSuperview];
     
      // Unless state is unknown, start playback
      if ([mp loadState] != MPMovieLoadStateUnknown)
      {
            // Remove observer
            [[NSNotificationCenter       defaultCenter] removeObserver:self
                                                                                name:MPMoviePlayerLoadStateDi dChangeNotification
                                                                              object:nil];

            // When tapping movie, status bar will appear, it shows up
            // in portrait mode by default. Set orientation to landscape
            //设置横屏
            [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLa ndscapeRight animated:NO];
            // Rotate the view for landscape playback
            [[self view] setBounds:CGRectMake(0, 0, 480, 320)];
            [[self view] setCenter:CGPointMake(160, 240)];
            //选中当前view
            [[self view] setTransform:CGAffineTransformMakeRot ation(M_PI / 2)];
           
            // Set frame of movieplayer
            [[mp view] setFrame:CGRectMake(0, 0, 480, 320)];
     
            // Add movie player as subview
            [[self view] addSubview:[mp view]];   
     
            // Play the movie
            [mp play];
      }
}


- (void) moviePlayBackDidFinish:(NSNotification*)notification
   
      //[[UIApplication sharedApplication] setStatusBarHidden:YES];
      //还原状态栏为默认状态
      [[UIApplication sharedApplication] setStatusBarStyle:UIStatusBarStyleDefault animated:NO];
        // Remove observer
      [[NSNotificationCenter       defaultCenter] removeObserver:self
                                                                                name:MPMoviePlayerPlaybackDid FinishNotification
                                                                            object:nil];
     
      [self dismissModalViewControll erAnimated:NO];     
}


- (void) readyPlayer
{
      mp =  [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
     
      if ([mp respondsToSelector:@selector(loadState)])
      {
            // Set movie player layout
            [mp setControlStyle:MPMovieControlStyleFulls creen];            //MPMovieControlStyleFulls creen            //MPMovieControlStyleEmbed ded
            //满屏
            [mp setFullscreen:YES];
            // 有助于减少延迟
            [mp prepareToPlay];
           
            // Register that the load state changed (movie is ready)
            [[NSNotificationCenter defaultCenter] addObserver:self
                                            selector:@selector(moviePlayerLoadStateChan ged:)
                                            name:MPMoviePlayerLoadStateDi dChangeNotification
                                            object:nil];
     
  else
  {
        // Play the movie For 3.1.x devices
        [mp play];
  }

  // Register to receive a notification when the movie has finished playing.
  [[NSNotificationCenter defaultCenter] addObserver:self
                                              selector:@selector(moviePlayBackDidFinish:)
                                              name:MPMoviePlayerPlaybackDid FinishNotification
                                              object:nil];
}

- (void)dealloc
{
      [mp release];
      [movieURL release];
      [loadingAni release];
      [label release];
     
      [super dealloc];
}

@end


上面为一个封装好的视频播放类,需要注意的是上面代码区分了3.1 和 以版本的
IOS差别,所以都可以直接去调用这个类。

调用时,例:

CustomMoviePlayerViewCon troller *moviePlayer = [[[CustomMoviePlayerViewCon troller alloc] init] autorelease];
      //将视频地址传过去
      moviePlayer.movieURL = [NSURL URLWithString:@"视频地址,网络地址和本地视频路径都可以"];
      //然后播放就OK了
      [moviePlayer readyPlayer];
      [self presentModalViewControll er:moviePlayer animated:NO];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值