我在doc看过音频管理器。我想在设备上播放任何音频/视频(媒体)时收到通知。那么广播接收器是否有任何行动来获取媒体的历史?我想,我可以使用辅助功能从通知更改中获取它。但我不想使用辅助功能,因为我想跟踪将在任何默认媒体播放器或设备中任何已安装的软件中播放的所有媒体。有什么办法吗?
我尝试过以下代码来获取有关媒体曲目的信息。但它仅适用于google play music
。如何从所有默认/已安装的媒体播放器获取曲目信息。
public class MainActivity extends Activity {
public static final String SERVICECMD = "com.android.music.musicservicecommand";
public static final String CMDNAME = "command";
public static final String CMDTOGGLEPAUSE = "togglepause";
public static final String CMDSTOP = "stop";
public static final String CMDPAUSE = "pause";
public static final String CMDPREVIOUS = "previous";
public static final String CMDNEXT = "next";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IntentFilter iF = new IntentFilter();
iF.addAction("com.android.music.metachanged");
iF.addAction("com.android.music.playstatechanged");
iF.addAction("com.android.music.playbackcomplete");
iF.addAction("com.android.music.queuechanged");
registerReceiver(mReceiver, iF);
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent)
{
String action = intent.getAction();
String cmd = intent.getStringExtra("command");
Log.d("mIntentReceiver.onReceive: ", action + " / " + cmd);
String artist = intent.getStringExtra("artist");
String album = intent.getStringExtra("album");
String track = intent.getStringExtra("track");
Log.d("Music","artist: "+artist+"\n album: "+album+"\n track: "+track);
}
};
任何帮助将不胜感激!
答案 0 :(得分:1)
要回答,没有绝对没有播放在Android中播放的事件歌曲。
虽然,音频管理器确实有一个API,可以告诉您是否在设备中播放了任何内容
AudioManager manager = (AudioManager)this.getSystemService(Context.AUDIO_SERVICE);
if(manager.isMusicActive())
{
// Something is being played.
}
http://developer.android.com/reference/android/media/AudioManager.html#isMusicActive%28%29
除此之外,您还可以使用提供商,如果由各种播放器(例如谷歌播放)曝光,以找出最近播放的哪首歌曲,但只有在提供商曝光的情况下才能使用。