【问题标题】:How can I get cover arts embedded in audio files using a cursor?如何使用光标将封面艺术嵌入到音频文件中?
【发布时间】:2014-06-12 15:56:40
【问题描述】:

我正在开发一个很小的音频播放器。问题是:在许多问题之后,我设法用从外部存储中的任何文件夹中检索到的歌曲构建了一个 ListView,并用标题、艺术家和专辑名称列出它们。现在我想在 ListView 中添加封面专辑。封面必须取自歌曲音频文件中嵌入的图片。

我尝试使用 MediaMetadataRetriever,但无法获取每个文件的完整 Uri,因此无法为其设置数据源。我怎样才能得到封面?如果我有字节数组,我会使用 BitmapFactory……但我没有。

顺便说一句,这是我的代码... 在活动中,这是在外部存储中搜索音频文件并将它们放入列表中的空白:

public void retrieveAudioFiles(){
        songsList = new SongsList();

        Uri sd = Audio.Media.EXTERNAL_CONTENT_URI;
        String[] cols = {Audio.Media.TITLE,Audio.Media.ARTIST,Audio.Media.ALBUM};
        String where = Audio.Media.IS_MUSIC;
        Cursor audioCursor = getContentResolver().query(sd,cols,where,null,null);

        while (audioCursor.moveToNext()){
            int posColTitle = audioCursor.getColumnIndex(Audio.Media.TITLE);
            int posColArtist = audioCursor.getColumnIndex(Audio.Media.ARTIST);
            int posColAlbum = audioCursor.getColumnIndex(Audio.Media.ALBUM);

            String songTitle = audioCursor.getString(posColTitle);
            String songArtist = audioCursor.getString(posColArtist);
            String songAlbum = audioCursor.getString(posColAlbum);

            songsList.add(new Song(songTitle,songArtist,songAlbum));
            }
        audioCursor.close();
    }

这是适配器:

public class SongsAdapter extends BaseAdapter {

    private SongsList songsList;
    private LayoutInflater songInf;

    public SongsAdapter(Context c, SongsList theSongs){
        super();  
        songsList=theSongs;
        songInf=LayoutInflater.from(c);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return songsList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return songsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        RowWrapper wrapper;
        if (convertView == null)
        {
            convertView = songInf.inflate(
                R.layout.song_row, null);
            wrapper = new RowWrapper(convertView);
            convertView.setTag(wrapper);
        }
        else
        {
            wrapper = (RowWrapper) convertView.getTag();
        }
        Song song = (Song) getItem(position);
        wrapper.populate(song);

        return convertView;
    }

    private static class RowWrapper
    {
        private TextView titleTextView;
        private TextView artistTextView;
        private TextView albumTextView;
        //private ImageView coverImageView;

        public RowWrapper(View convertView)
        {
            titleTextView = (TextView) convertView.findViewById(R.id.textTitle);
            artistTextView = (TextView) convertView.findViewById(R.id.textArtist);
            albumTextView = (TextView) convertView.findViewById(R.id.textAlbum);
            //coverImageView = (ImageView) convertView.findViewById(R.id.smallCover);
        }

        public void populate(Song song)
        {
            titleTextView.setText(song.title);
            artistTextView.setText(song.artist);
            albumTextView.setText(song.album);
            //if (song.cover != null)
            //coverImageView.setImageBitmap(song.cover);
        }
    }

}

这是 Song 类:

public class Song {

    public String title="";
    public String artist="";
    public String album="";
    public Bitmap cover=null;

    public Song(String t, String ar, String al){
        title=t;
        artist=ar;
        album=al;
        //cover=c;
    }

    public Song(){

    }

}

这是 SongsList 类(歌曲的简单 ArrayList):

public class SongsList extends ArrayList<Song> {

    public SongsList(){
        super();
    }

}

这是清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.audioplayer"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.audioplayer.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

这是ListActivity单行的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:orientation="vertical"
    android:onClick="songPicked" >

    <ImageView
        android:id="@+id/smallCover"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="@string/coverDescription"
        android:src="@drawable/no_cover" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/labelTitle" />

        <TextView
            android:id="@+id/textTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="@string/textTitle" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <TextView
            android:id="@+id/labelArtist"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="0dp"
            android:text="@string/labelArtist" />

        <TextView
            android:id="@+id/textArtist"
            android:layout_width="150dp"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="@string/textArtist" />

        <TextView
            android:id="@+id/labelAlbum"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="@string/labelAlbum" />

        <TextView
            android:id="@+id/textAlbum"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="5dp"
            android:text="@string/textAlbum" />
    </LinearLayout>

</LinearLayout>

希望你能帮助我...

【问题讨论】:

    标签: android android-contentprovider android-cursor


    【解决方案1】:

    我自己做的……

    我在 Song 类中为 id 添加了一个长参数,为路径添加了一个 Uri 参数。

    在活动中,就在songsList.add(new Song(......))之前(我在构造函数中添加了Bitmap参数),我添加了这些指令:

    int posColId = audioCursor.getColumnIndex(Audio.Media._ID);
    long songId = audioCursor.getLong(posColId);
    Uri songUri = ContentUris.withAppendedId(Audio.Media.EXTERNAL_CONTENT_URI,songId);
    String[] dataColumn = {Audio.Media.DATA};
    Cursor coverCursor = getContentResolver().query(songUri, dataColumn, null, null, null);
    coverCursor.moveToFirst();
    int dataIndex = coverCursor.getColumnIndex(Audio.Media.DATA);
    String filePath = coverCursor.getString(dataIndex);
    coverCursor.close();
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(filePath);
    byte[] coverBytes = retriever.getEmbeddedPicture();
    Bitmap songCover;
    if (coverBytes!=null) //se l'array di byte non è vuoto, crea una bitmap
        songCover = BitmapFactory.decodeByteArray(coverBytes, 0, coverBytes.length);
    else
        songCover=null;
    

    然后我取消了 Song 类和 Adapter 中的说明。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2015-11-11
      • 1970-01-01
      相关资源
      最近更新 更多