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.

  • How to set NSString's background cornerRadius on iOS7
  • iOS: How do I know if a property is KVO-compliant?
  • Objective C code base will work on Swift directly?
  • How to properly handle a nil UIApplication.sharedApplication().keyWindow
  • Change brightness of an image via uislider and gpuimage filter
  • Add custom Cell to JKExpandtableview as Child Cell in iOS
  • 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];