我正在使用MPMoviePlayerController播放一个小视频作为我的应用的介绍视频。我使用了以下代码,这很好用。但视频继续播放。结束并重新开始。即使我点击完成按钮,它也不会删除超级视图。我也希望我的状态栏可见,我保持半透明仍然没有结果。我搜索谷歌和堆栈溢出但仍然没有帮助。任何帮助都会非常明显。
我在[self playMovie]
viewDidAppear
方法
-(void)playMovie
{
NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"avc_intro" ofType:@"mp4"]];
moviePlayer = [[MPMoviePlayerController alloc]
initWithContentURL:url];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
name:MPMoviePlayerWillExitFullscreenNotification
object:moviePlayer];
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.shouldAutoplay = YES;
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES animated:YES];
}
- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *videoplayer = [notification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:videoplayer];
if ([videoplayer
respondsToSelector:@selector(setFullscreen:animated:)])
{
[videoplayer.view removeFromSuperview];
}
}
答案 0 :(得分:2)
更好地使用this. MPMoviePlayerController在iOS 9中正式弃用。
答案 1 :(得分:2)
从[self playMovie]
致电viewDidload
,因为viewDidAppear
会在您移除MPMoviePlayerController
时再次呼叫,因此它会再次启动。
更新:
您应该展示MPMusicPlayerController
而不是将其添加到超级视图中并在完成时将其关闭
希望这会有所帮助:)
答案 2 :(得分:0)
使用AVPlayerViewController,我的代码在下面给出,
目标-C代码:
声明AVPlayerViewController *playerViewController;
playerViewController = [[AVPlayerViewController alloc]init];
playerViewController.view.frame = videoView.bounds;
[playerViewController.view setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight)];
playerViewController.player = [AVPlayer playerWithURL:[NSURL URLWithString:viewModel.videoUrl]];
[videoView addSubview:playerViewController.view];
[playerViewController.player play];
playerViewController.player.actionAtItemEnd = AVPlayerActionAtItemEndNone;
不要忘记框架,
#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>
#import <AVKit/AVKit.h>
#import <MediaPlayer/MediaPlayer.h>
swift代码:
let type : String! = "mp4"
let targetURL : String? = NSBundle.mainBundle().pathForResource("Official Apple MacBook Air Video YouTube", ofType: "mp4")
let videoURL = NSURL(fileURLWithPath:targetURL!)
let player = AVPlayer(URL: videoURL)
let playerController = AVPlayerViewController()
playerController.player = player
self.addChildViewController(playerController)
self.playView.addSubview(playerController.view)
playerController.view.frame = playView.bounds
player.play()
不要忘记框架,
import UIKit
import AVKit
import AVFoundation
import MediaPlayer
它为我工作并希望它有用