2

How can I play a YouTube video in an MPMoviePlayerController on the iPhone while avoiding going into fullscreen mode?

This question has been raised here: MPMoviePlayerController is playing YouTube video? and here: Play Youtube video in MPMoviePlayerController or play RTSP - 3GP link with answers claiming such functionality was impossible.

Yet this app, Deja, has exactly the functionality I would like: a seamless MPMoviePlayerController whose frame I have explicit control over. http://itunes.apple.com/app/deja/id417625158

How is this done!?

4

5 回答 5

2

将此示例添加到您的项目中实例化YoutubeStreamPathExtractorTest

调用YoutubeStreamPathExtractorTest实例的测试方法。关注日志并快乐

#import "AFHTTPRequestOperationManager.h"
#import <MediaPlayer/MediaPlayer.h>

typedef void (^CallbackBlock)(NSArray* result, NSError* error);
static NSString* const kYouTubeStreamPathPattern = @"\\\"url_encoded_fmt_stream_map\\\\\":.*?url=(.*?)\\\\u0026";

@interface YoutubeStreamPathExtractorTest : NSObject
- (void)test;
- (void)youtubeURLPath:(NSString*)youtubeURLPath extractStreamURLPathsWithCallback:(CallbackBlock)callback;
@end

@implementation YoutubeStreamPathExtractorTest

- (void) test {
    NSString* path = @"http://www.youtube.com/watch?v=TEV5DZpAXSw";
    [self youtubeURLPath:path extractStreamURLPathsWithCallback:^(NSArray *result, NSError *error) {
        if (error){
            NSLog(@"extracting error:%@",[error localizedDescription]);
        }
        for(NSString* streamURLPath in result) {

            NSLog(@"streamURLPath:%@",streamURLPath);

            /*
                NSURL* url = [NSURL URLWithString:streamURLPath];
                MPMoviePlayerController* mpMoviePlayerController_ = [[MPMoviePlayerController alloc] initWithContentURL:url];
                mpMoviePlayerController_.controlStyle = MPMovieControlStyleDefault;
                [mpMoviePlayerController_ play];
                */

        }
    }];
}

- (void)youtubeURLPath:(NSString*)youtubeURLPath extractStreamURLPathsWithCallback:(CallbackBlock)callback {
    __block NSMutableArray* resultArray = [NSMutableArray new];
    AFHTTPRequestOperationManager* manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:nil];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html", nil];
    [manager GET:youtubeURLPath
      parameters:nil
         success:^(AFHTTPRequestOperation* operation, id responseObject) {
             NSData* data = (NSData*)responseObject;
             NSString* string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];


             NSError* error = nil;
             NSRegularExpression* expression = [NSRegularExpression regularExpressionWithPattern:kYouTubeStreamPathPattern
                                                                                         options:NSRegularExpressionCaseInsensitive
                                                                                           error:&error];

             NSRange range = NSMakeRange(0,[string length]);

             NSArray* matches =  [expression matchesInString:string options:0 range:range];

             for(NSTextCheckingResult* checkingResult in matches) {
                 if ([checkingResult numberOfRanges]>1){
                     NSString* resultStr = [string substringWithRange:[checkingResult rangeAtIndex:1]];
                     //remove extra slashes
                     [resultArray addObject:[resultStr stringByReplacingOccurrencesOfString:@"\\" withString:@""]];
                 }
             }

             if (callback) {
                 callback(resultArray,error);
             }

         } failure:^(AFHTTPRequestOperation* operation, NSError* error) {
             if (callback) {
                 callback(resultArray, error);
             }
         }];


}
@end
于 2013-11-16T11:28:35.880 回答
1

MPMoviePlayerController不支持播放 YouTube SWF (Flash) 视频,句号。

您提到的那个应用程序实际上以 MP4 格式播放逐步下载的文件,YouTube 也为其某些内容提供这些文件。这实际上违反了 Apple 的指导方针,因为它将(并且确实)超过了每个应用程序每个时间段的最大渐进式下载量。我很惊讶它通过了 iTunes 的批准。

警告:提交在 App Store 中分发的 iOS 应用程序必须符合这些要求。如果您的应用通过蜂窝网络传输视频,并且视频在 5 分钟内超过 10 分钟的持续时间或超过 5 MB 的数据,则您需要使用 HTTP 实时流式传输。(渐进式下载可用于较小的剪辑。)

如果您的应用通过蜂窝网络使用 HTTP 实时流媒体,您需要提供至少一个 64 Kbps 或更低带宽的流(低带宽流可能是纯音频或带有静止图像的音频)。

这些要求适用于提交在 App Store 中分发以用于 Apple 产品的 iOS 应用程序。Apple 可自行决定拒绝或删除不合规的应用程序。

因此,您的任务归结为有关如何获取通过 YouTube 提供的视频的 MP4 URL 的问题。这部分真的很棘手,Deja 很好地解决了这个问题。只需使用数据包嗅探器,您就会看到它实际上创建了一个为 MPMoviePlayerController 提供数据的本地服务器。

于 2012-05-03T00:05:25.800 回答
0

我想这违反了 Youtube ToS,但您可以在此处使用此代码:

https://github.com/larcus94/LBYouTubeView

它使用简单,就像一个魅力!

于 2013-08-15T14:53:11.977 回答
0

试试这个代码:

NSString *urlStr=[Your url is here];
NSURL *url = [NSURL fileURLWithPath:urlStr];
MPMoviePlayerController*  moviePlayer = [[MPMoviePlayerController alloc]initWithContentURL:url];
[self.view addSubview:moviePlayer.view];
moviePlayer.view.frame = CGRectMake(set frame is here);  
[moviePlayer play];

[moviePlayer setFullscreen:NO animated:YES];


[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(moviePlayBackDidFinish) 
                                             name:MPMoviePlayerPlaybackDidFinishNotification 
                                           object:nil];
于 2012-05-02T09:53:54.610 回答
0

Use UIWebView. Copy HtML code video in youtube.

UIWevView* movie = [UIWebView alloc] initWithFrame:CGRectMake(0,0,320,460)];

NSString* urlString = @"past HTML code";
[self.webView loadHTMLString:urlString baseURL:nil];

[self.view addSubview:movie];
于 2013-08-10T16:07:27.283 回答