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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With