6

これは私のビデオを管理した私のクラスです:

#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

しかし、ビデオを再生すると、次のエラーが表示されることがあります。

問題はどこだ?前もって感謝します

1 つのことに気付きました: 停止してから再生するまでの時間を過ぎると、ビデオがフリーズします。ビデオを停止せずに一時停止したままにするエラーを修正しました。

4

1 に答える 1

0

これが私があなたのコードを構造化する方法です

@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

親ビュー内で特定のサイズでビデオを再生しようとしている場合は、AVPlayer+を見てくださいAVPlayerLayer

于 2013-03-16T22:02:36.807 に答える