'Is there a way to play Twitch audio without interaction from the user when the phone is asleep?

--First off I am aware this is invasive, I'm trying to make an alarm--

I'm looking to create an Android app that sets an alarm which will play Twitch audio without any interaction from the user. For the alarm part I'm using AlarmManager However I am struggling to get twitch to autoplay on the locked phone screen when the alarm triggers.

My two leading ideas for how to do this (on Android) are:

Idea#1: Embed twitch player in a webpage that is shown on the lockscreen when the alarm triggers: Twitch Embed Fullscreen intent However the player is muted and I haven't been able to find a way around this (and doesn't seem like it is supported) [Similar issue #1]https://discuss.dev.twitch.tv/t/i-am-unable-to-mute-unmute-video-clips-in-twitch-preview/31585 [Similar issue #2]https://discuss.dev.twitch.tv/t/how-to-unmute-clips-on-load/26054/2

Idea #2: Just launch a stream using the Twitch app while the screen is locked. This is currently how I am starting a stream for Youtube audio and it launches while the phone is locked in the background fine. I know there is some functionality for this in the Twitch android app due to being able to play streams without the app open by changing settings in a stream. However currently if I try to launch a stream using Twitch while the screen is locked, as far as I can tell the video doesn't load until the user opens the phone.

So yeah, Is there a way to play Twitch audio without interaction from the user when the phone is asleep?

Thanks



Solution 1:[1]

  • Edit added code as text

I managed to solve this using solution #1 (Fullscreen intent with android webview). However instead of embeding the native twitch player, I used android webview to open the twitch website directly. The important part to make the videos not muted was to set the mediaPlaybackRequiresUserGesture flag to false. Code is below `

val rO  =  intent.getSerializableExtra("RingtoneOption") as RingtoneOption
        val url = rO.liveContentURL
        setContentView(R.layout.activity_full_screen_alarm)
        turnScreenOnAndKeyguardOff()
        var myWebView = findViewById<View>(R.id.webview) as WebView
        val webSettings = myWebView.settings
        webSettings.javaScriptEnabled = true
        webSettings.mediaPlaybackRequiresUserGesture = false;
        myWebView.webViewClient = WebViewClient()
        myWebView.webChromeClient = WebChromeClient()
        myWebView.loadUrl(url)

`

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1