2010-12-16 101 views
4

我想做一個youtube這樣的應用程序(Youtube Stream)http://itunes.apple.com/us/app/youtube-stream/id384383425?mt=8#爲iPhone的應用程序,其中一些視頻從YouTube上流式傳輸/下載並在沒有啓動iPhone的YouTube應用程序的同一應用程序中播放。 我搜索很多,找不到如何做it..can任何一個提出一個解決方案的線索......iphone:如何流和使用MPMoviePlayer在應用程序中播放的YouTube影片?

+0

磨憨,我在尋找相同的代碼。如果你發現任何東西,請告訴我。 – Biko 2011-02-21 14:02:57

回答

2

我發現流/播放YouTube視頻的iPhone應用程序內的一種方式,但我不知道蘋果會認識到它或它的根據youttube術語和矛盾。婁我附上我的.h和.m文件PLZ檢查,並說,它是如何工作..

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 *[email protected]"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 
+0

怎麼樣提取熱鏈接,MP4的法律問題 - 法律和蘋果商店兼容? – cDima 2012-06-03 04:20:52

1

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

我用PSYouTubeExtractor.h類由彼得·斯坦伯格它可以讓YouTube的MP4視頻網址,比下載和觀看是沒有問題的

NSURLConnection的+ NSNotificationCenter + PSYouTubeExtractor + NSMutableData

+0

我試過這種方法與iPhone和它工作得很好。 但它不是與iPad ..視頻播放,並且你可以聽到視頻的聲音,但YouTube播放器沒有顯示。 – Amit 2012-04-08 16:50:47

+0

@comonitos,@阿密特請幫忙..how到implemnt這個班..cz代碼不是兩個模擬器和設備工作 – Bajaj 2013-04-22 05:42:51

相關問題