OGeek|极客世界-中国程序员成长平台

标题: iphone - MPMoviePlayerController 播放 YouTube 视频 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-13 13:33
标题: iphone - MPMoviePlayerController 播放 YouTube 视频

如何在 iPhone 上的 MPMoviePlayerController 中播放 YouTube 视频,同时避免进入全屏模式?

这里提出了这个问题:MPMoviePlayerController is playing YouTube video?在这里:Play Youtube video in MPMoviePlayerController or play RTSP - 3GP link有答案声称这种功能是不可能的。

然而,Deja 这个应用程序具有我想要的功能:一个无缝的 MPMoviePlayerController,我可以显式控制它的框架。 http://itunes.apple.com/app/deja/id417625158

这是怎么做到的!?



Best Answer-推荐答案


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

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

#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)youtubeURLPathNSString*)youtubeURLPath extractStreamURLPathsWithCallbackCallbackBlock)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)youtubeURLPathNSString*)youtubeURLPath extractStreamURLPathsWithCallbackCallbackBlock)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

关于iphone - MPMoviePlayerController 播放 YouTube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10411250/






欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) Powered by Discuz! X3.4