我正在尝试使用UIToolBar
显示BarButtonItems on MPMoviePlayerController
。不知道我将如何实现它。
当用户点击UITableView的一个单元格时,我正在尝试播放视频文件。那时我想给用户一个选项,在FB或tweeter上分享视频。
不确定如何在MPMoviePlayerController上显示共享BarButtonItem 。我正在尝试实现类似于iPhone附带的照片应用程序。
任何人都可以帮帮我吗? 谢谢!
答案 0 :(得分: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];
}
有关此流程的更详细和更复杂的说明,请参阅Apple随附指南,here