我试图通过A2DP/AVRCP发送曲目信息。现在,音乐完美流畅,但在“接收器”(即:汽车音响)上,“轨道信息屏幕”是空白的(这不是使用流行的播放器的情况)。 有什么想法?通过A2DP/AVRCP发送曲目信息
回答
此代码为我工作:
private static final String AVRCP_PLAYSTATE_CHANGED = "com.android.music.playstatechanged";
private static final String AVRCP_META_CHANGED = "com.android.music.metachanged";
private void bluetoothNotifyChange(String what) {
Intent i = new Intent(what);
i.putExtra("id", Long.valueOf(getAudioId()));
i.putExtra("artist", getArtistName());
i.putExtra("album",getAlbumName());
i.putExtra("track", getTrackName());
i.putExtra("playing", isPlaying());
i.putExtra("ListSize", getQueue());
i.putExtra("duration", duration());
i.putExtra("position", position());
sendBroadcast(i);
}
呼叫bluetoothNotifyChange与根据您的播放状态相应的意图(如上定义):暂停/播放/元数据改变。
要将轨道元数据发送到headunit,您需要发送一个intent。
Intent avrcp = new Intent("com.android.music.metachanged");
avrcp.putExtra("track", "song title");
avrcp.putExtra("artist", "artist name");
avrcp.putExtra("album", "album name");
Context.sendBroadcast(avrcp);
当歌曲播放完毕后,发送另一个含有空字符串的意图作为putExtra方法的第二个参数。
之前,虽然我还没有找到另一种方式来做到这一点。这似乎是一个骇人听闻的方法。它看起来像你依靠谷歌音乐应用来处理这个意图。谷歌音乐应用如何做到这一点? – 2013-07-29 15:46:36
确实。谷歌音乐可能是唯一可以使用avrcp的音乐吗?所有的应用程序都应该通过RemoteControlClient来实现这个功能吗? – 2014-01-20 21:18:23
这花了我一直想弄清楚。只是传播意图没有奏效。我AVRCP通过发送意图和实施RemoteControlClient
这里工作是我使用的代码:
public void onCreate(){
super.onCreate();
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
ComponentName rec = new ComponentName(getPackageName(), MyReceiver.class.getName());
mAudioManager.registerMediaButtonEventReceiver(rec);
Intent i = new Intent(Intent.ACTION_MEDIA_BUTTON);
i.setComponent(rec);
PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
mRemoteControlClient = new RemoteControlClient(pi);
mAudioManager.registerRemoteControlClient(mRemoteControlClient);
int flags = RemoteControlClient.FLAG_KEY_MEDIA_PREVIOUS
| RemoteControlClient.FLAG_KEY_MEDIA_NEXT
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY
| RemoteControlClient.FLAG_KEY_MEDIA_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_PLAY_PAUSE
| RemoteControlClient.FLAG_KEY_MEDIA_STOP
| RemoteControlClient.FLAG_KEY_MEDIA_FAST_FORWARD
| RemoteControlClient.FLAG_KEY_MEDIA_REWIND;
mRemoteControlClient.setTransportControlFlags(flags);
}
private void onTrackChanged(...) {
String title = ...;
String artist = ...;
String album = ...;
long duration = ...;
Intent i = new Intent("com.android.music.metachanged");
i.putExtra("id", 1);
i.putExtra("track", title);
i.putExtra("artist", artist);
i.putExtra("album", album);
i.putExtra("playing", "true");
sendStickyBroadcast(i);
RemoteControlClient.MetadataEditor ed = mRemoteControlClient.editMetadata(true);
ed.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, title);
ed.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, album);
ed.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artist);
ed.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, track.getDuration());
ed.apply();
}
public void onDestroy(){
mAudioManager.unregisterRemoteControlClient(mRemoteControlClient);
super.onDestroy();
}
你可以解释一下这一行:ComponentName rec = new ComponentName(getPackageName(),MyReceiver.class.getName());' – Distwo 2015-06-17 18:09:51
这就是你如何使用反射来查看BroadcastReceiver ... – 2015-07-28 21:56:09
我试过你的代码,它确实符合我们在https://android.googlesource.com/platform/packages/apps/Music/+/android -5.1.1_r13/src/com/android/music/MediaPlaybackService.java,但这不起作用。 难道是因为我们必须处理AudioManager.AUDIOFOCUS? – 2015-08-30 14:03:43
如果你只是想从你的手机发送的元数据信息,以连接AVRCP兼容的音频蓝牙设备和不要想从蓝牙设备控制你的应用程序,你可能会发现下面的代码有用。 有没有需要实现和注册与AudioManager的MediaButtonEventReceiver。
我还包含API版本21(LOLLIPOP,5.0)的代码。从API 21使用RemoteControlClient已弃用,鼓励使用MediaSession。
初始化阶段:
if (mAudioManager == null) {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
if (mRemoteControlClient == null) {
Log.d("init()", "API " + Build.VERSION.SDK_INT + " lower then " + Build.VERSION_CODES.LOLLIPOP);
Log.d("init()", "Using RemoteControlClient API.");
mRemoteControlClient = new RemoteControlClient(PendingIntent.getBroadcast(this, 0, new Intent(Intent.ACTION_MEDIA_BUTTON), 0));
mAudioManager.registerRemoteControlClient(mRemoteControlClient);
}
} else {
if (mMediaSession == null) {
Log.d("init()", "API " + Build.VERSION.SDK_INT + " greater or equals " + Build.VERSION_CODES.LOLLIPOP);
Log.d("init()", "Using MediaSession API.");
mMediaSession = new MediaSession(this, "PlayerServiceMediaSession");
mMediaSession.setFlags(MediaSession.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setActive(true);
}
}
发送歌曲的元数据信息,AVRCP兼容的蓝牙音频设备的方法:
private void onTrackChanged(String title, String artist, String album, long duration, long position, long trackNumber) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
RemoteControlClient.MetadataEditor ed = mRemoteControlClient.editMetadata(true);
ed.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, title);
ed.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, artist);
ed.putString(MediaMetadataRetriever.METADATA_KEY_ALBUM, album);
ed.putLong(MediaMetadataRetriever.METADATA_KEY_DURATION, duration);
ed.putLong(MediaMetadataRetriever.METADATA_KEY_CD_TRACK_NUMBER, trackNumber);
ed.apply();
mRemoteControlClient.setPlaybackState(RemoteControlClient.PLAYSTATE_PLAYING, position, 1.0f);
} else {
MediaMetadata metadata = new MediaMetadata.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, title)
.putString(MediaMetadata.METADATA_KEY_ARTIST, artist)
.putString(MediaMetadata.METADATA_KEY_ALBUM, album)
.putLong(MediaMetadata.METADATA_KEY_DURATION, duration)
.putLong(MediaMetadata.METADATA_KEY_TRACK_NUMBER, trackNumber)
.build();
mMediaSession.setMetadata(metadata);
PlaybackState state = new PlaybackState.Builder()
.setActions(PlaybackState.ACTION_PLAY)
.setState(PlaybackState.STATE_PLAYING, position, 1.0f, SystemClock.elapsedRealtime())
.build();
mMediaSession.setPlaybackState(state);
}
}
呼叫如果元数据的变化,但是否我们有A2DP连接到音频蓝牙设备。无需发送元数据信息,如果我们没有连接:
if (mAudioManager.isBluetoothA2dpOn()) {
Log.d("AudioManager", "isBluetoothA2dpOn() = true");
onTrackChanged(getTitle(), getArtist(), getAlbum(), getDuration(), getCurrentPosition(), getId());
}
清理上摧毁:
@Override
public void onDestroy() {
super.onDestroy();
[..]
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
mAudioManager.unregisterRemoteControlClient(mRemoteControlClient);
} else {
mMediaSession.release();
}
}
This是如何看起来像我的汽车音响
洗牌重复模式呢?你也处理过这个吗?谢谢 – issamux 2016-06-03 20:25:36
@issamux看看这一个http://stackoverflow.com/questions/31194180/how-do-i-make-shuffle-playlist-button-and-repeat-button-in-android-studio – 2016-07-09 20:10:48
你不需要如果您使用Compat版本的组件,可以控制SDK_INT。 下面的代码测试了很多车载蓝牙设备,并且像魅力一样工作。 有些设备不理解某些KEY,因此最好使用可能的KEY。 Reference。别忘了。建立()后putBitmap不
public static void sendTrackInfo() {
if(audioManager == null) {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
}
if (mMediaSession == null) {
mMediaSession = new MediaSessionCompat(this, "PlayerServiceMediaSession");
mMediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mMediaSession.setActive(true);
}
if (audioManager.isBluetoothA2dpOn()) {
try {
String songTitle = getTitle();
String artistTitle = getArtist();
String radioImageUri = getImagesArr().get(0);
String songImageUri = getImagesArr().get(1);
long duration = getDuration();
final MediaMetadataCompat.Builder metadata = new MediaMetadataCompat.Builder();
metadata.putString(MediaMetadataCompat.METADATA_KEY_TITLE, songTitle);
metadata.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_TITLE, songTitle);
metadata.putString(MediaMetadataCompat.METADATA_KEY_ARTIST, artistTitle);
metadata.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ARTIST, artistTitle);
metadata.putString(MediaMetadataCompat.METADATA_KEY_ART_URI, radioImageUri);
metadata.putString(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON_URI, radioImageUri);
metadata.putString(MediaMetadataCompat.METADATA_KEY_ALBUM_ART_URI, songImageUri);
metadata.putLong(MediaMetadataCompat.METADATA_KEY_DURATION, duration);
imageCounter = 0;
Glide.with(act)
.load(Uri.parse(radioImageUri))
.asBitmap()
.into(new SimpleTarget<Bitmap>(250, 250) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
metadata.putBitmap(MediaMetadataCompat.METADATA_KEY_ART, bitmap);
metadata.putBitmap(MediaMetadataCompat.METADATA_KEY_DISPLAY_ICON, bitmap);
imageCounter = imageCounter + 1;
if(imageCounter == 2) {
mMediaSession.setMetadata(metadata.build());
}
}
});
Glide.with(act)
.load(Uri.parse(songImageUri))
.asBitmap()
.into(new SimpleTarget<Bitmap>(250, 250) {
@Override
public void onResourceReady(Bitmap bitmap, GlideAnimation anim) {
metadata.putBitmap(MediaMetadataCompat.METADATA_KEY_ALBUM_ART, bitmap);
imageCounter = imageCounter + 1;
if(imageCounter == 2) {
mMediaSession.setMetadata(metadata.build());
}
}
});
}
catch (JSONException e) {
e.printStackTrace();
}
}
}
- 1. 我想通过通知发送信息
- 2. 通过Javascript发送有效信息
- 3. 通过AJAX发送登录信息
- 4. 通过PHP页面发送信息
- 5. 通过Html将信息发送到IP
- 6. JAVA通过json发送/接收信息
- 7. 通过Android获取歌曲信息
- 8. 通过SMPP发送短信
- 9. 通过NFC发送短信
- 10. 发送短信通过.net
- 11. 通过彩信发送gif
- 12. 通过IP发送短信
- 13. 通过Android 2.3.3读取PayWave(ISO14443-B格式)曲目信息NFC
- 14. Android:通过短信发送网络信息
- 15. 通过HTTPS发送消息
- 16. 通过SuperSocket发送消息
- 17. 通过SIP发送消息
- 18. 使用作曲者通过twilio发送消息
- 19. 截取通过未加密通道发送的信息
- 20. 通过卷曲发送XML和头
- 21. 通过PHP发送短信/短信
- 22. 通过在Java中发送数据的信息曝光
- 23. 通过DataOutputStream.writeUTF()发送信息时出现奇怪的字符
- 24. 通过套接字连接发送额外信息
- 25. Android没有收到通过UDP发送的信息
- 26. 通过邮政发送信息与阿贾克斯问题
- 27. 窗体信息不通过电子邮件发送
- 28. Django/HTML:通过点击按钮发送信息?
- 29. 通过PHP(curl)或jQuery - IP问题发送POST信息 -
- 30. 由ozeki消息服务器通过PHP发送短信错误?
此代码适用于我的蓝牙耳机,但只显示曲目名称...将测试更多。谢谢。 – DavyJonesUA 2013-07-22 13:23:46
此代码在Sony耳机上不适用于我,但Google音乐应用程序仍然可以在该耳机上显示歌曲信息。 – Wayne 2013-08-20 03:29:23
你可以给我一个带有元数据的示例链接吗? – Wayne 2013-08-20 06:30:30