Sound in Xcode App

audio

(Frank) #1

Hello. When I create an Xcode app from my html exported Hype project, I’ve noticed in the Simulator that it will not play secondary timeline sounds. For instance, when the app loads, it plays the Scene-related audio when clicked but any other timeline sounds that are set to run will not play their accompanying sounds. Any suggestions here would be great!

Thanks for your help!


#2

This line might help? Autoplaying audio in a Webview (iOS)

You may need to do allow autoplaying sounds if you’re not starting the audio in response to a mouse click.


(Frank) #3

Thanks for the tip Daniel. I cannot figure it out. Can anyone help? That would be much appreciated. Thanks!

Xcode Project and Files:


#4

Sorry this is pretty far outside of my comfort zone…


(Mark Hunte) #5

Cannot test this but the code line would need to go after the load request as @nick points out.


(Frank) #6

Thanks Mark! I’ll give it a try.

Frank


(Frank) #7

:thinking:


#8

The reason here is that property is deprecated. You should look at

WKAudiovisualMediaTypes

perhaps:

WKAudiovisualMediaTypeNone = 0

(Mark Hunte) #9

In that project the Main storyboard is missing. The one in the project folder is corrupt !!

So cannot test your set up. But as @DBear indicates

And I should have remembered this cropped up before in a couple of thread.
And I have it in my template for Hype-Xcode projects…

See


(Frank) #10

Hello,

Here is my current code that loads without error but no change in the sound. I notice that Xcode Simulator will start the Scene Timeline sound on click but no other timeline sounds will automatically start as the scene asks it to. Thanks!

Code…

import WebKit
//import UIKit

class ViewController: UIViewController, WKNavigationDelegate {
    override func viewDidLoad() {
        super.viewDidLoad()
        let webView = WKWebView()
        let htmlPath = Bundle.main.path(forResource: "LMS_Ver2", ofType: "html")
        let folderPath = Bundle.main.bundlePath
        let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
        let wc = WKWebViewConfiguration()
        wc.allowsInlineMediaPlayback = true
        wc.mediaTypesRequiringUserActionForPlayback = []
        do {
            let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
            webView.loadHTMLString(htmlString as String, baseURL: baseUrl)
        } catch {
            // catch error
        }
        webView.navigationDelegate = self
        view = webView
        }

}

(Mark Hunte) #11

The main problem is that you do not understand enough about swift and how the bits of code you are using are used together.

Most of the code is correct. But …

You have initiated and set a WKWebViewConfiguration but then not used it.
The WKWebView needs to be configured using the configuration you just made.
This is normally done when the WKWebView itself is initialised.

webView = WKWebView(frame: .zero, configuration: wc)

Since we are initialising the WKWebView this way with arguments instead.

we need to change the line let webView = WKWebView()

to var webView: WKWebView!

Then after we set up the WKWebViewConfiguration we can do the WKWebView initialising.
with the webView = WKWebView(frame: .zero, configuration: wc)
line

  override func viewDidLoad() {
        super.viewDidLoad()
        var webView: WKWebView!
        let htmlPath = Bundle.main.path(forResource: "LMS_Ver2", ofType: "html")
        let folderPath = Bundle.main.bundlePath
        let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true)
        let wc = WKWebViewConfiguration()
        wc.allowsInlineMediaPlayback = true
        wc.mediaTypesRequiringUserActionForPlayback = []
        
         webView = WKWebView(frame: .zero, configuration: wc)
        
        do {
            let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue)
            webView.loadHTMLString(htmlString as String, baseURL: baseUrl)
        } catch {
            // catch error
        }
        webView.navigationDelegate = self
        
        view = webView
    }

I would suggest though that you do read the Swift docs on the frameworks you are trying to use and maybe go through some tutorials.


(Frank) #12

Hi Mark,

These additions seemed to completely resolve my sound issues! Thank you so much for your help!

Frank