1

我正在尝试UIToolBar使用BarButtonItems on MPMoviePlayerController. 不知道我将如何实现它。

当用户点击 UITableView 的一个单元格时,我正在尝试播放视频文件。那时我想给用户一个选项,让他们在 FB 或 tweeter 上分享视频。

不知道如何在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 回答