iphone ios xcode 4.2 - EXC_BAD_ACCESS信号

时间:2012-03-01 18:04:43

标签: iphone ios xcode ios5

我正在设计一款可在本地播放视频的iPhone应用程序。当我单击模拟器中的按钮时,它会完美播放,但是当它停止或者我手动结束时它会崩溃并继续给我这个问题。我尝试清理,构建,分析并再次运行但仍然相同。有什么帮助吗?

我的代码是:

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];
}

2 个答案:

答案 0 :(得分:1)

代码中存在一些问题,以下是修复:

1&GT;删除[mpviewController release];,因为它是使用返回*autorelease*对象的方法创建的。[notification object]。要释放mpviewController对象,请将其声明为实例变量并释放它并将其设为nil。

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

2 - ;由于您已将mpviewController声明为实例变量,因此无需通过mpviewController访问[notification object]变量,因为当您将观察者添加到通知中心时,它没有提供它。 / p>

3&GT;替换以下代码行:

 [[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

谢谢,

答案 1 :(得分:0)

你有没有尝试制作电影播放器​​控制器na ivar

  #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];
  }