iPhone:如何使用MPMoviePlayer在应用程序中流式播放YouTube视频?

4

我想制作一个类似于Youtube Stream的应用程序(http://itunes.apple.com/us/app/youtube-stream/id384383425?mt=8#),专门为iPhone设计,其中一些视频可以从YouTube流式传输/下载,并在同一应用程序中播放,而不必启动iphone的YouTube应用程序。

我搜索了很多但是找不到如何做到这一点的线索...有人能提供解决方案吗?


Mohan,我正在寻找相同的代码。如果您找到了什么,请告诉我。 - Biko
2个回答

2
我发现了一种在iPhone应用内播放/流式传输YouTube视频的方法,但我不知道苹果是否会承认它或者它是否符合YouTube条款和条件。下面我附上我的.h和.m文件,请检查并告诉我它是如何工作的。 YoutubePlayerViewController.h
#import<UIKit/UIKit.h>
#import<MediaPlayer/MediaPlayer.h>

@interface YoutubePlayerViewController : UIViewController
{
    UITextField *yurl;  
    NSMutableData *responseData;
    NSString *cacheLink;
    MPMoviePlayerController *moviePlayer;

}

@property(nonatomic,retain)IBOutlet UITextField *yurl;
@property(nonatomic,retain)NSString *cacheLink;

-(IBAction)Play:(id)sender;
-(IBAction)removeKeyboard;
@end 

enter image description here

//--------------------------------------------------------------------------------

//YoutubePlayerViewController.m

#import "YoutubePlayerViewController.h"

@implementation YoutubePlayerViewController
@synthesize yurl,cacheLink;


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    NSLog(@"view did load");
}


-(IBAction)removeKeyboard
{

    [yurl resignFirstResponder];

}

-(IBAction)Play:(id)sender
{

    //1.get the url
    NSString *url=yurl.text;
    //NSString *url=@"http://www.youtube.com/watch?v=t2o5MhaSWRs";
    //2.show loding view

    //3.make http request
    responseData = [[NSMutableData data] retain];
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
    [[NSURLConnection alloc] initWithRequest:request delegate:self];





    //3.parse jason string for itag=18
    //5.create an NSURL with that string
    //6.start the player with url

}



- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    NSLog(@"did receving response");
    [responseData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
    //NSLog(@"receving data");
    [responseData appendData:data];

}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{
    NSLog(@"Connection failed: %@", [error description]);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
    NSLog(@"%d",responseData.length);
    NSString* strServerResponse= [[NSString alloc]initWithData:responseData encoding:NSASCIIStringEncoding];

    NSLog(@"%@",strServerResponse);
    NSLog(@"\n***********************************************\n");

    NSArray *temp=[strServerResponse componentsSeparatedByString:@"swfConfig"];
    strServerResponse=[temp objectAtIndex:1];

    temp=[strServerResponse componentsSeparatedByString:@".c.youtube.com,18|"];
    strServerResponse=[temp objectAtIndex:1];

    temp=[strServerResponse componentsSeparatedByString:@"||"];
    strServerResponse=[temp objectAtIndex:0];

    strServerResponse=[strServerResponse stringByReplacingOccurrencesOfString:@"\\" withString:@""];
    NSLog(@"%@",strServerResponse);
    self.cacheLink=strServerResponse;


    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"link" message:self.cacheLink delegate:self cancelButtonTitle:@"ok" otherButtonTitles:nil];
    [alert show];
    [alert release];

    NSURL *url=[[NSURL alloc] initWithString:self.cacheLink];
    moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:url];
    [self.view addSubview:moviePlayer.view];
    moviePlayer.view.frame = CGRectMake(5,150,310,230);  
    moviePlayer.view.backgroundColor=[UIColor blackColor];
    [moviePlayer play]; 

    [connection release];
}              


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


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

@end

提取 mp4 的热链接涉及到法律问题 - 是否合法且符合苹果应用商店的规定? - Dmitry Sadakov

1

很容易 这是我的例子:https://github.com/comonitos/youtube_video

我使用了Peter Steinberger的PSYouTubeExtractor.h类,它可以获取YouTube MP4视频的URL,然后下载和查看都不是问题

NSURLConnection + NSNotificationCenter + PSYouTubeExtractor + NSMutableData


我用 iPhone 尝试了这种方法,效果很好。 但是在 iPad 上不起作用... 视频可以播放,声音也能听到,但是 YouTube 播放器没有显示出来。 - Amit
@comonitos,@amit请帮忙...如何实现这些类...因为代码在模拟器和设备上都不起作用。 - Bajaj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接