如何加载页面并在UIWebView中自动播放声音?

时间:2014-06-17 05:28:00

标签: ios objective-c uiwebview

我创建了一个ViewController,简单地命名为StreamViewController.m,我有这个代码:

    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

和:

- (IBAction)play:(id)sender {

        NSString *stream = @"https://soundcloud.com/aviciiofficial/avicii-wake-me-up-extended-mix";

        NSURL *url = [NSURL URLWithString:stream];

        NSURLRequest *request = [NSURLRequest requestWithURL:url];

        [_music loadRequest:request];

    }

我现在要做的是分配一些代码,以便当我点击进入View Controller时,流会自动打开并播放。现在,这是一个很烦人的步骤。首先加载页面,然后我必须单击按钮甚至打开URL(在它只显示空白/白页之前),最后我必须单击播放。有谁知道这个伎俩?

2 个答案:

答案 0 :(得分:0)

- (void) viewDidAppear{
    [super viewDidAppear];
    [self play:nil];
}

答案 1 :(得分:0)

我认为你不想自动播放Webview ..如果我正确则使用此代码..它会正常工作

NSString *stream = @"https://soundcloud.com/aviciiofficial/avicii-wake-me-up-extended-mix";

            NSString *embedHTML = @"\
            <html><head>\
            <style type=\"text/css\">\
            body {\
            background-color: transparent;  color: white;  }\
            </style>\
            </head><body style=\"margin:0\">\
            <embed id=\"yt\" src=\"%@\" type=\"application/x-shockwave-flash\" \
            width=\"%0.0f\" height=\"%0.0f\"></embed>\
            </body></html>";


        NSString *html = [NSString stringWithFormat:embedHTML, stream, _music.frame.size.width*3, _music.frame.size.height*3];

        [_music loadHTMLString:html baseURL:nil];

谢谢.. :))