Issue with not getting sound in device when play youtube video in UIWebView in IOS

冷暖自知 提交于 2019-12-03 23:33:36

问题


I am using a UIWebView to play Yoytube Video.
My code is like that

self.webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0,320, 240)];
[self.webView setBackgroundColor:[UIColor clearColor]];
[self.webView setOpaque:NO];
self.webView.delegate=self;
[self.webView setAllowsInlineMediaPlayback:YES];
[self.webView setMediaPlaybackRequiresUserAction:NO];
[self.viewContent addSubview:self.webView];

NSString* embedHTML = [NSString stringWithFormat:@"\
                                   <html>\
                                   <body style='margin:0px;padding:0px;'>\
                                   <script type='text/javascript' src='http://www.youtube.com/iframe_api'></script>\
                                   <script type='text/javascript'>\
                                   function onYouTubeIframeAPIReady()\
                                   {\
                                   ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
                                   }\
                                   function onPlayerReady(a)\
                                   { \
                                   a.target.playVideo(); \
                                   }\
                                   </script>\
                                   <iframe id='playerId' type='text/html' width='%d' height='%d' src='http://www.youtube.com/embed/%@?enablejsapi=1&rel=0&playsinline=1&autoplay=1' frameborder='0'>\
                                   </body>\
                                   </html>", 320, 240, [dictIW valueForKey:@"embedCode"]];
[self.webView loadHTMLString:embedHTML baseURL:[[NSBundle mainBundle] resourceURL]];

In Simulator it is playing absolutely fine. But when I play this video in iPhone or iPad, the video is showing, but the sound is not getting.
please help.


回答1:


Set the audio session category to 'playback' before creating the UIWebView

AVAudioSession *audioSession = [AVAudioSession sharedInstance];
BOOL flag;
NSError *setCategoryError = nil;
flag = [audioSession setCategory:AVAudioSessionCategoryPlayback
                     error:&setCategoryError];



回答2:


do{
    try AVAudioSession.sharedInstance().setCategory(.playback, mode: .moviePlayback, options: .defaultToSpeaker)
} catch let error {
    print("Error: \(error)")
}

in your view did load, if using swift.



来源:https://stackoverflow.com/questions/23288765/issue-with-not-getting-sound-in-device-when-play-youtube-video-in-uiwebview-in-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!