iPhone:如何使用MPMoviePlayer在应用程序中流式传输和播放YouTube视频? [英] iphone:How to stream and play youtube video with in application using MPMoviePlayer?
问题描述
我想制作一个像这样的youtube应用程序(Youtube Stream) http://itunes.apple.com/us/app/youtube-stream/id384383425?mt=8# 适用于iphone的应用,其中一些视频是从YouTube播放/下载并在同一个应用中播放的没有启动iphone的youtube应用程序..
我搜索很多,但无法找到如何做到的线索..任何人都可以建议解决方案......
I want to make a youtube app like this (Youtube Stream) http://itunes.apple.com/us/app/youtube-stream/id384383425?mt=8# app for the iphone, where some videos are streamed/downloaded from a youtube and played within same app without launching iphone's youtube app.. I searched lot but couldn't find a clue on how to do it..can any one suggest a solution...
推荐答案
我找到了一种在iphone应用程序中播放/播放youtube视频的方法,但我不知道苹果会在youttube条款和条件下识别它。贝娄我附上我的.h和.m文件plz检查并说明它是如何工作的..
I found a way to stream/play youtube video inside iphone app, but i don't know apple will recognize it or its under youttube terms and condictions. Bellow i attach my .h and .m file plz check and say how it works..
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
// ------------------ -------------------------------------------------- ------------
//--------------------------------------------------------------------------------
// YoutubePlayerViewController.m
//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
这篇关于iPhone:如何使用MPMoviePlayer在应用程序中流式传输和播放YouTube视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!