How to automatically start Twitch stream audio on locked Android device?

I want to build an alarm app that plays Twitch streams automatically when the device is locked. I know this sounds intrusive but it’s specifically for alarm purposes.

I’m using AlarmManager to handle the scheduling part, but I can’t figure out how to make Twitch audio start playing when the phone screen is off and locked.

I’ve tried two approaches so far:

Method 1: Using a web view with Twitch’s embedded player and showing it via full screen notifications. The problem is the embedded player starts muted by default and I can’t find any way to unmute it programmatically.

Method 2: Trying to launch the official Twitch app directly when the alarm fires. This works great with YouTube streams - they start playing in the background even when locked. But with Twitch, the stream doesn’t seem to actually load until someone unlocks the phone and opens the app.

I know Twitch has some background audio features since you can keep listening to streams when you minimize the app. But I need it to start from a completely closed state while the device is asleep.

Has anyone managed to get Twitch audio to autoplay on a locked Android phone? Any suggestions for alternative approaches would be really helpful.

twitch has cracked down hard on autoplay lately. even if u get it working, they’ll probably block it server-side - they hate automated requests hitting their streams. i’d switch to a different service for your alarm. youtube live or internet radio stations work way better for this and won’t give u the same headaches.

I faced a similar challenge while working on a project that involved background audio. The issue largely stems from Android’s power management and Twitch’s strict security protocols designed to prevent exactly what you’re attempting. Unfortunately, the Twitch API does not allow you to programmatically unmute embedded players due to security restrictions.

However, I discovered a workaround by implementing Android’s media session framework. You can set up a foreground service that registers as a media session, using intent filters to activate when your alarm goes off. This service can acquire audio focus and stream through a hidden WebView.

Keep in mind that you’ll require the permissions such as FLAG_KEEP_SCREEN_ON and SYSTEM_ALERT_WINDOW, in addition to acquiring a wake lock. It’s also essential to whitelist your app from battery optimizations. In my experience, I managed to make it work by using a transparent overlay activity, which seems to maintain the screen on while appearing off to the user.

Just a heads up: this method might not be reliable with future Android updates, as it relies on certain edge cases within the permission system.

I’ve tested this with similar background audio setups. The problem is Twitch’s autoplay policies - they need user interaction to start playback. Even if you launch content programmatically, the stream won’t play without the user actually tapping something. Hit this wall constantly when building notification-based media apps.

What worked for me: use Android’s notification system instead of fighting autoplay. When your alarm goes off, fire a heads-up notification with media controls. Add a custom action button that launches the Twitch stream with proper audio focus. This gives you the user interaction Twitch wants while still working as an alarm.

You’ll need NotificationCompat.MediaStyle with a PendingIntent for your stream handler. Use FLAG_ACTIVITY_NEW_TASK + SHOW_WHEN_LOCKED so the notification shows over the lock screen. Works with platform rules instead of against them.

Try using MediaPlayer or ExoPlayer in a foreground service instead of WebViews or external apps. I’ve done streaming audio in background services before - building a custom solution gives you way more control over playback. The trick is grabbing the actual stream URL from Twitch’s HLS manifest instead of using their embedded players. Parse the m3u8 playlist files Twitch uses and feed them straight into your media player. This completely bypasses the muting restrictions since you’re not dealing with web players. For the locked screen problem, combine PARTIAL_WAKE_LOCK with your foreground service so the audio system stays active. I’ve had good luck using MediaSessionCompat to register your service as a proper media player - helps with Android’s doze mode restrictions. One heads up though - Twitch changes their stream URL structure and adds auth tokens all the time, so you’ll need token refresh logic to keep things working long-term.