Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 965
SUXA Avatar asked Jul 26 '17 08:07

SUXA


1 Answers

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.

like image 83
Vanna Avatar answered Oct 09 '22 18:10

Vanna