iOS 播放本地,網絡視頻


 

 

/**

 *  創建媒體播放控制器MPMoviePlayerControlle 可以控制尺寸

 *

 *  @return 媒體播放控制器

 */

-(MPMoviePlayerController *)moviePlayer{

    if (!_moviePlayer) {

        NSURL *url=[self getFileUrl];

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

        _moviePlayer.view.frame=self.view.bounds;

        _moviePlayer.view.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;

        [self.view addSubview:_moviePlayer.view];

    }

    return _moviePlayer;

}

- (void)viewDidLoad {

    [super viewDidLoad];

 

        //播放

        [self.moviePlayer play];

   }

 

#pragma mark - 私有方法

//獲取本地路徑

-(NSURL *)getFileUrl{

    NSString *urlStr=[[NSBundle mainBundle] pathForResource:@"xxx.mp4" ofType:nil];

    NSURL *url=[NSURL fileURLWithPath:urlStr];

    return url;

}

 

/**

 *  取得網絡文件路徑

 *

 *  @return 文件路徑

 */

-(NSURL *)getNetworkUrl{

    NSString *urlStr=@"http://192.168.1.161/xxxx.mp4";

    urlStr=[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSURL *url=[NSURL URLWithString:urlStr];

    return url;

}

/**

 *  添加通知監控媒體播放控制器狀態

 */

-(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 MPMoviePlaybackStatePlaying:

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

            break;

        case MPMoviePlaybackStatePaused:

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

            break;

        case MPMoviePlaybackStateStopped:

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

            break;

        default:

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

            break;

    }

}

 

/**

 *  播放完成

 *

 *  @param notification 通知對象

 */

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

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

}

 

 

//使用 MPMoviePlayerViewController,只能全屏

/**

 *  視頻播放控制器全屏

 */

@property (nonatomic,strong) MPMoviePlayerViewController *moviePlayerViewController;

 

-(MPMoviePlayerViewController *)moviePlayerViewController{

    if (!_moviePlayerViewController) {

        NSURL *url=self.videoUrl;

        _moviePlayerViewController=[[MPMoviePlayerViewController alloc]initWithContentURL:url];

        [self addNotification1];

    }

    return _moviePlayerViewController;

}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event

{

    //播放

      [self presentMoviePlayerViewControllerAnimated:self.moviePlayerViewController];

}

 

-(void)addNotification1{

    NSNotificationCenter *notificationCenter=[NSNotificationCenter defaultCenter];

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

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

    

}

 

/**

 *  播放狀態改變,注意播放完成時的狀態是暫停

 *

 *  @param notification 通知對象

 */

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

    switch (self.moviePlayerViewController.moviePlayer.playbackState) {

        case MPMoviePlaybackStatePlaying:

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

            break;

        case MPMoviePlaybackStatePaused:

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

            break;

        case MPMoviePlaybackStateStopped:

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

             self.moviePlayerViewController =nil;

            break;

        default:

            NSLog(@"播放狀態:%li",self.moviePlayerViewController.moviePlayer.playbackState);

            break;

    }

}

 

/**

 *  播放完成

 *

 *  @param notification 通知對象

 */

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

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

    self.moviePlayerViewController =nil;

}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM