Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube video embedded in UIWebView has no sound on real device

This is how I embed my video. It has sound in the simulator, however has no sound on a real device (iOS 9.0.2 (13A452)) Xcode 7.1.1.

This is the code I use to play it:

if let youtubeID = youtubeID {
    let embededHTML = "<html><body><iframe src=\"http://www.youtube.com/embed/\(youtubeID)?playsinline=1\" width=\"\(width)\" height=\"\(height)\" frameborder=\"0\" allowfullscreen></iframe></body></html>"
    cell!.webView.allowsInlineMediaPlayback = true
    cell!.webView.scalesPageToFit = true
    cell!.webView.mediaPlaybackAllowsAirPlay = true
    cell!.webView.mediaPlaybackRequiresUserAction = false

    cell!.webView.loadHTMLString(embededHTML, baseURL: NSBundle.mainBundle().bundleURL)
}
like image 610
Timm Kent Avatar asked Nov 27 '22 13:11

Timm Kent


2 Answers

Most developers come across this issue they forget to
switch on the ringer button on the left side of your iPhone. It worked for me.

like image 62
Jincy Varughese Avatar answered Nov 30 '22 03:11

Jincy Varughese


SWIFT 2.0 version I have the same problem for me the solution was to add in the AppDelegate

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


    do {
        try AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback)
    }
    catch let error as NSError {
        print(error)  
    }

    do {
        try AVAudioSession.sharedInstance().setActive(true)
    }
    catch let error as NSError {
        print(error)  
    }

    // Override point for customization after application launch.
    return true
}

This enable sound in my UIWebView

like image 29
Aaleks Avatar answered Nov 30 '22 03:11

Aaleks