gpt4 book ai didi

objective-c - 如何在 MPMoviePlayerController 中打开 Youtube 视频并播放

转载 作者:搜寻专家 更新时间:2023-10-30 20:10:31 25 4
gpt4 key购买 nike

这个方法我用过1次。但现在我无法从这个直接在 MPMoviePlayerController 中播放 YouTube 视频的 URL 获取视频。

网址 = http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded

- (void)viewDidLoad
{

player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://www.youtube.com/watch?v=JPUWNcGDyvM&feature=player_embedded"]]];

[[NSNotificationCenter defaultCenter]
addObserver:self
selector:@selector(movieFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];

//---play movie---
[player prepareToPlay];
[player pause];

player.view.frame = CGRectMake(0, 0, 320, 367);

[self.view addSubview:player.view];

[super viewDidLoad];

我的 movieFinishedCallback 就像在 ...

- (void) movieFinishedCallback:(NSNotification*) aNotification 
{
player = [aNotification object];
[[NSNotificationCenter defaultCenter]
removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:player];
}

最佳答案

创建自定义 uiwebview:请按照说明操作

1) 创建一个名为“YouTubeView.h”的接口(interface).h 文件

#import <UIKit/UIKit.h>

@interface YouTubeView : UIWebView
{
}

- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType;

@end

2) 到.m 文件

 #import "YouTubeView.h"

@implementation YouTubeView


- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame mimeSubType:(NSString *)mimeType
{
NSString *strMimeType;

if([mimeType length]>0)
{
strMimeType = mimeType;
}

else
{
strMimeType =@"x-shockwave-flash"; //@"x-shockwave-mp4";
}

if (self = [super init])
{
// Create webview with requested frame size
self = [[UIWebView alloc] initWithFrame:frame];

// HTML to embed YouTube video

NSString *youTubeVideoHTML = @"<html><head>\
<body style=\"margin:0\">\
<embed id=\"yt\" src=\"%@\" type=\"application/%@\" \
width=\"%0.0f\" height=\"%0.0f\"></embed>\
</body></html>";

// Populate HTML with the URL and requested frame size
NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString,strMimeType, frame.size.width, frame.size.height];

// Load the html into the webview
[self loadHTMLString:html baseURL:nil];
}

return self;

}


- (void)dealloc
{
[super dealloc];
}

@end

3) 将 youtubeview 文件导入到您要播放管视频的 Controller 中,并像下面的代码一样创建 uiview

YouTubeView *videoVw = [[YouTubeView alloc] initWithStringAsURL:[NSString 

stringWithFormat:@"%@",strNormalVdoURL] frame:CGRectMake(0,0,320,480) mimeSubType:@"x-shockwave-

flash"];

[self.view addSubview:videoVw];
[videoVw release];
videoVw = nil;

注意:视频将仅在设备中播放,因此请检查设备

关于objective-c - 如何在 MPMoviePlayerController 中打开 Youtube 视频并播放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11187577/

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