使用LBYouTube在UIView中打开YouTube视频

时间:2014-01-04 19:43:04

标签: ios iphone objective-c xcode youtube

我是新编码员,在使用以下资源在我的应用中播放YouTube视频时遇到问题

https://github.com/larcus94/LBYouTubeView

我已经打开了示例代码,但在他们的示例代码中,它会在应用启动时加载视频,但我不希望它完全执行此操作。我想在特定的视图中加载视频,甚至可能在一个框架中,但我似乎无法做到这一点

有任何指示或帮助吗?我可以提供您可能需要的任何资源

1 个答案:

答案 0 :(得分:1)

在你的应用程序中,在你的笔尖上添加一个UIView,并将此视图的大小设置为你想要视频播放器的大小,然后将其连接到你的代码,调用视图“viewForYouTube”,@在S中合成它。 ViewController的文件。

你的.h文件应该是这样的:

#import <UIKit/UIKit.h>
#import "LBYouTube.h"

@interface YourViewController : UIViewController <LBYouTubePlayerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *viewForYouTube;

@end

然后将此代码添加到.m文件中:

- (void)viewDidAppear:(BOOL)animated
{
    // Setup the player controller and add it's view as a subview:
    LBYouTubePlayerViewController* controller = [[LBYouTubePlayerViewController alloc] initWithYouTubeURL:[NSURL URLWithString:@"http://www.youtube.com/watch?v=1fTIhC1WSew&list=FLEYfH4kbq85W_CiOTuSjf8w&feature=mh_lolz"] quality:LBYouTubeVideoQualityLarge];
    controller.delegate = self;
    [controller.view setFrame:CGRectMake(0, 0, viewForYouTube.frame.size.width, viewForYouTube.frame.size.height)];
    [viewForYouTube addSubview:controller.view];
}

#pragma mark LBYouTubePlayerViewControllerDelegate

-(void)youTubePlayerViewController:(LBYouTubePlayerViewController *)controller didSuccessfullyExtractYouTubeURL:(NSURL *)videoURL {
    NSLog(@"Did extract video source:%@", videoURL);
}

-(void)youTubePlayerViewController:(LBYouTubePlayerViewController *)controller failedExtractingYouTubeURLWithError:(NSError *)error {
    NSLog(@"Failed loading video due to error:%@", error);
}