2010-12-20 23 views
0

你好!我正在編寫一個iPad應用程序,當UIView加載時我需要能夠播放視頻。然而,如果我在初始化它的任何地方嘗試向我的MPMoviePlayerController發送消息,我正在獲得BAD_EXC_ACCESS。我從我的* .h文件中刪除了MPMediaPlayerController,然後完全在實現文件中聲明瞭它,現在我在代碼下方的底部得到消息。在構建和分析內存泄漏(或任何問題)方面,沒有任何問題,並且我找不到任何關於此的帖子。這裏是我的代碼:當UIView加載時立即播放視頻

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; 
    if (self) { 

    } 

NSString *url = [[NSBundle mainBundle] pathForResource:@"p0600c0100cmpintro" ofType:@"m4v"]; 
MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:url]]; 
NSLog(@"%@", movie); 
movie.view.frame = CGRectMake(5, 0, 1035, 768); 
movie.view.contentMode = UIViewContentModeScaleToFill; 
[[movie view] setCenter:CGPointMake(movie.view.center.x-10, movie.view.center.y)]; 
[movie setControlStyle:MPMovieControlStyleNone]; 
    [movie setShouldAutoplay:YES]; 
[[self view] addSubview:[movie view]]; 

    return self; 
    } 

的 「電影」 的NSLog賦予 「的MPMoviePlayerController:0x1b77f0」,但隨後在碰撞時的錯誤消息是 「* - [的MPMoviePlayerController playbackState]:消息發送到釋放實例0x1473a0」。幫幫我?

回答

0

幾周前我找到了解決方案,並忘記了這篇文章。我沒有成功發佈MPMoviePlayerController。對於那些好奇的人,爲了發佈MPMoviePlayerController,我們必須首先從NSNotificationCenter中刪除通知(如果已設置),停止播放電影(即使播放完畢),然後釋放它。我之前沒有在應用程序中使用我的第一個MPMoviePlayerController執行此操作,因此它試圖引用釋放的實例。當電影完成播放時,以下是成功發佈電影的代碼:

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackStateDidChangeNotification object:movie]; 
[movie.view removeFromSuperview]; 
[movie stop]; 
[movie release]; 
0

根據文檔,它看起來像電影視圖的框架需要匹配其父視圖。也可以嘗試移動你的代碼了initWithNibName的:束:到viewDidLoad中:

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    UIView *movieContainer = [[UIView alloc] initWithFrame:CGRectMake(5, 0, 300, 400)]; 
    //Do any other positioning of the view you would like 

    NSString *path = [[NSBundle mainBundle] pathForResource:@"p0600c0100cmpintro" ofType:@"m4v"]; 
    NSURL *url = [NSURL fileURLWithPath:path]; 
    MPMoviePlayerController *movie = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
    movie.view.frame = movieContainer.bounds; //Make sure this is the bounds of its parent view 
    movie.scalingMode = MPMovieScalingModeFill; 
    movie.controlStyle = MPMovieControlStyleNone; 
    movie.shouldAutoplay = YES; 

    [movieContainer addSubview:movie.view]; 
    [self.view addSubview:movieContainer]; 

    [movieContainer release]; 
} 

最後一個建議是保持電影的參考,這樣就可以的dealloc一次的視圖控制器被釋放

+0

不行。我仍然收到錯誤消息「發送到解除分配的實例

」。但現在我得到一個不錯的空白頁面,而不是應用程序崩潰。這是因爲我將代碼移到了viewDidLoad方法中。 – Jacob 2010-12-20 22:40:46

0

我會建議您在viewDidLoad中創建您的MoviePlayer,然後在viewDidAppear中進行電影播放,以獲得最佳效果。

+0

當我嘗試分配並初始化MPMoviePlayerController時,就會出現問題。我不知道爲什麼。我在我的應用程序中播放了另一個視頻,並將其內容與我正在與之戰鬥的視頻切換,而且播放效果非常好。 – Jacob 2010-12-20 22:51:37

+0

那麼視頻本身會出現問題? – WrightsCS 2010-12-20 23:02:36

+0

啊,我的意思被誤解了。我遇到的視頻在第一張專輯中表現不錯。我設法讓視頻的第一幀出現,但是如果我將shouldAutoplay設置爲YES,它會再次給我「解除分配」的消息並使應用程序崩潰。如果我試圖發送「電影」「播放」消息,我會得到「釋放」消息。 – Jacob 2010-12-20 23:40:20

0

好的,所以我有另一個MPMoviePlayerController實例被更早解除分配,但是當我嘗試創建MPMoviePlayerController的另一個實例時,發送到這個實例的所有消息都被髮送到解除分配的實例,導致內存問題。所以我只是刪除了我發佈第一個實例的部分,並且它工作得很好。我現在的問題是:有沒有辦法將這個第一個實例解除分配,以便它在不需要時不會成爲內存負擔?我覺得應該有一個更優雅的解決方案來解決這個問題。我需要在這個應用程序中經常播放視頻。