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

标签 objective-c ios4 mpmovieplayercontroller

这个方法我用过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/

相关文章:

objective-c - 测试按钮标题的正确公式是什么?

ios - MPMoviePlayerController 和基于 Auth 的 HLS 后端服务器

swift - 来自 MPMoviePlayerController 的音频在转至下一个 View Controller 后继续播放

iphone - MPMoviePlayerController 和 .m3u8 播放列表

iphone - 如何解决我的应用程序中的错误 EXC_BAD_ACCESS?

ios - 在 UIViewController 中填充 UITableView 并使用 UISegmentedControl 更改数据源

iphone - 在 iOS 中绘制 NSAttributedString?

ios - 我可以在我的应用程序中获取其他应用程序的打开关闭回调吗

iPhone:如何使 UITextField、UITextView 在切换 View 时不可编辑

ios - TableView 不会从 Parse.com 重新加载数据