dejix
dejix

Reputation: 1144

No sound on UIWebView iOS10

I load on UIWebView a HTML game from local files. iOS 10.2 / XCode 8 / Swift 3

    override func viewDidLoad() {
        super.viewDidLoad()
            if let path = Bundle.main.path(forResource: "index", ofType: "html", inDirectory: "game") {
            webView.loadRequest(URLRequest(url : URL(fileURLWithPath: path)))
        }
    }

Everything seems fine, but I cant hear any sound on game.

I open the same files (game) from localhost server via Mobile's Safari browser and sound seems OK. So i'm sure that html/javascript files & connection is OK and issue has to do with iOS.

Added "Required background modes" -- > "App plays audio or streams audio/video" in info.plist but nothing changed. No sound on device.

Upvotes: 1

Views: 1262

Answers (2)

dejix
dejix

Reputation: 1144

I answer my own question just in case someone has the same problem. I found a solution, by running my app on a localserver. I used GCDWebServer and everything went OK.

Not sure why happened this sound issue and solved by that way, but i suppose it's a matter of javascript "security" on iOS or something similar.

This post game me the answer.

Upvotes: 1

Andrii Kuzminskyi
Andrii Kuzminskyi

Reputation: 171

You should run this line before webview.loadRequest(...)

AVAudioSession.sharedInstance().setCategory(AVAudioSessionCategoryPlayback, error: nil)

Upvotes: 1

Related Questions