如何在AVPlayer IOS中使用URL播放YouTubevideo?
我想在AVPlayer中使用YouTube URL加载video,但它没有显示任何东西。每当我从本地存储使用NSBundle加载它工作正常。是有任何替代加载video或我们可以在AVPlayer中做的事情。
这是我的代码:
- (void)viewDidLoad { [super viewDidLoad]; NSError *setCategoryError = nil; [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback error: &setCategoryError]; AVAsset *asset = [AVAsset assetWithURL:[NSURL fileURLWithPath:@"http://www.youtube.com/watch?v=zPP6lXaL7KA&feature=youtube_gdata_player"]]; avPlayerItem = [[AVPlayerItem alloc]initWithAsset:asset]; self.songPlayer = [AVPlayer playerWithPlayerItem:avPlayerItem]; self.avPlayerLayer = [AVPlayerLayer playerLayerWithPlayer: self.songPlayer]; self.avPlayerLayer.frame = self.view.layer.bounds; UIView *newView = [[UIView alloc] initWithFrame:self.view.bounds]; [newView.layer addSublayer:avPlayerLayer]; [self.view addSubview:newView]; [ self.songPlayer play]; }
您应该使用iOS Youtube帮手库来播放YouTubevideo。
https://developers.google.com/youtube/v3/guides/ios_youtube_helper
我不知道你是否可以使用AVPlayer。 我见过一些使用CocoaControls上的MPMoviePlayerController的例子,就像这样: https ://www.cocoacontrols.com/controls/hcyoutubeparser或者这个: https ://www.cocoacontrols.com/controls/xcdyoutubevideoplayerviewcontroller
但我不认为直接在你的播放器中使用YouTube的url适合平台的ToS。 所以我会build议你使用Youtube帮手库,如果你打算发布你的应用程序。
我不认为如果你现在可以使用这个,因为我刚刚使用这个,遇到了同样的情况。
我读了苹果文档,它明确指出(你不能直接创build一个AVAsset
实例来表示HTTP实时stream中的媒体)。
相反,这里是苹果的例子:
NSURL *url = [NSURL URLWithString:@"<#Live stream URL#>];
//你可以在这里find一个testingstream
http://devimages.apple.com/iphone/samples/bipbop/bipbopall.m3u8 。
self.playerItem = [AVPlayerItem playerItemWithURL:url];
看,如果不是本地url
,应该是playerItemWithURL
:^ _ ^
- 设备locking时从iPod库中播放AVAudio
- AVPlayerItemVideoOutput.hasNewPixelBufferForItemTime无法正常工作
- 如何在Swift 2 / AVPlayer中恢复背景audio?
- AVPlayer持续时间是与Twilio南
- 如何创buildAVPlayer单例类
- 使用AVPlayer的MTAudioProcessingTap和远程URL进行AVFoundationaudio处理
- 从videourl中提取缩略图
- 即使allowExternalPlayback设置为NO,AVPlayer:AirPlaybutton也会显示在控制中心中
- 无法通过AVPlayer通过HTTP播放mp3文件