Android 9.0 蓝牙功能之一:蓝牙音乐
本章节记录如何构建蓝牙音乐。
文章目录
- Android 9.0 蓝牙功能之一:蓝牙音乐
- 主要流程
- 相关代码
- 其他要点:
- 蓝牙AG_EVENT广播 (手机网络?电量?)
- 蓝牙音乐播放/暂停/上下曲控制
注意蓝牙应用必须是 System App
主要流程
Android 蓝牙音乐播放器使用 MediaBrowser 框架实现
构造一个用于 Bind BluetoothMediaBrowserService 的MediaBrowser, 然后通过
来 Bind 到 BluetoothMediaBrowserService 上。
当 Bind 成功后,根据 构造一个播放控制 MediaController,并注册 Callback,以后则
通过 MediaController 控制播放,通过 Callback 监听播放状态及媒体信息变化。
相关代码
在蓝牙连接上a2dp后构造 MediaBrowser,Context必须是Application,不能是 Activity
private static final String BT_BROWSED_PACKAGE = "";
private static final String BT_BROWSED_SERVICE =
"";
private void connectMediaBrowser() {
(TAG, "connectMediaBrowser: ");
if (null != mMediaBrowser) {
disconnectMediaBrowser();
}
mMediaBrowser = new MediaBrowser(mContext,
new ComponentName(BT_BROWSED_PACKAGE, BT_BROWSED_SERVICE),
mConnectionCallback, null);
();
mTryConnectMediaSessionCnt++;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
实现 ConnectionCallback,以监听 bind 结果。
private mConnectionCallback = new () {
@Override
public void onConnected() {
//连接上 BrowserService 了
//构造一个 MediaController
mMediaController = new MediaController(mContext,());
//注册媒体状态信息监听
(mMediaControllerCallback);
}
@Override
public void onConnectionSuspended() {
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
实现媒体状态信息变化的监听。
private mMediaControllerCallback = new () {
@Override
public void onPlaybackStateChanged(PlaybackState state) {
//更新播放状态
}
@Override
public void onMetadataChanged(MediaMetadata metadata) {
//更新媒体信息,ID3 等
}
@Override
public void onSessionEvent(String event, Bundle extras) {
//客制化的其他 Event 通知
}
};
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
其他要点:
蓝牙AG_EVENT广播 (手机网络?电量?)
public static final String ACTION_AG_EVENT = ".AG_EVENT";
private void handleAGEvent(Intent intent) {
boolean ptsTest = ("", false);
if (!ptsTest || null == intent) {
return;
}
int network = (BluetoothHeadsetClient.EXTRA_NETWORK_STATUS, -1);
int networkSignalStrength = (
BluetoothHeadsetClient.EXTRA_NETWORK_SIGNAL_STRENGTH, -1);
int networkRoaming = (
BluetoothHeadsetClient.EXTRA_NETWORK_ROAMING, -1);
int batteryLevel = (BluetoothHeadsetClient.EXTRA_BATTERY_LEVEL, -1);
(TAG, "handleAGEvent: network: " + network
+ ", networkSignalStrength " + networkSignalStrength
+ ", networkRoaming " + networkRoaming
+ ", batteryLevel " + batteryLevel);
String toastString = "";
if (network != -1) {
if (network == 0) {
toastString = "network unavailable";
} else {
toastString = "network available";
}
showToast(toastString);
}
if (networkSignalStrength != -1) {
toastString = ("network signal stringth %d", networkSignalStrength);
showToast(toastString);
}
if (networkRoaming != -1) {
if (networkRoaming == 0) {
toastString = "no roaming";
} else {
toastString = "active roaming";
}
showToast(toastString);
}
if (batteryLevel != -1) {
toastString = ("battery level %d", batteryLevel);
showToast(toastString);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
蓝牙音乐播放/暂停/上下曲控制
通过 MediaController 的接口控制播放
mediaControllerCntrl = ();
switch (cmd) {
case CMD_PLAY:
if(isSendPlayCmd) {
return true;
}
(MSG_PLAY_STATE_SET_CMD,500);
isSendPlayCmd = true;
();
break;
case CMD_PAUSE:
();
break;
case CMD_PRV:
();
break;
case CMD_NEXT:
();
break;
case CMD_REPEAT:
sendCustomAction(CUSTOM_MSG_REPEAT_MODE_CHANGED, null);
break;
case CMD_SHUFFLE:
sendCustomAction(CUSTOM_MSG_SUFFLE_MODE_CHNAGED, null);
break;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32