【问题标题】:Embedding Youtube videos in iOS [duplicate]在 iOS 中嵌入 Youtube 视频 [重复]
【发布时间】:2011-08-20 20:43:39
【问题描述】:

可能重复:
Embedding YouTube videos on iOS

我正在尝试在 iOS 上嵌入 youtube 视频,这样当用户单击按钮时,youtube 视频会在不离开应用程序的情况下显示,并且一旦视频完成,用户就会返回到应用程序。我在网上找到了一个教程,展示了如何做到这一点,我跟着它。我创建了一个 YouTubeView:

#import "YouTubeView.h"


@implementation YouTubeView

- (YouTubeView *)initWithStringAsURL:(NSString *)urlString frame:(CGRect)frame {
    self = [super init];
    if (self) 
    {
        // Create webview with requested frame size
        self = [[UIWebView alloc] initWithFrame:frame];

        // HTML to embed YouTube video
        NSString *youTubeVideoHTML = @"<html><head>\
        <body style=\"margin:0\">\
        <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
        width=\"%0.0f\" height=\"%0.0f\"></embed>\
        </body></html>";

        // Populate HTML with the URL and requested frame size
        NSString *html = [NSString stringWithFormat:youTubeVideoHTML, urlString, frame.size.width, frame.size.height];

        // Load the html into the webview
        [self loadHTMLString:html baseURL:nil];
    }
    return self;  
}

#pragma mark -
#pragma mark Cleanup

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

@end

我正在像这样实例化它:(案例 2)

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
    SongInfoTableVC *infoVC;
    YouTubeView *youTubeView;
    switch (buttonIndex) {
        case 0:
            NSLog(@"hello");
            [self setLyrics];
            break;
        case 1:
            infoVC = [[SongInfoTableVC alloc] initWithStyle:UITableViewStyleGrouped];
            [infoVC setBook:data];
            [infoVC setTitle:@"Song Info"];
            [[self navigationController] pushViewController:infoVC animated:YES];
            [infoVC release];
            break;
        case 2:
            youTubeView = [[YouTubeView alloc] 
                                        initWithStringAsURL:@"http://www.youtube.com/watch?v=xli2E2i8GZ4" 
                                        frame:CGRectMake(0, 0, 320, 480)];

            [[[self navigationController] view] addSubview:youTubeView];
            break;
        default:
            break;
    }
}

但这会带来一个空白的白色屏幕。我究竟做错了什么? 这是屏幕截图:

谢谢

【问题讨论】:

    标签: cocoa-touch ios uiwebview uiviewcontroller youtube


    【解决方案1】:

    尝试在设备上进行测试。我认为模拟器不支持这种方法。

    请看这里:Embedding YouTube videos on

    【讨论】:

    • 谢谢,我在设备上试过了,它在那里工作!
    • 您好,我遇到了同样的问题,现在我成功在设备中播放视频,但是如果我想在 ios 版本 5.1 以下播放视频,还有另一个问题是视频只能在 ios 版本 5.1.1 中播放.1 我该怎么办?
    猜你喜欢
    • 2014-11-10
    • 2013-09-23
    • 1970-01-01
    • 2021-04-23
    • 2013-10-07
    • 2013-12-27
    • 2014-04-16
    • 2016-11-26
    • 2021-12-04
    相关资源
    最近更新 更多