MPMoviePlayerController和MPMoviePlayerViewController

本文深入探讨了使用ARC内存管理时,作为局部变量的MPMoviePlayerController在方法执行结束后被自动释放导致视频播放异常的问题,并提供了解决方案。同时,文章还解释了在模拟器上加载在线视频时出现断点无法调试的情况,以及如何通过简单修改代码避免此类崩溃。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MPMoviePlayerController和MPMoviePlayerViewController都是播放视频的原生类。有找到文章说是MPMoviePlayerController在4.0以后的被MPMoviePlayerViewController取代建议使用后者的今天在使用时发现有个问题


#import "videoViewController.h"
#import <MediaPlayer/MediaPlayer.h>
#define PATHSTRING @"http://devimages.apple.com/iphone/samples/bipbop/gear1/prog_index.m3u8"

@interface videoViewController ()

@property(nonatomic,strong)MPMoviePlayerViewController* moviePlay;
@property(nonatomic,strong)MPMoviePlayerController* player;
@end

@implementation videoViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    NSString* filepath=[[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v" ];
//     _moviePlay=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
//    _moviePlay=[[MPMoviePlayerViewController alloc] initWithContentURL:[[NSURL alloc] initWithString:PATHSTRING]];
//    _moviePlay.moviePlayer.view.hidden=NO;
//    [_moviePlay.moviePlayer prepareToPlay];
//    [self.view addSubview:_moviePlay.view];
//    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movienotification:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
//    [_moviePlay.moviePlayer play];
    
//    _player=[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
//    _player.view.frame=CGRectMake(0, 0, 320, 460);
//    [_player prepareToPlay];
//    [self.view addSubview:_player.view];
//    [_player play];

在将俩个对象做为全局变量时都能正常显示。

//    MPMoviePlayerViewController* player=[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
//    [player.moviePlayer prepareToPlay];
//    [self.view addSubview:player.view];
////    [self presentMoviePlayerViewControllerAnimated:player];
//    [player.moviePlayer play];
    
    MPMoviePlayerController* innerplay=[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filepath]];
//    innerplay.view.
    innerplay.view.frame=CGRectMake(0, 0, 320, 480);
    [innerplay prepareToPlay];
    [self.view addSubview: innerplay.view];
    [innerplay play];

当作为局部变量的时候无论是addsubview还是 presentMoviePlayerViewControllerAnimated方法

上都不行。


之前问题已解决

原因:

由于项目使用的ARC内存管理方式,所以做为局部变量的MPMoviePlayerController会在方法执行结束之后作为方法的局部变量释放掉,照成了视频只能播放个几秒后面直接就黑屏的现象,而且在模拟器不能打断点调试的(详细见下面),所以会找不出是什么原因。今天又碰上了这个情况,刚好碰上有人回答。一想确实会照成这样的解决。贴上原网址:点击打开链接



关于在模拟器上加载在线视频项目中有断点存在时会crash的情况,只要去掉断掉就可以。具体原因看原链接:

点击打开链接



音频:

  1. #import <AVFoundation/AVFoundation.h>     
  2. NSError* err;  
  3. AVAudioPlayer* player = [[AVAudioPlayer alloc]  
  4.                         initWithContentsOfURL:[NSURL fileURLWithPath:  
  5.                                               [[NSBundle mainBundle]pathForResource:  
  6.                                            @"music" ofType:@"m4a"   
  7.                                            inDirectory:@"/"]]  
  8.                         error:&err ];//使用本地URL创建  
网络的使用fileurlwithdata方法来init


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值