在电脑终端使用adb命令让手机播放音乐

  1. 从adb给手机发送广播:adb shell am broadcast -a com.android.test --es test_string "this is test string" --ei test_int 100 --ez test_boolean true 后面的e代表参数extra,--es代表extra是string类型,--ei代表int类型,--ez代表boolean类型。
    让手机播放和停止音乐就是借助于广播来实现的:
    让手机播放音乐:adb shell am broadcast -a com.android.music.musicservicecommand --es command "play"
    让手机暂停音乐:adb shell am broadcast -a com.android.music.musicservicecommand -es command "pause"
    对应的java代码实现:
Intent i = new Intent("com.android.music.musicservicecommand");
i.putExtra("command", "pause");
sendBroadcast(i);
  1. 通过adb查询手机是否在播放音乐:adb shell dumpsys media.player
    由于我们只需要返回结果的一部分,所以可以改成:adb shell dumpsys media.player | grep 'AudioTrack::dump' -A 4 | grep 'state'
    返回的结果会类似下面这段:
AudioTrack::dump
 stream type(3), left - right volume(1.000000, 1.000000)
 format(16777216), channel count(2), frame count(262144)
 sample rate(44100), speed(1.000000), status(0)
 state(0), latency (6040)

其中:
state(0) - playing
state(1) - interrupted by popup
state(2) - paused / stopped

参考链接:

  1. https://stackoverflow.com/questions/25846015/how-to-get-media-player-state-using-adb-command
  2. https://stackoverflow.com/questions/14910360/how-to-play-or-resume-music-of-another-music-player-from-my-code
posted @ 2018-01-15 15:13  dfghjkloiuy  阅读(1499)  评论(0编辑  收藏  举报