title: Metadata of the current played song on android
body: <p>Hi stackers im working on a project and i'm having some troubles on getting the metadata of the current played song. The idea is to get the next items:
  title, album, artist, genre, date, year. I've been testing the MetadataRetriever method that I've found on several examples here on StackOverflow, also the intent.getStringExtra("Key"), and finally trying to get the data from a bundle, but anything gaves me all the data I need. I usually get nulls. Any idea? But the most important is that the metadata have to come from the actual song played. 
  Thanks in advance. </p>
  
  <pre><code>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();
  
          }
      };
  }
  </code></pre>