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

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.

  • YTPlayerView youtube-ios-player-helper pause not working
  • Making youtube.com/embed URLs work on iOS
  • How to get the url of currently playing video in UIWebview
  • Setting snippet data for youtube upload via REST API using Swift
  • How do videos autoplay on iOS at youtube.com
  • iOS open YouTube App with query (url schemes)
  • Solutions Collect From Internet About “Issue with not getting sound in device when play youtube video in UIWebView in IOS”

    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];