2013-10-21 47 views
3

Im使用MPMoviePlayerController在Ive類中創建,名爲MYVideo。下面是代碼:__NSCFString playback MPMoviePlayerControllerFigureished unrecognized selector

#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」觀察者,我也不會關閉課程。

有人可以告訴我爲什麼我會得到這個崩潰?

非常感謝。

回答

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) { ... }這樣的測試,你需要ensu回去吧nil儘快:

- (void)destroy { 
    if(videoController){ 
     [videoController stop]; 
     [videoController.view removeFromSuperview]; 
     videoController = nil; // Add 
    } 
} 
+0

非常感謝!我一直盯着這個代碼幾個小時,而我錯過了一個該死的錯字! gah ......也歡呼你的建議。 – Jimmery

相關問題