是否可以通过STREAM_ALARM播放YouTubePlayer

时间:2017-07-26 08:19:44

标签: android youtube-api

我有一个闹钟应用程序,我希望用户能够将YouTube视频设置为闹钟。问题是YouTubePlayer正在通过音乐流播放,这意味着如果用户在耳机插入智能手机时要进入睡眠状态,他/她将无法听到警报。在MediaPlayer上,我可以使用以下代码行设置闹钟以使用手机扬声器:

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

是否有可能对YouTubePlayer做同样的事情,或者我必须通过音乐流播放?

3 个答案:

答案 0 :(得分:2)

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.

答案 1 :(得分:0)

不确定YouTubePlayer API,但您可以ExoPlayer。 API允许您设置流类型(请参阅issue #755)并能够播放YouTube视频。有一个demo project yt videos

答案 2 :(得分:0)

是的,在收到报警广播后,您可以使用窗口管理器和代码创建一个小窗口,在其中播放YouTube视频。