Start audio automatically on iOS app

ios

(julian) #1

I know it’s possible since I’ve seen it in a few Hype-created apps, where audio starts as soon as the app launches without having to tap on any button. I’m assuming it’s done with some java library. If anyone can point me in the right direction?

Thanks.

JD


Audio not playing in scenes
#2

Actions can be triggered on scene load.
http://tumult.com/hype/documentation/3.0/#scene-actions


(Jonathan Deutsch) #3

This requires configuring the UIWebView or WKWebView of a native within the app properly to make sure it can autoplay audio. It isn’t a java library but how you program the app in Objective-C or Swift. The basic (and most modern) steps are:

  1. Create a WKWebViewConfiguration object
  2. Set the mediaTypesRequiringUserActionForPlayback property to WKAudiovisualMediaTypeNone
  3. Initialize the WKWebView with the configuration object

Then in Hype audio can be played on actions that don’t require user actions, like the mentioned ‘On Scene Load’ action.


(julian) #4

Thank you for your very helpful reply, as always!


(erik) #5

This is what I am looking for. But I don’t understand how to add this to my project. Can you help me? best regards, Erik


(Jonathan Deutsch) #6

There’s a lot of moving parts and questions when programming a native app, so it is hard to help specifically more than the suggestion I gave without more details on your project… Here is sample Objective-C code showing how it is setup:

WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
[configuration setAllowsInlineMediaPlayback: YES];
[configuration setMediaTypesRequiringUserActionForPlayback:WKAudiovisualMediaTypeNone];
	
CGRect webViewFrame = [[self view] bounds];
WKWebView *webView = [[WKWebView alloc] initWithFrame:webViewFrame configuration:configuration];
[self.view insertSubview:webView atIndex:0];

This can’t be copied/pasted directly, since it relies on knowledge of a [self view] and also probably a lot of things on what you want done with the webView.


#7

If you want sound “on scene load” you can just put that in Hype. It works when building the app in Xcode both on iPad en iPhone.
If you want sound triggered by a timeline (I have tried that before, but did not work), then you probably need the code Jonathan showed you.


(erik) #8

Hi, my app where build from this tut (Tutorial). My code looks like this:

import UIKit
import WebKit

class ViewController: UIViewController, UITextFieldDelegate, WKNavigationDelegate {
    
    var webView: WKWebView
    @IBOutlet weak var barView: UIView!
    @IBOutlet weak var urlField: UITextField!
    @IBOutlet weak var progressView: UIProgressView!
    
    required init?(coder aDecoder: NSCoder) {
        self.webView = WKWebView(frame: CGRectZero)
        super.init(coder: aDecoder)
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.webView.backgroundColor = UIColor.clearColor()
        self.webView.scrollView.backgroundColor = UIColor.clearColor()
        view.insertSubview(webView, belowSubview: progressView)
        
        webView.translatesAutoresizingMaskIntoConstraints = false
        let height = NSLayoutConstraint(item: webView, attribute: .Height, relatedBy: .Equal, toItem: view, attribute: .Height, multiplier: 1, constant: -20)
        let width = NSLayoutConstraint(item: webView, attribute: .Width, relatedBy: .Equal, toItem: view, attribute: .Width, multiplier: 1, constant: 0)
        view.addConstraints([height, width])
        
        webView.addObserver(self, forKeyPath: "loading", options: .New, context: nil)
        webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
        
        var locale = NSLocale.preferredLanguages()[0]
        if (locale.containsString("-")) {
            locale = locale.substringToIndex(locale.startIndex.advancedBy(2))
        }
        let url = NSURL(string:"https://test.url/?lang=" + locale)
        let request = NSURLRequest(URL:url!)
        webView.loadRequest(request)
        
        view.bounds = CGRectInset(view.frame, 0, -20)
        progressView.bounds = CGRectInset(progressView.bounds, 0, -30)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [String : AnyObject]?, context: UnsafeMutablePointer<()>) {

        if (keyPath == "estimatedProgress") {
            progressView.hidden = webView.estimatedProgress == 1
            progressView.setProgress(Float(webView.estimatedProgress), animated: true)
        }
    }
 }

Any ide how to add the code to this? I am super new to Swift, sorry about that.


(Jonathan Deutsch) #9

I don’t know swift either! But you’d basically want your init method to be something like:

    required init?(coder aDecoder: NSCoder) {
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.allowsInlineMediaPlayback = true
        webConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone
        self.webView = WKWebView(frame: CGRectZero, configuration: webConfiguration)
        super.init(coder: aDecoder)
    }

No idea if this will work :slight_smile:.


#10

This is an old thread, but if you’re still having trouble with audio playing automatically, maybe this post will help…

Instead of…
webConfiguration.mediaTypesRequiringUserActionForPlayback = WKAudiovisualMediaTypeNone

Try using…
webConfiguration.mediaTypesRequiringUserActionForPlayback = []