一段简单的代码,实现iOS源生播放器,在线播放视频

#import "FourViewController.h"

#import <MediaPlayer/MediaPlayer.h>

@interface FourViewController ()



@property (nonatomic,strong) MPMoviePlayerController *moviePlayer;//视频播放控制器


@end


@implementation FourViewController


- (void)viewDidLoad {

    [super viewDidLoad];

    //播放

    [self.moviePlayer play];

    

    //添加通知

    [self addNotification];

}

-(void)dealloc{

    //移除所有通知监控

    [[NSNotificationCenter defaultCenter] removeObserver:self];

}

#pragma mark - 私有方法

/**

 *  取得本地文件路径

 *

 *  @return 文件路径

 */

//-(NSURL *)getFileUrl{

//    NSString *urlStr=[[NSBundle mainBundle] pathForResource:@"The New Look of OS X Yosemite.mp4" ofType:nil];

//    NSURL *url=[NSURL fileURLWithPath:urlStr];

//    return url;

//}


/**

 *  取得网络文件路径

 *

 *  @return 文件路径

 */

-(NSURL *)getNetworkUrl{

    NSString *urlStr=@"放入你的视频播放地址";// 切记不要放入网页,要放入视频链接地址

    urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url=[NSURL URLWithString:urlStr];

    return url;

}


/**

 *  创建媒体播放控制器

 *

 *  @return 媒体播放控制器

 */

-(MPMoviePlayerController *)moviePlayer{

    if (!_moviePlayer) {

        NSURL *url=[self getNetworkUrl];

        

        _moviePlayer=[[MPMoviePlayerController alloc]initWithContentURL:url];

          // 设置播放器的大小

        _moviePlayer.view.frame = CGRectMake(0, 55, VIEW_WIDTH, 300);

                  _moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

        [self.view addSubview:_moviePlayer.view];

    }

    return _moviePlayer;

}


/**

 *  添加通知监控媒体播放控制器状态

 */

-(void)addNotification{

    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];

    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackStateChange:) name:MPMoviePlayerPlaybackStateDidChangeNotification object:self.moviePlayer];

    [notificationCenter addObserver:self selector:@selector(mediaPlayerPlaybackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];

    

}


/**

 *  播放状态改变,注意播放完成时的状态是暂停

 *

 *  @param notification 通知对象

 */

// 观察是否播放

-(void)mediaPlayerPlaybackStateChange:(NSNotification *)notification{

    switch (self.moviePlayer.playbackState) {

        case 1:

            NSLog(@"正在播放...");

            break;

        case 2:

            NSLog(@"暂停播放...");

            break;

        default:

            NSLog(@"播放状态:%li",self.moviePlayer.playbackState);

            break;

    }

}


/**

 *  播放完成

 *

 *  @param notification 通知对象

 */

-(void)mediaPlayerPlaybackFinished:(NSNotification *)notification{

    NSLog(@"播放完成.%li",self.moviePlayer.playbackState);

}

- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.

}



@end


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值