Android 音乐播放器中显示歌曲的信息

时间:2022-12-14 05:00:49

前言
         欢迎大家我分享和推荐好用的代码段~~
声明
         欢迎转载,但请保留文章原始出处:
         CSDN:
http://www.csdn.net
         雨季o莫忧离:http://blog.csdn.net/luckkof

正文

 



首先,从database中获取歌曲的信息.

然后,应用SimpleAdapter ListActivity中显示这些信息。

 

TrackBrowserActivity中长按一首歌曲,从ContextMenu中选择METADATA_INFO,就会启动MusicMetadataActivity,该Activity会显示歌曲的信息。

 

具体步骤如下:

1, packages\apps\Music\AndroidManifest.xml中添加MusicMetadataActivity:

        <activity android:name="com.android.music.MusicMetadataActivity" android:exported="false"

            android:theme="@android:style/Theme.Holo"

            android:configChanges="orientation|screenSize" />

 

2, packages\apps\Music\res\layout\中新建布局文件music_metadata.xml :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <ListView

        android:id="@android:id/list"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:drawSelectorOnTop="false"

        android:fastScrollEnabled="true"

        android:scrollbars="none"/>

   

    <TextView android:id="@+id/metadata_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="22dip"

        android:textStyle="bold"

        android:paddingLeft="6dip"

        android:paddingTop="3dip"

        android:paddingBottom="1dip"

        android:gravity="center_vertical"/>

   

    <TextView android:id="@+id/metadata_content"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="18dip"

        android:textColor="@color/metadata_content"

        android:paddingLeft="6dip"

        android:paddingTop="1dip"

        android:paddingBottom="1dip"

        android:gravity="bottom"/>

 

</LinearLayout>

 

3, packages\apps\Music\res\values\colors.xml中添加颜色信息:

<color name="metadata_content">#FF808080</color>

 

4, packages\apps\Music\res\values\mtk_strings.xml添加字串,并在各语言中添加对应的字串:

    <string name="music_metadata_info">Music Info</string>

    <string name="music_metadata_artist">Artist</string>

    <string name="music_metadata_tile">Title</string>

    <string name="music_metadata_album">Album</string>

    <string name="music_metadata_duration">Duration</string>

    <string name="music_metadata_genre">Genre</string>

    <string name="music_metadata_recording_date">Recording Date</string>

    <string name="music_metadata_number">Number</string>

    <string name="music_metadata_format">Format</string>

    <string name="music_metadata_size">Size</string>

    <string name="music_metadata_emplacement">Emplacement</string>

    <string name="music_metadata_info_unknown">Unknown</string>

 

5, packages\apps\Music\src\com\android\music\中创建MusicMetadataActivity.java:

package com.android.music;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import android.app.ActionBar;

import android.app.ListActivity;

import android.content.ContentUris;

import android.content.Intent;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.MenuItem;

import android.widget.SimpleAdapter;

 

public class MusicMetadataActivity extends ListActivity{

       private static final String TAG = "MusicMetadata";

 

       private static final String METADATA_NAME = "metadataName";

       private static final String METADATA_CONTENT = "metadataContent";

       private List<Map<String, String>> mListItems = new ArrayList<Map<String, String>>();

       private String mSelectedId;

       private Cursor mCursor;

      

       //Column Index

       private int mTitleIdx;

       private int mArtistIdx;

       private int mAlbumIdx;

       private int mDurationIdx;

       private int mGenreIdx;

       private int mYearIdx;

       private int mTrackNoIdx;

       private int mMimeTypeIdx;

       private int mSizeIdx;

       private int mEmplacementIdx;

 

       //meta data

       private String mTitle;

       private String mArtist;

       private String mAlbum;

       private String mDuration;

       private String mGenre;

       private String mYear;

       private String mTrackNo;

       private String mMimeType;

       private String mSize;

       private String mEmplacement;

      

       public MusicMetadataActivity()

    {

    }

 

    @Override

