2012-03-01 184 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