3

MPMoviePlayerController在我建立的一个名为MYVideo. 这是代码:

#import <MediaPlayer/MediaPlayer.h>
#import "MYVideo.h"

@interface MYVideo()
@property (strong, nonatomic) UIView * viewRef;
@property (strong, nonatomic) NSDictionary * contentData;
@property (strong, nonatomic) MPMoviePlayerController * videoController;
@end

@implementation MYVideo
@synthesize contentData,videoController,viewRef;

- (MYVideo*) initIntoView: (UIView*) view withContent:(NSDictionary*)contentDict{
    self=[super init];
    viewRef=view;
    contentData = contentDict;
    NSString *rawUrl = [[NSString alloc] initWithFormat:@"http://....com/app/%@.mp4", [contentDict objectForKey:@"cnid"]];
    NSURL *videoUrl = [[NSURL alloc]initWithString:rawUrl];
    videoController = [[MPMoviePlayerController alloc] initWithContentURL:videoUrl];
    videoController.movieSourceType=MPMovieSourceTypeFile;
    videoController.view.frame = viewRef.bounds;
    [videoController.view setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
    videoController.controlStyle=MPMovieControlStyleNone; 
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(playbackFinished:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:videoController];
    [viewRef addSubview:videoController.view];
    return self;
}

- (void) playbackFinished: (NSNotification*) notification {
    NSLog(@"playback finished");
    if(videoController){
        [videoController play];
    }
}

- (void) play: (int) offset {
    videoController.initialPlaybackTime=offset;
    [videoController play];
}

- (void) stop {
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:@"playbackFinished"
                                                  object:nil];
    if(videoController){
        [videoController stop];
    }
}

- (void) destroy {
    if(videoController){
        [videoController stop];
        [videoController.view removeFromSuperview];
    }
}
@end

我的问题是偶尔会出现以下错误:

playback finished
-[__NSCFString playbackFinished:]: unrecognized selector sent to instance 0x1664e6a0

我猜这是由于MPMoviePlayerController该视频类已经发布时触发“playbackFinished”通知引起的。我这样想对吗?

问题是,这个MYVideo类在视频播放时应该仍然存在,这个错误只在视频播放时发生,并且在控制台日志中我的“播放完成”的 NSLogging 立即发生在崩溃之前。此外,我从来没有在没有先删除“playbackFinished”观察者的情况下关闭课程。

任何人都可以向我建议为什么我会遇到这种崩溃?

非常感谢。

4

1 回答 1

3

是的,看起来您并没有删除观察者,因为这段代码:

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:@"playbackFinished"
                                              object:nil];

应该:

[[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:@"playbackFinished:"
                                              object:nil];         //  ^

或者更好(因为你并不真正关心被称为什么):

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

此外,鉴于您if (videoController) { ... }在很多地方都使用测试,您需要确保它nil尽快进行:

- (void)destroy {
    if(videoController){
        [videoController stop];
        [videoController.view removeFromSuperview];
        videoController = nil;   // Add
    }
}
于 2013-10-21T14:59:06.297 回答