    public void onCreate(Bundle icicle) {

          MusicLogUtils.d(TAG, "onCreate");

        super.onCreate(icicle);

 

              Intent intent = getIntent();

        if (intent != null) {

            mSelectedId = intent.getStringExtra("selected_id");

        }

              MusicLogUtils.d(TAG, "onCreate: selectedId = " + mSelectedId);

             

              ActionBar actionBar = getActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);

        actionBar.setHomeButtonEnabled(true);

             

        setContentView(R.layout.music_metadata);

 

              //cursor column

              String[] cursorCols = new String[] {

                     MediaStore.Audio.Media._ID,

               MediaStore.Audio.Media.TITLE,

               MediaStore.Audio.Media.ARTIST,

               MediaStore.Audio.Media.ALBUM,

               MediaStore.Audio.Media.DURATION,

               MediaStore.Audio.Media.YEAR,

               MediaStore.Audio.Media.TRACK,

               MediaStore.Audio.Media.MIME_TYPE,

               MediaStore.Audio.Media.SIZE,

               MediaStore.Audio.Media.DATA

           };

 

              Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.valueOf(mSelectedId));

              Cursor mCursor = MusicUtils.query(this, uri, cursorCols, null, null, null);

 

              getColumnIndicesFromDB(mCursor);

              getMetadataFromDB(mCursor);

              getGenre();

 

              getAdapterData();

              setTitle(mTitle);

        initAdapter();

    }

 

       @Override

    public void onDestroy() {

          MusicLogUtils.d(TAG, "onDestroy");

          if(mCursor != null){

                     mCursor.close();

                     mCursor = null;

              }

 

        super.onDestroy();

    }

 

       @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case android.R.id.home:

                /// M: Navigation button press back

                MusicLogUtils.d(TAG, "onOptionsItemSelected: Navigation button press back");

                finish();

                return true;

 

            default:

                break;

        }

        return super.onOptionsItemSelected(item);

    }

 

       private void getColumnIndicesFromDB(Cursor cursor) {

              MusicLogUtils.d(TAG, "getColumnIndices: cursor = " + cursor);

              if(cursor != null){

                     if(cursor.moveToFirst() && cursor.getCount() > 0){

                            mTitleIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);

                            MusicLogUtils.d(TAG, "getColumnIndices: mTitleIdx = " + mTitleIdx);

                            mArtistIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);

                            MusicLogUtils.d(TAG, "getColumnIndices: mArtistIdx = " + mArtistIdx);

                            mAlbumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);

                            MusicLogUtils.d(TAG, "getColumnIndices: mAlbumIdx = " + mAlbumIdx);

                            mDurationIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);

                            MusicLogUtils.d(TAG, "getColumnIndices: mDurationIdx = " + mDurationIdx);

                            mYearIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.YEAR);

                            MusicLogUtils.d(TAG, "getColumnIndices: mYearIdx = " + mYearIdx);

                            mTrackNoIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TRACK);

                            MusicLogUtils.d(TAG, "getColumnIndices: mTrackNoIdx = " + mTrackNoIdx);

                            mMimeTypeIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);

                            MusicLogUtils.d(TAG, "getColumnIndices: mMimeTypeIdx = " + mMimeTypeIdx);

                            mSizeIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);

                            MusicLogUtils.d(TAG, "getColumnIndices: mSizeIdx = " + mSizeIdx);

                            mEmplacementIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);

                            MusicLogUtils.d(TAG, "getColumnIndices: mEmplacementIdx = " + mEmplacementIdx);

                     }

              }

       }

 

       private void getMetadataFromDB(Cursor cursor) {

              MusicLogUtils.d(TAG, "getMetadataFromDB: cursor = " + cursor);

              if(cursor != null){

                     if(cursor.moveToFirst() && cursor.getCount() > 0){

                            mTitle = cursor.getString(mTitleIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: mTitle = " + mTitle);

                           

                            String artist = cursor.getString(mArtistIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: artist = " + artist);

                            if(MediaStore.UNKNOWN_STRING.equals(artist)){

                                   artist = getString(R.string.unknown_artist_name);

                                   MusicLogUtils.d(TAG, "getColumnIndices: artist = " + artist);

                            }

                            mArtist = artist;

                           

                            String album = cursor.getString(mAlbumIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: album = " + album);

                            if(MediaStore.UNKNOWN_STRING.equals(album)){

                                   album = getString(R.string.unknown_album_name);

                                   MusicLogUtils.d(TAG, "getColumnIndices: album = " + album);

                            }

                            mAlbum = album;

                           

                            long duration = cursor.getLong(mDurationIdx);

                            final int secondToMs = 1000;

                            MusicLogUtils.d(TAG, "getColumnIndices: duration = " + duration);

                            mDuration = MusicUtils.makeTimeString(this, duration / secondToMs);

                           

                            int year = cursor.getInt(mYearIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: year = " + year);

                            if(year == 0){

                                   mYear = getString(R.string.music_metadata_info_unknown);

                            }else{

                                   mYear = String.valueOf(year);

                            }

                            MusicLogUtils.d(TAG, "getColumnIndices: mYear = " + mYear);

 

                            int trackNo = cursor.getInt(mTrackNoIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: trackNo = " + trackNo);

                            mTrackNo = String.valueOf(trackNo);

                           

                            mMimeType = cursor.getString(mMimeTypeIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: mMimeType = " + mMimeType);

                           

                            long size = cursor.getLong(mSizeIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: size = " + size);

                            mSize = MusicUtils.makeSizeString(size);

                            MusicLogUtils.d(TAG, "getColumnIndices: mSize = " + mSize);

                           

                            mEmplacement = cursor.getString(mEmplacementIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: mEmplacement = " + mEmplacement);

                     }

              }

       }

 

       private void getGenre(){

              MusicLogUtils.d(TAG, "getGenre");

              String[] cursorGenreCols = new String[]{

                     MediaStore.Audio.Genres._ID,

                     MediaStore.Audio.Genres.NAME

              };

             

              Uri uri = MediaStore.Audio.Genres.getContentUriForAudioId("external", Integer.valueOf(mSelectedId));

              MusicLogUtils.d(TAG, "getGenre: uri = " + uri);

 

              String genre = null;

              Cursor genreCursor = MusicUtils.query(this, uri, cursorGenreCols, null, null, null);

              MusicLogUtils.d(TAG, "getGenre: genreCursor = " + genreCursor);

 

              if(genreCursor != null){

                     if(genreCursor.moveToFirst() && genreCursor.getCount() > 0){

                            int genreIdx = genreCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);

                            MusicLogUtils.d(TAG, "getGenre: nameIdx = " + genreIdx);

                            genre = genreCursor.getString(genreIdx);

                            MusicLogUtils.d(TAG, "getGenre: genre = " + genre);

                     }

                     genreCursor.close();

                     genreCursor = null;

              }

              MusicLogUtils.d(TAG, "getGenre: genreCursor");

              if(genre == null){

                     mGenre = getString(R.string.music_metadata_info_unknown);

              }else{

                     mGenre = genre;

              }

              MusicLogUtils.d(TAG, "getGenre: mGenre = " + mGenre);

       }

 

       private void getAdapterData(){

              MusicLogUtils.d(TAG, "getAdapterData");

 

              //metadata name string id

              int[] metadataName = new int[]{

                     R.string.music_metadata_tile,

                     R.string.music_metadata_artist,

                     R.string.music_metadata_album,

                     R.string.music_metadata_duration,

                     R.string.music_metadata_genre,

                     R.string.music_metadata_recording_date,

                     R.string.music_metadata_number,

                     R.string.music_metadata_format,

                     R.string.music_metadata_size,

                     R.string.music_metadata_emplacement

              };

             

              //metadata content string

              String[] metadataContent = new String[]{

                     mTitle,

                     mArtist,

                     mAlbum,

                     mDuration,

                     mGenre,

                     mYear,

                     mTrackNo,

                     mMimeType,

                     mSize,

                     mEmplacement

              };

 

              for(int i = 0; i < metadataName.length; i++){

             Map<String, String> listItem = new HashMap<String, String>();

             listItem.put(METADATA_NAME, getString(metadataName[i]));

             listItem.put(METADATA_CONTENT, metadataContent[i]);

             mListItems.add(listItem);

        }

       }

 

       private void initAdapter(){

              MusicLogUtils.d(TAG, "initAdapter");

              String[] from = new String[]{METADATA_NAME, METADATA_CONTENT};

              int[] to = new int[]{R.id.metadata_name, R.id.metadata_content};

              SimpleAdapter simpleAdapter = new SimpleAdapter(this, mListItems, R.layout.music_metadata, from, to);

      

              setListAdapter(simpleAdapter);

       }

}

 

6, packages\apps\Music\src\com\android\music\ MusicUtils.java中添加转换时间格式的方法:

    public static final String UNIT_B = "B";

    public static final String UNIT_KB = "KB";

    public static final String UNIT_MB = "MB";

    public static final String UNIT_GB = "GB";

    public static final String UNIT_TB = "TB";

    private static final int UNIT_INTERVAL = 1024;

    private static final double ROUNDING_OFF = 0.005;

    private static final int DECIMAL_NUMBER = 100;

      

       /**

     * This method converts a size to a string

     *

     * @param size the size of a file

     * @return the string represents the size

     */

    public static String makeSizeString(long size) {

        String unit = UNIT_B;

        if (size < DECIMAL_NUMBER) {

            MusicLogUtils.d(TAG, "makeSizeString(),size = " + size);

            return Long.toString(size) + " " + unit;

        }

       

        unit = UNIT_KB;

        double sizeDouble = (double) size / (double) UNIT_INTERVAL;

        if (sizeDouble > UNIT_INTERVAL) {

            sizeDouble = (double) sizeDouble / (double) UNIT_INTERVAL;

            unit = UNIT_MB;

        }

        if (sizeDouble > UNIT_INTERVAL) {

            sizeDouble = (double) sizeDouble / (double) UNIT_INTERVAL;

            unit = UNIT_GB;

        }

        if (sizeDouble > UNIT_INTERVAL) {

            sizeDouble = (double) sizeDouble / (double) UNIT_INTERVAL;

            unit = UNIT_TB;

        }

 

        // Add 0.005 for rounding-off.

        long sizeInt = (long) ((sizeDouble + ROUNDING_OFF) * DECIMAL_NUMBER); // strict to two

        // decimal places

        double formatedSize = ((double) sizeInt) / DECIMAL_NUMBER;

        MusicLogUtils.d(TAG, "makeSizeString(): " + formatedSize + unit);

 

        if (formatedSize == 0) {

            return "0" + " " + unit;

        } else {

            return Double.toString(formatedSize) + " " + unit;

        }

    }

 

7, packages\apps\Music\src\com\android\music\ TrackBrowserActivity.java中添加启动MusicMetadataActivity的入口:

a, 定义启动MusicMetadataActivitymenu item:

       private static final int METADATA_INFO = CHILD_MENU_BASE + 10;

 

b, onCreateContextMenu()中添加该menu item:

              menu.add(0, METADATA_INFO, 0, R.string.music_metadata_info);

 

c, onContextItemSelected()执行启动MusicMetadataActivity的操作:

                     case METADATA_INFO:

                Intent intent = new Intent();

                            intent.setClass(this, MusicMetadataActivity.class);

                            intent.putExtra("selected_id", String.valueOf(mSelectedId));

                            MusicLogUtils.d(TAG, "METADATA_INFO: mSelectedPosition = " + mSelectedPosition);

                            MusicLogUtils.d(TAG, "METADATA_INFO: mSelectedId = " + mSelectedId);

                startActivityForResult(intent, -1);

                return true;

 


首先,从database中获取歌曲的信息.

然后,应用SimpleAdapter ListActivity中显示这些信息。

 

TrackBrowserActivity中长按一首歌曲,从ContextMenu中选择METADATA_INFO,就会启动MusicMetadataActivity,该Activity会显示歌曲的信息。

 

具体步骤如下:

1, packages\apps\Music\AndroidManifest.xml中添加MusicMetadataActivity:

        <activity android:name="com.android.music.MusicMetadataActivity" android:exported="false"

            android:theme="@android:style/Theme.Holo"

            android:configChanges="orientation|screenSize" />

 

2, packages\apps\Music\res\layout\中新建布局文件music_metadata.xml :

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="fill_parent"

    android:layout_height="fill_parent"

    android:orientation="vertical" >

 

    <ListView

        android:id="@android:id/list"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:drawSelectorOnTop="false"

        android:fastScrollEnabled="true"

        android:scrollbars="none"/>

   

    <TextView android:id="@+id/metadata_name"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="22dip"

        android:textStyle="bold"

        android:paddingLeft="6dip"

        android:paddingTop="3dip"

        android:paddingBottom="1dip"

        android:gravity="center_vertical"/>

   

    <TextView android:id="@+id/metadata_content"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:textSize="18dip"

        android:textColor="@color/metadata_content"

        android:paddingLeft="6dip"

        android:paddingTop="1dip"

        android:paddingBottom="1dip"

        android:gravity="bottom"/>

 

</LinearLayout>

 

3, packages\apps\Music\res\values\colors.xml中添加颜色信息:

<color name="metadata_content">#FF808080</color>

 

4, packages\apps\Music\res\values\mtk_strings.xml添加字串,并在各语言中添加对应的字串:

    <string name="music_metadata_info">Music Info</string>

    <string name="music_metadata_artist">Artist</string>

    <string name="music_metadata_tile">Title</string>

    <string name="music_metadata_album">Album</string>

    <string name="music_metadata_duration">Duration</string>

    <string name="music_metadata_genre">Genre</string>

    <string name="music_metadata_recording_date">Recording Date</string>

    <string name="music_metadata_number">Number</string>

    <string name="music_metadata_format">Format</string>

    <string name="music_metadata_size">Size</string>

    <string name="music_metadata_emplacement">Emplacement</string>

    <string name="music_metadata_info_unknown">Unknown</string>

 

5, packages\apps\Music\src\com\android\music\中创建MusicMetadataActivity.java:

package com.android.music;

 

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

 

import android.app.ActionBar;

import android.app.ListActivity;

import android.content.ContentUris;

import android.content.Intent;

import android.database.Cursor;

import android.net.Uri;

import android.os.Bundle;

import android.provider.MediaStore;

import android.view.MenuItem;

import android.widget.SimpleAdapter;

 

public class MusicMetadataActivity extends ListActivity{

       private static final String TAG = "MusicMetadata";

 

       private static final String METADATA_NAME = "metadataName";

       private static final String METADATA_CONTENT = "metadataContent";

       private List<Map<String, String>> mListItems = new ArrayList<Map<String, String>>();

       private String mSelectedId;

       private Cursor mCursor;

      

       //Column Index

       private int mTitleIdx;

       private int mArtistIdx;

       private int mAlbumIdx;

       private int mDurationIdx;

       private int mGenreIdx;

       private int mYearIdx;

       private int mTrackNoIdx;

       private int mMimeTypeIdx;

       private int mSizeIdx;

       private int mEmplacementIdx;

 

       //meta data

       private String mTitle;

       private String mArtist;

       private String mAlbum;

       private String mDuration;

       private String mGenre;

       private String mYear;

       private String mTrackNo;

       private String mMimeType;

       private String mSize;

       private String mEmplacement;

      

       public MusicMetadataActivity()

    {

    }

 

    @Override

    public void onCreate(Bundle icicle) {

          MusicLogUtils.d(TAG, "onCreate");

        super.onCreate(icicle);

 

              Intent intent = getIntent();

        if (intent != null) {

            mSelectedId = intent.getStringExtra("selected_id");

        }

              MusicLogUtils.d(TAG, "onCreate: selectedId = " + mSelectedId);

             

              ActionBar actionBar = getActionBar();

        actionBar.setDisplayHomeAsUpEnabled(true);

        actionBar.setHomeButtonEnabled(true);

             

        setContentView(R.layout.music_metadata);

 

              //cursor column

              String[] cursorCols = new String[] {

                     MediaStore.Audio.Media._ID,

               MediaStore.Audio.Media.TITLE,

               MediaStore.Audio.Media.ARTIST,

               MediaStore.Audio.Media.ALBUM,

               MediaStore.Audio.Media.DURATION,

               MediaStore.Audio.Media.YEAR,

               MediaStore.Audio.Media.TRACK,

               MediaStore.Audio.Media.MIME_TYPE,

               MediaStore.Audio.Media.SIZE,

               MediaStore.Audio.Media.DATA

           };

 

              Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, Long.valueOf(mSelectedId));

              Cursor mCursor = MusicUtils.query(this, uri, cursorCols, null, null, null);

 

              getColumnIndicesFromDB(mCursor);

              getMetadataFromDB(mCursor);

              getGenre();

 

              getAdapterData();

              setTitle(mTitle);

        initAdapter();

    }

 

       @Override

    public void onDestroy() {

          MusicLogUtils.d(TAG, "onDestroy");

          if(mCursor != null){

                     mCursor.close();

                     mCursor = null;

              }

 

        super.onDestroy();

    }

 

       @Override

    public boolean onOptionsItemSelected(MenuItem item) {

        switch (item.getItemId()) {

            case android.R.id.home:

                /// M: Navigation button press back

                MusicLogUtils.d(TAG, "onOptionsItemSelected: Navigation button press back");

                finish();

                return true;

 

            default:

                break;

        }

        return super.onOptionsItemSelected(item);

    }

 

       private void getColumnIndicesFromDB(Cursor cursor) {

              MusicLogUtils.d(TAG, "getColumnIndices: cursor = " + cursor);

              if(cursor != null){

                     if(cursor.moveToFirst() && cursor.getCount() > 0){

                            mTitleIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);

                            MusicLogUtils.d(TAG, "getColumnIndices: mTitleIdx = " + mTitleIdx);

                            mArtistIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST);

                            MusicLogUtils.d(TAG, "getColumnIndices: mArtistIdx = " + mArtistIdx);

                            mAlbumIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM);

                            MusicLogUtils.d(TAG, "getColumnIndices: mAlbumIdx = " + mAlbumIdx);

                            mDurationIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION);

                            MusicLogUtils.d(TAG, "getColumnIndices: mDurationIdx = " + mDurationIdx);

                            mYearIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.YEAR);

                            MusicLogUtils.d(TAG, "getColumnIndices: mYearIdx = " + mYearIdx);

                            mTrackNoIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TRACK);

                            MusicLogUtils.d(TAG, "getColumnIndices: mTrackNoIdx = " + mTrackNoIdx);

                            mMimeTypeIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.MIME_TYPE);

                            MusicLogUtils.d(TAG, "getColumnIndices: mMimeTypeIdx = " + mMimeTypeIdx);

                            mSizeIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);

                            MusicLogUtils.d(TAG, "getColumnIndices: mSizeIdx = " + mSizeIdx);

                            mEmplacementIdx = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);

                            MusicLogUtils.d(TAG, "getColumnIndices: mEmplacementIdx = " + mEmplacementIdx);

                     }

              }

       }

 

       private void getMetadataFromDB(Cursor cursor) {

              MusicLogUtils.d(TAG, "getMetadataFromDB: cursor = " + cursor);

              if(cursor != null){

                     if(cursor.moveToFirst() && cursor.getCount() > 0){

                            mTitle = cursor.getString(mTitleIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: mTitle = " + mTitle);

                           

                            String artist = cursor.getString(mArtistIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: artist = " + artist);

                            if(MediaStore.UNKNOWN_STRING.equals(artist)){

                                   artist = getString(R.string.unknown_artist_name);

                                   MusicLogUtils.d(TAG, "getColumnIndices: artist = " + artist);

                            }

                            mArtist = artist;

                           

                            String album = cursor.getString(mAlbumIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: album = " + album);

                            if(MediaStore.UNKNOWN_STRING.equals(album)){

                                   album = getString(R.string.unknown_album_name);

                                   MusicLogUtils.d(TAG, "getColumnIndices: album = " + album);

                            }

                            mAlbum = album;

                           

                            long duration = cursor.getLong(mDurationIdx);

                            final int secondToMs = 1000;

                            MusicLogUtils.d(TAG, "getColumnIndices: duration = " + duration);

                            mDuration = MusicUtils.makeTimeString(this, duration / secondToMs);

                           

                            int year = cursor.getInt(mYearIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: year = " + year);

                            if(year == 0){

                                   mYear = getString(R.string.music_metadata_info_unknown);

                            }else{

                                   mYear = String.valueOf(year);

                            }

                            MusicLogUtils.d(TAG, "getColumnIndices: mYear = " + mYear);

 

                            int trackNo = cursor.getInt(mTrackNoIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: trackNo = " + trackNo);

                            mTrackNo = String.valueOf(trackNo);

                           

                            mMimeType = cursor.getString(mMimeTypeIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: mMimeType = " + mMimeType);

                           

                            long size = cursor.getLong(mSizeIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: size = " + size);

                            mSize = MusicUtils.makeSizeString(size);

                            MusicLogUtils.d(TAG, "getColumnIndices: mSize = " + mSize);

                           

                            mEmplacement = cursor.getString(mEmplacementIdx);

                            MusicLogUtils.d(TAG, "getColumnIndices: mEmplacement = " + mEmplacement);

                     }

              }

       }

 

       private void getGenre(){

              MusicLogUtils.d(TAG, "getGenre");

              String[] cursorGenreCols = new String[]{

                     MediaStore.Audio.Genres._ID,

                     MediaStore.Audio.Genres.NAME

              };

             

              Uri uri = MediaStore.Audio.Genres.getContentUriForAudioId("external", Integer.valueOf(mSelectedId));

              MusicLogUtils.d(TAG, "getGenre: uri = " + uri);

 

              String genre = null;

              Cursor genreCursor = MusicUtils.query(this, uri, cursorGenreCols, null, null, null);

              MusicLogUtils.d(TAG, "getGenre: genreCursor = " + genreCursor);

 

              if(genreCursor != null){

                     if(genreCursor.moveToFirst() && genreCursor.getCount() > 0){

                            int genreIdx = genreCursor.getColumnIndexOrThrow(MediaStore.Audio.Genres.NAME);

                            MusicLogUtils.d(TAG, "getGenre: nameIdx = " + genreIdx);

                            genre = genreCursor.getString(genreIdx);

                            MusicLogUtils.d(TAG, "getGenre: genre = " + genre);

                     }

                     genreCursor.close();

                     genreCursor = null;

              }

              MusicLogUtils.d(TAG, "getGenre: genreCursor");

              if(genre == null){

                     mGenre = getString(R.string.music_metadata_info_unknown);

              }else{

                     mGenre = genre;

              }

              MusicLogUtils.d(TAG, "getGenre: mGenre = " + mGenre);

       }

 

       private void getAdapterData(){

              MusicLogUtils.d(TAG, "getAdapterData");

 

              //metadata name string id

              int[] metadataName = new int[]{

                     R.string.music_metadata_tile,

                     R.string.music_metadata_artist,

                     R.string.music_metadata_album,

                     R.string.music_metadata_duration,

                     R.string.music_metadata_genre,

                     R.string.music_metadata_recording_date,

                     R.string.music_metadata_number,

                     R.string.music_metadata_format,

                     R.string.music_metadata_size,

                     R.string.music_metadata_emplacement

              };

             

              //metadata content string

              String[] metadataContent = new String[]{

                     mTitle,

                     mArtist,

                     mAlbum,

                     mDuration,

                     mGenre,

                     mYear,

                     mTrackNo,

                     mMimeType,

                     mSize,

                     mEmplacement

              };

 

              for(int i = 0; i < metadataName.length; i++){

             Map<String, String> listItem = new HashMap<String, String>();

             listItem.put(METADATA_NAME, getString(metadataName[i]));

             listItem.put(METADATA_CONTENT, metadataContent[i]);

             mListItems.add(listItem);

        }

       }

 

       private void initAdapter(){

              MusicLogUtils.d(TAG, "initAdapter");

              String[] from = new String[]{METADATA_NAME, METADATA_CONTENT};

              int[] to = new int[]{R.id.metadata_name, R.id.metadata_content};

              SimpleAdapter simpleAdapter = new SimpleAdapter(this, mListItems, R.layout.music_metadata, from, to);

      

              setListAdapter(simpleAdapter);

       }

}

 

6, packages\apps\Music\src\com\android\music\ MusicUtils.java中添加转换时间格式的方法:

    public static final String UNIT_B = "B";

    public static final String UNIT_KB = "KB";

    public static final String UNIT_MB = "MB";

    public static final String UNIT_GB = "GB";

    public static final String UNIT_TB = "TB";

    private static final int UNIT_INTERVAL = 1024;

    private static final double ROUNDING_OFF = 0.005;

    private static final int DECIMAL_NUMBER = 100;

      

       /**

     * This method converts a size to a string

     *

     * @param size the size of a file

     * @return the string represents the size

     */

    public static String makeSizeString(long size) {

        String unit = UNIT_B;

        if (size < DECIMAL_NUMBER) {

            MusicLogUtils.d(TAG, "makeSizeString(),size = " + size);

            return Long.toString(size) + " " + unit;

        }

       

        unit = UNIT_KB;

        double sizeDouble = (double) size / (double) UNIT_INTERVAL;

        if (sizeDouble > UNIT_INTERVAL) {

            sizeDouble = (double) sizeDouble / (double) UNIT_INTERVAL;

            unit = UNIT_MB;

        }

        if (sizeDouble > UNIT_INTERVAL) {

            sizeDouble = (double) sizeDouble / (double) UNIT_INTERVAL;

            unit = UNIT_GB;

        }

        if (sizeDouble > UNIT_INTERVAL) {

            sizeDouble = (double) sizeDouble / (double) UNIT_INTERVAL;

            unit = UNIT_TB;

        }

 

        // Add 0.005 for rounding-off.

        long sizeInt = (long) ((sizeDouble + ROUNDING_OFF) * DECIMAL_NUMBER); // strict to two

        // decimal places

        double formatedSize = ((double) sizeInt) / DECIMAL_NUMBER;

        MusicLogUtils.d(TAG, "makeSizeString(): " + formatedSize + unit);

 

        if (formatedSize == 0) {

            return "0" + " " + unit;

        } else {

            return Double.toString(formatedSize) + " " + unit;

        }

    }

 

7, packages\apps\Music\src\com\android\music\ TrackBrowserActivity.java中添加启动MusicMetadataActivity的入口:

a, 定义启动MusicMetadataActivitymenu item:

       private static final int METADATA_INFO = CHILD_MENU_BASE + 10;

 

b, onCreateContextMenu()中添加该menu item:

              menu.add(0, METADATA_INFO, 0, R.string.music_metadata_info);

 

c, onContextItemSelected()执行启动MusicMetadataActivity的操作:

                     case METADATA_INFO:

                Intent intent = new Intent();

                            intent.setClass(this, MusicMetadataActivity.class);

                            intent.putExtra("selected_id", String.valueOf(mSelectedId));

                            MusicLogUtils.d(TAG, "METADATA_INFO: mSelectedPosition = " + mSelectedPosition);

                            MusicLogUtils.d(TAG, "METADATA_INFO: mSelectedId = " + mSelectedId);

                startActivityForResult(intent, -1);

                return true;