SUXA
SUXA

Reputation: 169

Is it possible to play the YouTubePlayer via STREAM_ALARM

I have an alarm clock app and i want the users to be able to set a youtube video as their alarm. The thing is that the YouTubePlayer is playing via the music stream, which means that if the user is going to sleep while the earphones are plugged to the smartphone, he/she won't be able to hear the alarm. On the MediaPlayer i can set the alarm to use the phone speakers by using this line of code:

audioManager.setStreamVolume(AudioManager.STREAM_ALARM, stream_music_volume, AudioManager.FLAG_REMOVE_SOUND_AND_VIBRATE);

Is there a possibility to do the same with the YouTubePlayer or i have to play it via the music stream?

Upvotes: 7

Views: 193

Answers (3)

Vanna
Vanna

Reputation: 756

It is not possible. You can not change YoutubePlayer's stream type.
You are not allowed to play youtube videos in the background.
That being said you should not use a online resource as an alarm, a network connection might not be available. You should instead download the media file and use a mediaplayer (which can handle online resources) and adjust the stream type via setAudioStreamType(AudioManager.STREAM_ALARM). You can adjust the media player's volume by using adjustVolume or AudioManager's setStreamVolume, but you might have to get the MODIFY_AUDIO_SETTINGS permission. Note that you should also get the ACCESS_NOTIFICATION_POLICY permission to override the Do Not Dirturb feature if you were to use YoutubePlayer in the background as a media stream.

Upvotes: 2

Atam_Jeet Singh
Atam_Jeet Singh

Reputation: 109

yes, on receive broadcast of alarm, you can create a little window using window manager and code to play youtube video in it.

Upvotes: 0

Dodge
Dodge

Reputation: 8307

Not sure about YouTubePlayer API but you could you ExoPlayer. The API allows you to set the stream type (see issue #755) and is able to play youtube videos. There is a demo project with yt videos.

Upvotes: 0

Related Questions