5

Làm cách nào để kiểm tra xem ứng dụng nhạc nào đang được phát trong thiết bị Android?Cách kiểm tra xem nhạc có đang phát từ ứng dụng nhạc nào không?

Sử dụng Trình quản lý âm thanh, chúng tôi có thể kiểm tra xem nhạc đang được phát hay không. Nhưng nó không cung cấp chi tiết về ứng dụng nhạc mà từ đó nó đang được phát.

AudioManager am = (AudioManager) this.getSystemService (Context.AUDIO_SERVICE);

am.isMusicActive() trả về true nếu nhạc được phát trong thiết bị. Nhưng tôi muốn tên ứng dụng nhạc mà từ đó nó đang được phát.

Tài liệu tham khảo:

How do you check if music is playing by using a broadcast receiver?

Android development : To find out which app is playing music currently

Ví dụ, nếu tôi chơi nhạc từ máy nghe nhạc Google Music. Sau đó, tôi phải có khả năng nhận tên ứng dụng nhạc là "Google Music Player".

Bất kỳ câu trả lời nào cho điều này sẽ được đánh giá cao. Cảm ơn!

Trả lời

0

Hãy thử này để hiện được chơi đường dẫn tập tin theo dõi của:

public Cursor query(Context context, Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder, int limit) { 
    try { 
     ContentResolver resolver = context.getContentResolver(); 
     if (resolver == null) return null; 
     if (limit > 0) uri = uri.buildUpon().appendQueryParameter("limit", "" + limit).build(); 
     return resolver.query(uri, projection, selection, selectionArgs, sortOrder); 
    } catch (UnsupportedOperationException ex) { return null; } 
} 
private BroadcastReceiver mReceiver = new BroadcastReceiver() { 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     String path = intent.getStringExtra("track").replaceAll("'","''"); 
     Cursor c = query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, 
       new String[] { MediaStore.Audio.Media.DATA }, MediaStore.Audio.Media.TITLE + "='" + path + "'", 
       null, null, 0); 
     try { 
      if (c == null || c.getCount() == 0) return; 
      int size = c.getCount(); 
      if (size != 1) return; 
      c.moveToNext(); 

      // Here's the song path 
      String songPath = c.getString(0); 
      Log.d("ROAST_APP", "" + songPath); 
     } finally { 
      if (c != null) c.close(); 
     } 
    } 
}; 

@Override 
protected 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); 
} 

nguồn:
Track info of currently playing music
Needletagger

Các vấn đề liên quan