1

で表示しようとしてUIToolBarいますBarButtonItems on MPMoviePlayerController。どのように実装するのかわかりません。

ユーザーがUITableViewのセルの1つをタップすると、ビデオファイルを再生しようとしています。その際、FB やツィーターで動画を共有するオプションをユーザーに提供したいと思います。

MPMoviePlayerController に共有 BarButtonItemを表示する方法がわかりません。iPhoneに付属の写真アプリに似たものを実装しようとしています。

誰でも私を助けてもらえますか?ありがとうございました!

4

1 に答える 1

0

MPMovieplayer は、この目的には適していません。AVPlayer (AVFoundation.framework の下にあります) を使用して、目的に合ったカスタム ムービー プレーヤーを作成できます。プロジェクトに通常の ViewController を作成し、以下のようなコードで AVPlayer を追加します。

-(void)viewDidLoad {
//prepare player
self.videoPlayer = [AVPlayer playerWithURL:<# NSURL for the video file #>];
self.videoPlayer.actionAtItemEnd = AVPlayerActionAtItemEndPause;

//prepare player layer
self.videoPlayerLayer = [AVPlayerLayer playerLayerWithPlayer:self.videoPlayer];
self.videoPlayerLayer.videoGravity = AVLayerVideoGravityResizeAspectFill;
self.videoPlayerLayer.frame = self.view.bounds;
[self.view.layer addSublayer:self.videoPlayerLayer];

//add player item status notofication handler
[self addObserver:self forKeyPath:@"videoPlayer.currentItem.status" options:NSKeyValueObservingOptionNew context:NULL];

//notification handler when player item completes playback
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playerItemDidReachEnd:) name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
}

//called when playback completes
-(void)playerItemDidReachEnd:(NSNotification *)notification {
[self.videoPlayer seekToTime:kCMTimeZero]; //rewind at the end of play

 //other tasks
 }

-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"videoPlayer.currentItem.status"]) {
    //NSLog(@"player status changed");

    if (self.videoPlayer.currentItem.status == AVPlayerItemStatusReadyToPlay) {
        //player is ready to play and you can enable your playback buttons here
    }
}
}

これは通常のビュー コントローラーであるため、再生/共有などのためにツールバー ボタン/ボタンを追加し、以下のようなプレーヤー アクションおよびその他の関連アクションをトリガーできます。

-(IBAction)play:(id)sender {
    [self.videoPlayer play];
}
-(IBAction)pause:(id)sender {
    [self.videoPlayer pause];
}
//etc.

また、dealloc でオブザーバーを必ず削除してください。

-(void)dealloc {
    //remove observers
    @try {
    [self removeObserver:self forKeyPath:@"videoPlayer.currentItem.status" context:NULL];
    }
    @catch (NSException *exception) {}
    @try {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:AVPlayerItemDidPlayToEndTimeNotification object:self.videoPlayer.currentItem];
    }
    @catch (NSException *exception) {}

    //other deallocations
    [super dealloc];
}

プロセスのより詳細で洗練された説明は、こちらから入手できるりんごのコンパニオン ガイドにあります。

于 2013-11-12T22:52:05.097 に答える