2013-10-21 2 views
3

Im MYVideo이라고 불리는 Ive 클래스 내에 MPMoviePlayerController을 사용합니다. 여기에 코드입니다 : 나는 "playbackFinished"알림을 발사 MPMoviePlayerController에 의해 발생 같은데요__NSCFString playback MPMoviePlayerController의 인식 할 수없는 선택기

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

때이 비디오 :

#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 

내 문제는 가끔 나는 다음과 같은 오류를 얻을 수 있다는 것입니다 클래스가 이미 릴리스되었습니다. 나는 이것을 생각하는 것이 맞습니까?

동영상이 재생되는 동안이 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

관련 문제