【发布时间】:2019-01-04 19:32:03
【问题描述】:
我在我的 Android 应用中使用 VideoView 来显示介绍动画。
如果 Google 音乐应用正在后台播放音乐,则调用 videoview.start() 会在后台停止 Google 音乐应用中的音乐播放。
有没有办法确保背景中的任何音乐都与我的介绍视频同时播放? (它没有音频)
谢谢!
【问题讨论】:
标签: android audio android-videoview
我在我的 Android 应用中使用 VideoView 来显示介绍动画。
如果 Google 音乐应用正在后台播放音乐,则调用 videoview.start() 会在后台停止 Google 音乐应用中的音乐播放。
有没有办法确保背景中的任何音乐都与我的介绍视频同时播放? (它没有音频)
谢谢!
【问题讨论】:
标签: android audio android-videoview
事实证明,当任何视频开始播放时,Google 音乐应用和其他一些应用都会停止播放音乐。
为了确保不会影响用户的聆听体验,如果我确定背景中有音乐播放,我现在会跳过介绍视频。
为此:
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if (am.isMusicActive()) {
loadApp(); // skip video and go straight to the app
}
else {
videoView.start(); // play video
}
【讨论】:
使用前面给出的两个答案,这里有一个解决方案,可以在您的视频结束后恢复音乐:
final boolean music_was_playing = ((AudioManager) getSystemService(Context.AUDIO_SERVICE)).isMusicActive();
VideoView vv_Video = (VideoView) findViewById(R.id.intro_video_view);
// play the intro video
vv_Video.setOnCompletionListener( new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer m) {
// resume music if it was playing cause our intro video just paused it temporarily
if (music_was_playing) {
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "play");
sendBroadcast(i);
}
// go to main menu
startActivity(new Intent(IntroActivity.this, MainMenuActivity.class));
}
});
【讨论】:
取自VideoView.java中的openVideo()
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
mContext.sendBroadcast(i);
【讨论】: