MPMoviePlayerController播放YouTube视频 [英] MPMoviePlayerController playing YouTube video
问题描述
如何在iPhone上的MPMoviePlayerController中播放YouTube视频,同时避免进入全屏模式?
How can I play a YouTube video in an MPMoviePlayerController on the iPhone while avoiding going into fullscreen mode?
此问题已在此处提出:MPMoviePlayerController正在播放YouTube视频?并在此处:在MPMoviePlayerController中播放Youtube视频或播放RTSP-3GP链接,答案声称此类功能是不可能的。
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.
然而,这个应用程序,Deja,具有我想要的功能:一个无缝的MPMoviePlayerController,其框架我有明确的控制权。 http://itunes.apple.com/app/deja/id417625158
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
这是怎么做到的??
推荐答案
将此示例添加到项目$中b $ b实例化 YoutubeStreamPathExtractorTest
add this sample into you project instantiate YoutubeStreamPathExtractorTest
调用 YoutubeStreamPathExtractorTest <的 测试 方法/ strong>实例。
关注日志并开心
invoke test method of YoutubeStreamPathExtractorTest instance. Follow logs and be happy
#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
这篇关于MPMoviePlayerController播放YouTube视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!