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!
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
}
}
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.
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.