我正在尝试使用MPMoviePlayerController传输一些youTube视频,但我遇到了一些问题.我正在使用的代码非常简单,我可以通过将URL传递给initWithContentURL来播放.m4v视频.当我启动电影播放器时,播放器出现但在大约20秒后消失.当我在模拟器中尝试它时,我得到一个警告视图,说明服务器配置不正确.我是否需要通过URL传递来自谷歌的特定类型的视频源?
NSURL *videoURL = [NSURL URLWithString:@"http://www.youtube.com/v/HGd9qAfpZio&hl=en_US&fs=1&"];
MPMoviePlayerController *moviePlayer;
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
[moviePlayer play];
Run Code Online (Sandbox Code Playgroud)
我还尝试了以下网址 http://www.youtube.com/watch?v=HGd9qAfpZio
我也看到了参数&format = 1并尝试将其添加到两个字符串的末尾,但没有运气.
我正在开发一个需要播放视频的iPhone应用程序.到目前为止,我了解到实现这一目标至少有两个API; AVPlayer和MPMoviePlayerController.
有哪些主要区别?
我没有收到任何通知MPMoviePlayerController.我究竟做错了什么?
我使用以下逻辑.
我开始播放youtube视频了UIWebView.UIWebView称之为标准MPMoviePlayerController.我不控制MPMoviePlayerController因为我没有实例化MPMoviePlayerController.
我使用自动播放运行youtube的剪辑(延迟1秒):
[self performSelector:@selector(touchInView:) withObject:b afterDelay:1];
Run Code Online (Sandbox Code Playgroud)
我的代码是:
- (void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadStateDidChange:) name:MPMoviePlayerLoadStateDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackDidFinish:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[self embedYouTube];
}
- (void)loadStateDidChange:(NSNotification*)notification
{
NSLog(@"________loadStateDidChange");
}
- (void)playbackDidFinish:(NSNotification*)notification
{
NSLog(@"________DidExitFullscreenNotification");
}
- (void)embedYouTube
{
CGRect frame = CGRectMake(25, 89, 161, 121);
NSString *urlString = [NSString stringWithString:@"http://www.youtube.com/watch?v=sh29Pm1Rrc0"];
NSString *embedHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>"; …Run Code Online (Sandbox Code Playgroud) youtube mpmovieplayercontroller mpmovieplayer nsnotificationcenter ios
更新到iOS 8.4后,我得到了臭名昭着的例外,MPMoviePlayerController其中说:
AVPlayerItem不能与多个AVPlayer实例关联
我已经看到了几个解决方法,主要包括在重用之前重新初始化播放器.但是,对我来说,当我尝试播放新视频时,不会发生崩溃,而是当我通过旋转到纵向模式关闭播放器的全屏时.
这是我的代码:
@implementation MoviePlayerViewController
-(void)viewDidLoad
{
[super viewDidLoad];
self.moviePlayer.controlStyle = MPMovieControlStyleEmbedded;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillEnterFullscreenNotification:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayerWillExitFullscreenNotification:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
}
- (void) moviePlayerWillEnterFullscreenNotification:(NSNotification*)notification
{
[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientationDidChange) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void) moviePlayerWillExitFullscreenNotification:(NSNotification*)notification
{
[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)deviceOrientationDidChange
{
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
if (orientation == UIDeviceOrientationPortrait) {
[self.moviePlayer setFullscreen:NO animated:YES];
}
}
@end
Run Code Online (Sandbox Code Playgroud)
对全屏的更改发生在UIViewController具有MoviePlayerViewController …
我正在尝试使用Swift从服务器播放视频.
我已经导入了MediaPlayer框架,这是我的代码:
import UIKit
import MediaPlayer
class VideoViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
var moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)
self.view.addSubview(moviePlayer.view)
moviePlayer.fullscreen = true
moviePlayer.controlStyle = MPMovieControlStyle.Embedded
}
}
Run Code Online (Sandbox Code Playgroud)
当我在模拟器中运行时,我只得到一个黑盒子,但无论我在哪里尝试加载视频,都没有播放视频.
UPDATE
这是当前的代码
var url:NSURL = NSURL(string: "http://jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v")
var moviePlayer = MPMoviePlayerController(contentURL: url)
moviePlayer.view.frame = CGRect(x: 20, y: 100, width: 200, height: 150)
moviePlayer.movieSourceType = MPMovieSourceType.File
self.view.addSubview(moviePlayer.view)
moviePlayer.prepareToPlay()
moviePlayer.play()
Run Code Online (Sandbox Code Playgroud)
这个代码有趣地播放~2秒的视频再次变黑!
我想从我的iPhone应用程序播放YouTube视频.我必须尝试使用以下代码在我的iPhone应用中播放YouTube视频,
[self playVideo:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q" frame:CGRectMake(20, 70, 280, 250)];
- (void)playVideo:(NSString *)urlString frame:(CGRect)frame
{
NSString *embedHTML = @"\
<html><head>\
<style type=\"text/css\">\
body {\
background-color: transparent;\
color: white;\
}\
</style>\
</head><body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";
NSString *html = [NSString stringWithFormat:embedHTML, urlString, frame.size.width, frame.size.height];
UIWebView *videoView = [[UIWebView alloc] initWithFrame:frame];
[videoView loadHTMLString:html baseURL:nil];
[self.view addSubview:videoView];
[videoView release];
NSLog(@"%@",html);
}
Run Code Online (Sandbox Code Playgroud)
但是,这段代码对我不起作用.而且我也试过MPMoviePlayerController代码是,
NSURL *fileURL = [NSURL URLWithString:@"http://www.youtube.com/watch?v=WL2l_Q1AR_Q"];
MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:fileURL];
[moviePlayerController.view setFrame:CGRectMake(0, 70, 320, 270)];
[self.view …Run Code Online (Sandbox Code Playgroud) 我创建了一个空白项目(iOS)并将其放在我的viewDidLoad中:
NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"];
MPMoviePlayerViewController *playerController = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:moviePath]];
[self presentMoviePlayerViewControllerAnimated:playerController];
[playerController.moviePlayer play];
Run Code Online (Sandbox Code Playgroud)
当应用程序启动时,我得到的是一个白色屏幕,其中包含日志中的错误消息:
<Error>: CGContextSaveGState: invalid context 0x0
<Error>: CGContextClipToRect: invalid context 0x0
<Error>: CGContextTranslateCTM: invalid context 0x0
<Error>: CGContextDrawShading: invalid context 0x0
<Error>: CGContextRestoreGState: invalid context 0x0
Warning: Attempt to present <MPMoviePlayerViewController: 0x821e3b0> on <ViewController: 0x863aa40> whose view is not in the window hierarchy!
Run Code Online (Sandbox Code Playgroud)
......以及一系列关于禁用自动播放的内容.我特别不理解视图不属于层次结构的一行,因为它是一个空白的"单视图应用程序"iOS项目,代码在ViewController.m中.它位于视图层次结构中.
我知道电影文件本身不是问题,因为我是从Apple的MPMoviePlayer上的示例代码中得到的.尽管我(看似)尝试了样本中所写的所有内容,但我还是无法让播放器工作.
这是另一个尝试,这次使用MPMoviePlayerController(不是MPMoviePlayerViewController):
MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:url];
[player setContentURL:url];
[player setMovieSourceType:MPMovieSourceTypeFile];
[[player view] setFrame:self.view.bounds]; …Run Code Online (Sandbox Code Playgroud) 在我的iPhone上,我正在以全屏模式播放视频/音频文件.当视频/音频文件到达终点时,将触发以下方法:
- (void) movieFinishedCallback:(NSNotification*) aNotification {
MPMoviePlayerController *player = [aNotification object];
[player stop];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
[player autorelease];
[moviePlayer.view removeFromSuperview];
NSLog(@"stopped?");
}
Run Code Online (Sandbox Code Playgroud)
这很好用!但问题是当视频/音频文件仍在播放时我按下"完成"按钮.那么这个方法不会被触发......
任何人都知道在按下"完成"按钮时如何捕捉事件?因为现在媒体播放器仍然在视图中.它并没有消失.
我正在努力更新MPNowPlayingInfoCenter并遇到一些麻烦.我已经尝试了很多,以至于我不知所措.以下是我的代码:
self.audioPlayer.allowsAirPlay = NO;
Class playingInfoCenter = NSClassFromString(@"MPNowPlayingInfoCenter");
if (playingInfoCenter) {
NSMutableDictionary *songInfo = [[NSMutableDictionary alloc] init];
MPMediaItemArtwork *albumArt = [[MPMediaItemArtwork alloc] initWithImage:[UIImage imageNamed:@"series_placeholder"]];
[songInfo setObject:thePodcast.title forKey:MPMediaItemPropertyTitle];
[songInfo setObject:thePodcast.author forKey:MPMediaItemPropertyArtist];
[songInfo setObject:@"NCC" forKey:MPMediaItemPropertyAlbumTitle];
[songInfo setObject:albumArt forKey:MPMediaItemPropertyArtwork];
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:songInfo];
}
Run Code Online (Sandbox Code Playgroud)
这不起作用,我也尝试过:
[[MPNowPlayingInfoCenter defaultCenter] setNowPlayingInfo:nil];
Run Code Online (Sandbox Code Playgroud)
试图让它从iPod应用程序中删除现有信息(或任何可能有信息的信息).另外,为了看看我是否能找到问题,我已经尝试检索应用启动时的当前信息:
NSDictionary *info = [[MPNowPlayingInfoCenter defaultCenter] nowPlayingInfo];
NSString *title = [info valueForKey:MPMediaItemPropertyTitle];
NSString *author = [info valueForKey:MPMediaItemPropertyArtist];
NSLog(@"Currently playing: %@ // %@", title, author);
Run Code Online (Sandbox Code Playgroud)
我明白了 Currently playing: (null) // (null)
我对此进行了相当多的研究,以下文章对此进行了彻底的解释,但是,我仍然无法正常工作.我错过了什么吗?会有什么干扰吗?这是我的应用程序需要注册访问的服务(在任何文档中都没有看到这个)吗?
首先是关于应用程序的一些背景信息......
- 有很多涉及视频播放器的大量UI操作(主要是滚动)
- 视频是动态的,并根据我们当前的页面进行更改.
- 因此视频必须是动态的并且不断变化,并且UI需要响应
我最初使用a MPMoviePlayerController但是由于某些要求我必须回到AVPlayer
上我自己制作了包装器AVPlayer.
要更改videoPlayer中的内容,这是AVPlayer-wrapper类中的方法
/**We need to change the whole playerItem each time we wish to change a video url */
-(void)initializePlayerWithUrl:(NSURL *)url
{
AVPlayerItem *tempItem = [AVPlayerItem playerItemWithURL:url];
[tempItem addObserver:self forKeyPath:@"status"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:nil];
[tempItem addObserver:self forKeyPath:@"playbackBufferEmpty"
options:NSKeyValueObservingOptionInitial | NSKeyValueObservingOptionNew
context:nil];
//Not sure if this should be stopped or paused under the ideal circumstances
//These will be changed to custom enums later
[self setPlaybackState:MPMoviePlaybackStateStopped];
[self setLoadState:MPMovieLoadStateUnknown];
[self.videoPlayer replaceCurrentItemWithPlayerItem:tempItem]; …Run Code Online (Sandbox Code Playgroud) ios ×10
avplayer ×3
iphone ×3
video ×3
youtube ×3
avfoundation ×2
objective-c ×2
cocoa-touch ×1
ios4 ×1
ios8.4 ×1
ipad ×1
playback ×1
streaming ×1
swift ×1
uiwebview ×1