2012-06-29 11 views
6

Đây là lớp học của tôi mà được quản lý video của tôi:Chú ý: trong điều kiện bình thường, _fillInQueueWithExtraSpace: ignoreExistingItems: nên không được nhập lại

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

@interface video() 
{ 
    MPMoviePlayerController* videoView; 
} 
@end 

@implementation video 

static video *sharedSingleton = nil; 

+ (video *)sharedSingleton 
{ 
    @synchronized([video class]) 
    { 
     if (!sharedSingleton) 
      sharedSingleton = [[super allocWithZone:NULL] init]; 
     return sharedSingleton; 
    } 
    return nil; 
} 

- (id)init 
{ 
    self = [super init]; 

    CGRect dimVideo = CGRectMake(0, 0, 472, 400); 
    NSURL* videoPath = [[NSBundle mainBundle] URLForResource: @"videoName" withExtension: @"mp4"]; 

    self->videoView = [[MPMoviePlayerController alloc] initWithContentURL:videoPath]; 
    [self->videoView.view setFrame:dimVideo]; 
    [self->videoView prepareToPlay]; 

    return self; 
} 

- (void)addVideoOn:(UIView*)view{ 
    [view addSubview:self->videoView.view]; 
    [self->videoView play]; 
} 

- (void)removeVideo{ 
    [self->videoView stop]; 
    [self->videoView.view removeFromSuperview]; 
} 

@end 

Nhưng đôi khi tôi nhận được lỗi này khi phát video: CẢNH BÁO: dưới điều kiện bình thường, _fillInQueueWithExtraSpace: ignoreExistingItems: không nên được nhập lại.

Sự cố ở đâu? Cảm ơn bạn trước

Tôi nhận thấy một điều: video bị treo khi bạn chuyển thời gian giữa dừng và phát. Tôi đã sửa lỗi để video tạm dừng, không dừng lại.

+0

đó là truyền thống để tận dụng tên lớp học của bạn : "Video" thay vì "video" – nielsbot

+0

cũng điều này sẽ sụp đổ nếu 'self == nil' vì bất kỳ lý do nào trong' -init' – nielsbot

+0

tài liệu cho 'MPMoviePlayerController' cho biết" khung của người chơi phải khớp với "của người chơi phải phù hợp với ma cua ban. Tôi sẽ cố gắng sửa nó trước. – nielsbot

Trả lời

0

Đây là cách tôi sẽ cấu trúc mã của bạn

@interface Video : NSObject 
@property (nonatomic, strong, readonly) MPMoviePlayerController * videoView ; 
@end 

@implementation Video 
@synthesize videoView = _videoView ; 

+(Video *)sharedSingleton 
{ 
    static Video * __sharedSingleton ; 
    static dispatch_once_t once ; 
    dispatch_once(&once, ^{ 
     __sharedSingleton = [ [ [ self class ] alloc ] init ] ; 
    } 
    return __sharedSingleton ; 
} 

-(void)dealloc 
{ 
    [ _videoView stop ] ; 
    [ _videoView.view removeFromSuperview ] ; 
} 

-(void)ensureMoviePlayer 
{ 
    if (!_videoView) 
    { 
     NSURL* url = [[NSBundle mainBundle] URLForResource:@"videoName" withExtension:@"mp4"]; 
     _videoView = [[MPMoviePlayerController alloc] initWithContentURL:url]; 
    } 
} 

- (void)addVideoOn:(UIView*)view 
{ 
    [ self ensureMoviePlayer ] ; 
    self.videoView.view.frame = view.bounds ; // per documentation 
    [view addSubview:self.videoView.view]; 
    [self.videoView play]; 
} 

- (void)removeVideo 
{ 
    [self.videoView stop]; 
    [ self.videoView.view removeFromSuperview ] ; 
} 

@end 

Nếu bạn đang cố gắng để chơi video ở một kích thước nhất định trong một cái nhìn mẹ, nhìn vào AVPlayer + AVPlayerLayer

Các vấn đề liên quan