gpt4 book ai didi

java - 如何处理来自 MediaSession 远程卷覆盖的更改?

转载 作者:行者123 更新时间:2023-12-04 23:35:38 28 4
gpt4 key购买 nike

我正在尝试实现远程音量控制。它已经可以使用硬件音量键控制音量,但是当我尝试移动 MediaSession 远程音量覆盖中的 slider 时,VolumeProviderCompat.onAdjustVolume(..)不调用回调。我还尝试了其他回调,例如 MediaSessionCompat.Callback.onMediaButtonEvent(..)VolumeProviderCompat.onSetVolumeTo(..)但他们根本没有被调用。

如果您不知道我所说的“MediaSession 远程音量覆盖”是什么意思,这里是一个屏幕截图:

enter image description here

我创建了一个演示项目,您可以在这里下载:https://github.com/SaschaZ/VolumeProviderDemo .

这是我的DemoActivity的相关部分:

public class DemoActivity extends AppCompatActivity {

...

private Notification createNotification(@NonNull final DemoVolumeController demoVolumeController) {
Log.d(TAG, "createNotification()");

final NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
.setPriority(NotificationCompat.PRIORITY_HIGH)
.setSmallIcon(R.mipmap.ic_launcher);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
if (session != null) {
session.release();
}
session = new MediaSessionCompat(this, "demoMediaSession");
session.setPlaybackState(new PlaybackStateCompat.Builder()
.setState(PlaybackStateCompat.STATE_PLAYING, 1, 1.0f)
.build());
session.setPlaybackToRemote(createVolumeProvider(demoVolumeController));
session.setActive(true);
}

return builder.build();
}

private VolumeProviderCompat createVolumeProvider(@NonNull final DemoVolumeController demoVolumeController) {
// I don't use this callback directly, but I need to set it or my VolumeProvider will not work. (sounds
// strange but I tried it several times)
session.setCallback(new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(final Intent mediaButtonEvent) {
Log.d(TAG, "onMediaButtonEvent() called with: " + "mediaButtonEvent = [" + mediaButtonEvent + "]");
return super.onMediaButtonEvent(mediaButtonEvent);
}
});

return new VolumeProviderCompat(VolumeProviderCompat.VOLUME_CONTROL_RELATIVE,
100,
demoVolumeController.getVolume()) {
@Override
public void onAdjustVolume(final int direction) {
final int volume = demoVolumeController.setVolumeRelative(direction);
showVolume(volume);

Log.d(TAG, "onAdjustVolume() called with: " + "direction = [" + direction + "] - " +
"new volume=" + volume);

// Nasty hack to get sync with the volume overlay of Android. setCurrentVolume does not work :(
session.setPlaybackToRemote(createVolumeProvider(demoVolumeController));
}
};
}

...
}

有什么提示吗?
先感谢您!

最佳答案

使用 Activity 的 setVolumeControlStream 方法——通常在它的 onCreate 方法中——
允许您指定哪个音频流应由音量键控制,而当前
Activity 处于 Activity 状态:

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.audioplayer);

setVolumeControlStream(AudioManager.STREAM_MUSIC);
}
您可以指定任何可用的音频流,但在使用媒体播放器时,您应该
指定 STREAM_MUSIC 流以使其成为音量键的焦点。

关于java - 如何处理来自 MediaSession 远程卷覆盖的更改?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36353856/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com