TVML vs Native Audio Streaming.

I have an (TVMLKit) app that plays audio streams. Upon exiting the app the audio stream ends. I have seen apps that allow continous audio to stream continuously even upon exiting the app. I am not 100% certain but I believe those apps are native and use the avaudioplayer. Is it possible for a TVML/TVJS app (Uses Player Object: https://developer.apple.com/library/tvos/documentation/TVMLJS/Reference/TVJSPlayer_Ref/index.html#//apple_ref/javascript/instp/Player/stateDidChange)

to play a continuous audio stream? In other words, I want to play an audio stream from the app (music) and then exit and navigate the AppleTV menu while still hearing my audio selection. If it is possible, how is that accomplished?

Replies

Did you find an answer in the meantime? I have the exact same issue.


Usually, in order to get background audio to work you have to do the following:

- Enable 'Required Background Modes' in Info.plist and item 0 must read: 'App plays audio or streams audio/video using AirPlay'


However this doesn't seem to work in my TVML app also which also leads me to believe that these are native apps. Would love to know if anyone managed to get this to work in a TVML app

Thanks Jermain150! I still have not been able to get this to work. I am kind of lost. I have a client that is pushing to get this done and I am in a real bind.

Check my reply history, since a lot of the questions on here can be answered with a variation on one or two solutions. There may be code that's helpful. If you're seeing background audio on any third party TVOS app, then it's possible, and you'll need to trigger the native player from JavaScript.


In app delegate:


func appController(appController: TVApplicationController, evaluateAppJavaScriptInContext jsContext: JSContext) {
        let playAudioURL: @convention(block) (String) -> Void = {
            (name: String) -> Void in
            // configure native player here...
        }

        jsContext.setObject(unsafeBitCast(playAudioURL, AnyObject.self), forKeyedSubscript:"playAudioURL");
}

Then in JS:

playAudioURL("http://whatever.example");

Please look at following thread: https://forums.developer.apple.com/thread/24072

Hello Nurinder,


Your link applies to a native app that uses the avaudioplayer. However, the question is if it's also possible within an app that's using javascript version of the Player. See the following TVJS code:


var url = "" // url to audio stream;
var player = new Player();
var playlist = new Playlist();
var mediaItem = new MediaItem("audio", url);
player.playlist = playlist;
player.playlist.push(mediaItem);        
player.present();      


Here it's not possible to set the AVAudioSessionCategoryPlayback or the AVAudioSession, since they're just not available here. Is there a way to get the code above working in the background?


I know it's possible to trigger the native player from JavaScript like 'potatoes' wrote. However doing that loses the standard interface the JSPlayer provides.

I managed to get this working. For anyone who has the same problem here's what to do:


1. Enable 'Required Background Modes' in Info.plist and item 0 must read: 'App plays audio or streams audio/video using AirPlay'


2. In your AppDelegate file, you must import AVFoundation and add the following code (even if you are not using the AVPlayer in Xcode but are using the javascript version of the Player).

        // CATEGORY PLAYBACK
        let newAudioSession = AVAudioSession.sharedInstance()
      
        do {
            try newAudioSession.setCategory(AVAudioSessionCategoryPlayback)
            try newAudioSession.setActive(true)
        } catch {
            print(“Did not assign a audio category")
        }

Thanks, this worked. Allows audio to play in the background, great find and I have had no luck finding this in the documentation.


Although, now that I can play audio in the background, I loose my play/pause functionality when I navigate back into my app. Any idea how to persist that audio player in order to properly play/pause once back into the origional application?

We now allow you to add the now playing view easily to your menu bar template. Checkout the nowPlayingMenuItem.


https://developer.apple.com/library/prerelease/content/documentation/LanguagesUtilities/Conceptual/ATV_Template_Guide/CompoundInformationElements.html#//apple_ref/doc/uid/TP40015064-CH22-SW15

This worked perfectly for me! Thank you! Too bad there is no official documentation about this issue. Took me a lot of time to find this thread.

This is broken with the tvOS 11.3 betas. I have a bug report for this here: https://bugreport.apple.com/web/?problemID=38170063