gpt4 book ai didi

ios - 使用 MPMoviePlayerController 播放 youtube 视频

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:16:58 24 4
gpt4 key购买 nike

我长期以来一直使用 html 字符串通过 UIWebView 播放 youtube 视频,问题是我想获得播放状态更改的通知。我决定创建一个 MPMoviePlayerController 并通过它播放 youtube 视频,但似乎无法正常工作。我在 viewdidload 中使用以下代码:

NSString *urlAddress = @"http://www.youtube.com/watch?v=m01MYOpbdIk";
NSURL *url = [NSURL URLWithString:urlAddress];
CGFloat width = [UIScreen mainScreen].bounds.size.width;
CGFloat height = [UIScreen mainScreen].bounds.size.height;
movie = [[MPMoviePlayerController alloc] initWithContentURL:url];
movie.scalingMode=MPMovieScalingModeAspectFill;
movie.view.frame = CGRectMake(0.0, 0.0, width, height);
[self.view addSubview:movie.view];
[movie play];

给我这个错误:

_itemFailedToPlayToEnd: {
kind = 1;
new = 2;
old = 0;
}

最佳答案

对我来说,这个库运行得非常完美! https://github.com/hellozimi/HCYoutubeParser

moviePlayer = [[MPMoviePlayerController alloc] init];    
moviePlayer.shouldAutoplay = YES;
moviePlayer.fullscreen = YES;
moviePlayer.repeatMode = MPMovieRepeatModeNone;
moviePlayer.controlStyle = MPMovieControlStyleDefault;
moviePlayer.movieSourceType = MPMovieSourceTypeFile;
moviePlayer.scalingMode = MPMovieScalingModeAspectFit;

在这个调用方法之后。

[self callYouTubeURL:[NSString stringWithFormat:@"http://www.youtube.com/embed/%@",_urlcode]];

在此方法中解析 youtube 链接。

- (void)callYouTubeURL:(NSString *)urlLink
{

NSURL *url = [NSURL URLWithString:urlLink];
actvity.hidden = NO;
[HCYoutubeParser thumbnailForYoutubeURL:url thumbnailSize:YouTubeThumbnailDefaultHighQuality completeBlock:^(UIImage *image, NSError *error) {

if (!error) {

[HCYoutubeParser h264videosWithYoutubeURL:url completeBlock:^(NSDictionary *videoDictionary, NSError *error) {


NSDictionary *qualities = videoDictionary;

NSString *URLString = nil;
if ([qualities objectForKey:@"small"] != nil) {
URLString = [qualities objectForKey:@"small"];
}
else if ([qualities objectForKey:@"live"] != nil) {
URLString = [qualities objectForKey:@"live"];
}
else {
[[[UIAlertView alloc] initWithTitle:@"Error" message:@"Couldn't find youtube video" delegate:nil cancelButtonTitle:@"Close" otherButtonTitles: nil] show];
return;
}
_urlToLoad = [NSURL URLWithString:URLString];


[self urlLoadintoPlayer];

}];
}
else {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[alert show];
}
}];
}

在此加载后,将新解析的 url 加载到播放器中。

-(void)urlLoadintoPlayer
{
moviePlayer.contentURL = _urlToLoad;
}

关于ios - 使用 MPMoviePlayerController 播放 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21198169/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com