嗨堆垛机正在处理一个项目,我在获取当前播放歌曲的元数据方面遇到了一些麻烦。想法是获得下一个项目: 标题,专辑,艺术家,流派,日期,年。我一直在测试我在StackOverflow上的几个例子中找到的MetadataRetriever方法,也就是intent.getStringExtra(" Key"),最后尝试从一个包中获取数据,但任何东西都给我提供了我需要的所有数据。我通常得到零。任何的想法?但最重要的是元数据必须来自播放的实际歌曲。 提前致谢。
package com.example.andres.metadataextract;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.graphics.Path;
import android.media.MediaMetadataRetriever;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends ActionBarActivity {
//ImageView album_art;
String title, album, artist, genre, fecha, address, path, length;
Long duration;
File archivo;
//MediaMetadataRetriever metaRetriver;
//byte[] art;
//public static final String SERVICECMD = "com.android.music.musicservicecommand";
@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");
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");
registerReceiver(mReceiver, iF);
//metaRetriver = new MediaMetadataRetriever();
}
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//metaRetriver.setDataSource(address);
//String action = intent.getAction();
//String cmd = intent.getStringExtra("command");
//Log.v("tag ", action + " / " + cmd);
//artist = intent.getStringExtra("artist");
//album = intent.getStringExtra("album");
//title = intent.getStringExtra("track");
//address = intent.getStringExtra("address");
//archivo = new File(direccion);
//I can get the values of those 3 items
Bundle extra = intent.getExtras();
title = intent.getStringExtra("track");
album = intent.getStringExtra("album");
artist= intent.getStringExtra("artist");
//Cant get the values of those others
genre= intent.getStringExtra("genre");
fecha= extra.getString("date");
duration= (intent.getLongExtra(MediaStore.Audio.AudioColumns.DURATION,1000));
address = extra.getString("address");
//album = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
//artist = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
//genre = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
//fecha = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE));
//duration = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
//Cursor cursor = getContentResolver().query(
// MediaStore.Audio.Media.INTERNAL_CONTENT_URI,
// null,
// null,
// null,
// MediaStore.Audio.Media.TITLE + " ASC");
//art = metaRetriver.getEmbeddedPicture();
//Bitmap songImage = BitmapFactory
// .decodeByteArray(art, 0, art.length);
//album_art.setImageBitmap(songImage);
try {
//while (cursor.moveToNext()) {
// title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
// Whatever else you need
// }
//title = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE));
//album = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM));
//artist = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST));
//genre = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE));
//fecha = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DATE));
//duration = (metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION));
} catch (Exception e) {
//album_art.setBackgroundColor(Color.GRAY);
//title = ("Unknown Title");
//album = ("Unknown Album");
//artist = ("Unknown Artist");
//genre = ("Unknown Genre");
//fecha = ("Unknown Date");
//duration = ("Unknown Duration");
}
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;
//Log.v("yossi", extra.toString());
//Toast.makeText(MainActivity.this, all, Toast.LENGTH_SHORT).show();
//Toast.makeText(MainActivity.this, extra.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, all, Toast.LENGTH_LONG).show();
}
};
}