2012-03-01 66 views
0

那麼我設計的iPhone應用程序將在本地播放視頻。當我點擊模擬器中的按鈕時,它會完美播放,但當它停止時或手動結束時,它會崩潰,並一直給我提出這個問題。我嘗試清理,構建,分析並再次運行,但仍然一樣。任何幫助?iphone ios xcode 4.2 - EXC_BAD_ACCESS信號

我的代碼是:

MoviePlayerViewController.h

#import <UIKit/UIKit.h> 
#import <Foundation/Foundation.h> 
#import <MediaPlayer/MediaPlayer.h> 

@interface MoviePlayerViewController : UIViewController { 

} 
-(IBAction)playMovie:(id)sender; 
@end 

和MoviePlayerViewController.m主鑽頭

- (IBAction)playMovie:(id)sender { 
    NSString *movpath = [[NSBundle mainBundle] pathForResource:@"think" ofType:@"mp4"]; 
    MPMoviePlayerViewController *mpviewController = [[MPMoviePlayerViewController alloc] 
                initWithContentURL:[NSURL fileURLWithPath:movpath]]; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinishedCallback:) 
               name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

    [self.view addSubview:mpviewController.view]; 
    MPMoviePlayerController *mp = [mpviewController moviePlayer]; 
    [mp prepareToPlay]; 
    mp.scalingMode=MPMovieScalingModeAspectFill; 
    [[mpviewController moviePlayer] play]; 
} 

- (void)playbackFinishedCallback:(NSNotification *)notification { 
    MPMoviePlayerViewController *mpviewController = [notification object]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController]; 
    [mpviewController.view removeFromSuperview]; 
    [mpviewController release]; 
} 
+0

使用殭屍運行你的應用程序儀器。它會告訴你到底是什麼問題。 – edc1591 2012-03-01 18:06:04

+0

如何?在新的Xcode是完全不同的。我搜索了它,但沒有找到它.. – 2012-03-01 18:07:20

+0

如果你進入你的計劃設置有一個殭屍複選框。它會導致你試圖訪問的殭屍對象被打印在日誌中。然後你可以看到它們是什麼並修復它們:) – 2012-03-01 18:09:40

回答

0

您是否嘗試過製作電影播放控制器吶伊娃

#import <UIKit/UIKit.h> 
    #import <Foundation/Foundation.h> 
    #import <MediaPlayer/MediaPlayer.h> 

    @interface MoviePlayerViewController : UIViewController { 

    } 

    @property (nonatomic, retain) MPMoviePlayerViewController *mpviewController; 

    -(IBAction)playMovie:(id)sender; 
    @end 

然後你就可以在實現文件做一些像這樣

@synthesize mpviewController; 

    - (IBAction)playMovie:(id)sender { 
     NSString *movpath = [[NSBundle mainBundle] pathForResource:@"think" ofType:@"mp4"]; 
     MPMoviePlayerViewController *mpController = [[MPMoviePlayerViewController alloc] 
                 initWithContentURL:[NSURL fileURLWithPath:movpath]]; 

     self.mpviewController = mpController; 
     [mpController release];            

     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinishedCallback:) 
                name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

     [self.view addSubview:self.mpviewController.view]; 
     MPMoviePlayerController *mp = [self.mpviewController moviePlayer]; 
     [mp prepareToPlay]; 
     mp.scalingMode=MPMovieScalingModeAspectFill; 
     [[self.mpviewController moviePlayer] play]; 
    } 

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

     [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController]; 
     [mpviewController.view removeFromSuperview]; 
    } 

    - (void)viewDidUnload { 
    self.mpviewController = nil; 
    } 

    - (void)dealloc{ 
    self.mpviewController = nil; 

    [super dealloc]; 
    } 
+0

歡呼男人這是最好的方式!謝謝你! – 2012-03-01 18:49:07

1

有在代碼中的幾個問題,這裏有修復:

1>刪除[mpviewController release];,因爲它是cr使用返回*autorelease*對象的方法進行訪問([notification object])。釋放mpviewController對象將其聲明爲實例變量並釋放它並使其爲零。

if(mpviewController != nil) 
{ 
[mpviewController release]; 
mpviewController = nil; 
} 

2>正如你所宣佈mpviewController作爲實例變量,沒有必要通過[notification object]訪問mpviewController變量,因爲它不存在,你有沒有當你添加觀察者通知中心提供它。

3>替換的代碼下面的行:

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:mpviewController]; 

[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 

闡釋:當添加觀察者您沒有提供任何對象的信息,但在去除時你

所以現在你的代碼將變成:

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

    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil]; 
    [mpviewController.view removeFromSuperview]; 
    if(mpviewController != nil) 
    { 
     [mpviewController release]; 
     mpviewController = nil; 
    } 
} 

此外,在此控制器的- (void) dealloc中,您應該編寫用於發佈mpviewController的類似代碼。

感謝,

+0

我試過,但後來它不承認mpviewController ..我宣佈它沒有錯誤,但它仍然不斷崩潰,並給我同樣的錯誤。 – 2012-03-01 18:43:38

+0

請發佈錯誤和崩潰日誌。 – Ravin 2012-03-01 18:50:17