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 setAllowsInlineMediaPlayback:YES];
[self.webView setMediaPlaybackRequiresUserAction:NO];
[self.viewContent addSubview:self.webView];

NSString* embedHTML = [NSString stringWithFormat:@"\
                                   <body style='margin:0px;padding:0px;'>\
                                   <script type='text/javascript' src=''></script>\
                                   <script type='text/javascript'>\
                                   function onYouTubeIframeAPIReady()\
                                   ytplayer=new YT.Player('playerId',{events:{onReady:onPlayerReady}})\
                                   function onPlayerReady(a)\
                                   { \
                         ; \
                                   <iframe id='playerId' type='text/html' width='%d' height='%d' src='' frameborder='0'>\
                                   </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 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
  • 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