【问题标题】:Get Spotify Stream Volume | Android (Kotlin)获取 Spotify 流音量 |安卓(科特林)
【发布时间】:2021-03-17 13:18:55
【问题描述】:

我正在尝试获取 Spotify 音乐流的音量值。 当 Spotify 从 Android 设备播放音乐或 Android 设备连接到蓝牙扬声器时,我可以让它抛出 STREAM_MUSIC。

但是当我从 Android 设备流式传输到 Smart TV/TV Streamer 上的 Spotify 应用程序时,Android 正在创建一个名为“Spotify”的新音量流(如图所示) 我怎样才能得到这个值?

Picture of Volume Streams on my Android device

请帮助我了解如何获取此流的音量.. 谢谢!

【问题讨论】:

  • 我不确定它是不是一个流,这首歌并没有真正在设备上播放,然后被发送到电视。电视本身正在播放它,而您正在应用程序上控制它。类似于我可以通过笔记本电脑或手机控制电脑的 Spotify,绝对不会将任何内容从一台设备流式传输到另一台设备。

标签: android android-studio kotlin spotify android-audiomanager


【解决方案1】:

其实,太难了。

首先,您必须使用 MediaSessionManager.getActiveSessions https://developer.android.com/reference/android/media/session/MediaSessionManager#getActiveSessions(android.content.ComponentName)

但它需要 NotificationListenerService。 请参阅 https://developer.android.com/reference/android/service/notification/NotificationListenerService https://github.com/codechacha/NotificationListener

MyNotificationService.java

public class MyNotificationService extends NotificationListenerService {
    @Override
    public void onNotificationRemoved(StatusBarNotification sbn) {
        super.onNotificationRemoved(sbn);
    }

    @Override
    public void onNotificationPosted(StatusBarNotification sbn) {
        super.onNotificationPosted(sbn);
    }
}

AndroidManifest.xml

    <service
        android:name=".MyNotificationService"
        android:permission="android.permission.BIND_NOTIFICATION_LISTENER_SERVICE">
        <intent-filter>
            <action android:name="android.service.notification.NotificationListenerService" />
        </intent-filter>
        <meta-data
            android:name="android.service.notification.default_filter_types"
            android:value="1,2">
        </meta-data>
    </service>

请求许可

if (!permissionGrantred()) {
    Intent intent = new Intent(
            "android.settings.ACTION_NOTIFICATION_LISTENER_SETTINGS");
    startActivity(intent);
}


private boolean permissionGrantred() {
    Set<String> sets = NotificationManagerCompat.getEnabledListenerPackages(this);
    if (sets != null && sets.contains(getPackageName())) {
        return true;
    } else {
        return false;
    }
}

主要代码

MediaSessionManager msm = (MediaSessionManager)getSystemService(Context.MEDIA_SESSION_SERVICE);
ComponentName cn = new ComponentName(this, MyNotificationService.class);
List<MediaController> list = msm.getActiveSessions(cn);
for (MediaController mc : list) {
    if (mc.getPackageName().equals("com.spotify.music")) {
        int spotifyVolume = mc.getPlaybackInfo().getCurrentVolume();
    }
}

或者你可以通过回调得到它

for (MediaController mc : list) {
    if (mc.getPackageName().equals("com.spotify.music")) {
        mc.registerCallback(new MediaController.Callback() {
            @Override
            public void onAudioInfoChanged(MediaController.PlaybackInfo info) {
                int spotifyVolume = info.getCurrentVolume();
            }
        });
    }
}

【讨论】:

  • 안녕하세요!是否可以在不允许应用访问通知的情况下获取媒体播放信息?这是一个完整的隐私破坏行为。我们不能在媒体会话直接可用时直接访问它吗?由于android 11将媒体播放通知分离为快速设置,它还应该允许在没有通知权限的情况下访问会话。
  • 你好,据我所知没有办法。 MediaSessionManager#getActiveSessions 需要 android.Manifest.permission.MEDIA_CONTENT_CONTROL 权限。但它可以从系统应用程序中使用。
猜你喜欢
  • 2023-02-14
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-23
  • 1970-01-01
相关资源
最近更新 更多