从MediaPlayer获取当前播放的歌曲详细信息

时间:2017-01-26 09:35:15

标签: android

我正在开发一些项目,我已经使用过这段代码,但是一旦我开始自己的应用程序而不是在音乐状态发生变化后我想要当前播放的音乐信息。有没有办法获得媒体播放器播放的当前歌曲的详细信息

package com.example.demomusic;

import java.io.File;

import android.os.Bundle;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    //public final static String START_REMOTE_CONTROLLER_ACTION = "com.example.demomusic.START_REMOTE_CONTROLLER";
    public static final String SERVICECMD = "com.android.music.musicservicecommand";



TextView tv;
String title, album, artist, genre, fecha, address, path, length;
Long duration;
File archivo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView) findViewById(R.id.textView1);

    IntentFilter iF = new IntentFilter();
    iF.addAction("com.android.music.com.android.music.musicservicecommand");
    iF.addAction("com.android.music.metachanged");

    iF.addAction("com.android.music.playstatechanged");
    iF.addAction("com.android.music.playbackcomplete");
    iF.addAction("com.android.music.queuechanged");
    iF.addAction("com.android.music.metachanged");
    iF.addAction("com.htc.music.metachanged");
    iF.addAction("fm.last.android.metachanged");
    iF.addAction("com.sec.android.app.music.metachanged");
    iF.addAction("com.nullsoft.winamp.metachanged");
    iF.addAction("com.amazon.mp3.metachanged");
    iF.addAction("com.miui.player.metachanged");
    iF.addAction("com.real.IMP.metachanged");
    iF.addAction("com.sonyericsson.music.metachanged");
    iF.addAction("com.rdio.android.metachanged");
    iF.addAction("com.samsung.sec.android.MusicPlayer.metachanged");
    iF.addAction("com.andrew.apollo.metachanged");
    iF.addAction("com.meizu.media.music");


    String scheme = mAudioUri.getScheme();
    String title = "";
    String artist = "";
    if(scheme.equals("content")) {
        String[] proj = {MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST};
        Cursor cursor = this.getContentResolver().query(mAudioUri, proj, null, null, null);
        if(cursor != null && cursor.getCount() > 0) {
            cursor.moveToFirst();
            if(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE) != -1) {  
                title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
            }
        }
    }


    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.v("tag ", action + " / " + cmd);
        String artist = intent.getStringExtra("artist");
        String album = intent.getStringExtra("album");
        String track = intent.getStringExtra("track");
        Log.v("tag", artist + ":" + album + ":" + track);
        Toast.makeText(MainActivity.this, track, Toast.LENGTH_SHORT).show();
        tv.setText("heyyy :" + track);*/


          Bundle extra = intent.getExtras();
            title = intent.getStringExtra("track");
            album = intent.getStringExtra("album");
            artist= intent.getStringExtra("artist");

            genre= intent.getStringExtra("genre");
            fecha= extra.getString("date");
            duration= (intent.getLongExtra(MediaStore.Audio.AudioColumns.DURATION,1000));
            address = extra.getString("address");

            String all = "Title:"+ title +"\n Album:" + album + "\n Artist:" + artist + "\n Genre:" + genre
                    + "\n Fecha:" + fecha + "\n Duration:" + duration + "\n Address:" + address + "\n lenght:" + length + "\n Path:" + path;
            Toast.makeText(MainActivity.this, all, Toast.LENGTH_LONG).show();
            tv.setText("hey "+ all);
    }
};

}

0 个答案:

没有答案