6.iOS 视频播放 MPMoviePlayerController

/*
 通过MPMoviePlayerController或者MPMoviewPlayerViewController类(视图控制器)
 来播放视频(含流媒体视频播放)
 引入“MediaPalyer.franework”
 MPMoviePlayerController播放器可以任意修改比方页面尺寸
 MPMoviewPlayerViewController特殊视图控制器,包含一个播放器MPMoviePlayerController
 
 */
#import <UIKit/UIKit.h>
#import <MediaPlayer/MediaPlayer.h>
@interface MianViewController : UIViewController
#import "MianViewController.h"

@interface MianViewController ()

@end

@implementation MianViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //添加通知监控 MoviePlayer 的变化
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerFinsh:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:nil];
    }
    return self;
}
-(void)MPMoviePlayerFinsh:(NSNotification *)notification
{
//    id obj=notification.object;
    MPMoviePlayerController *moviePlayer=notification.object;
    //播放状态
    MPMoviePlaybackState playstate=moviePlayer.playbackState;
    if (playstate==MPMoviePlaybackStateSeekingForward) {
        NSLog(@"向前");
    }
    else if(playstate==MPMoviePlaybackStatePaused)
    {
        NSLog(@"暂停");
    }
    
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    //本地播放
    NSString *path=[[NSBundle mainBundle]pathForResource:@"少女时代oh超清 高清" ofType:@"mp4"];
    
//    NSURL *url=[[NSURL alloc]initWithString:@"http://baidu.iqiyi.com/kan/Q9Nm?fr=v.baidu.com/"];
    NSURL *url=[NSURL fileURLWithPath:path];
    MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:url];
    moviePlayer.view.frame=CGRectMake(10, 20, 300, 300);
    moviePlayer.backgroundView.backgroundColor=[UIColor greenColor];
  
    [self.view addSubview:moviePlayer.view];
    
    /*MPMoviePlaybackState     视频类相关通知
    //视频播放结束的通知
    MPMoviePlayerPlaybackDidFinishNotification
    // Posted when the scaling mode changes.
    MPMoviePlayerScalingModeDidChangeNotification
     
     */
    [moviePlayer prepareToPlay];
    [moviePlayer play];
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值