2014-04-27 1 views
2

아주 작은 오디오 플레이어를 개발 중입니다. 문제는 다음과 같습니다. 많은 문제 이후에 외부 저장소의 폴더에서 가져온 노래로 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(); 
    } 

이 어댑터는 다음과 같습니다 BTW

, 이것은 내 코드 ... 활동에서 이며, 이것은 외부 저장 장치에있는 오디오 파일을 검색하여 목록에 넣어 무효입니다 :

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입니다 :

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); 
     } 
    } 

} 

는 노래 클래스입니다 클래스 (노래의 간단한 ArrayList를이) :
<?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> 

은 당신이 할 수있는 희망이는 ListActivity의 단일 행에 대한 레이아웃이

<?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> 

입니다 :

public class SongsList extends ArrayList<Song> { 

    public SongsList(){ 
     super(); 
    } 

} 

이 매니페스트입니다 도와주세요 ...

답변

1

나는 내 자신으로 만들었습니다.

id에 긴 매개 변수를, Song 클래스에 경로에 Uri 매개 변수를 추가했습니다. 활동에

, 단지 songsList.add 전에 (새 노래 (...)) (I 생성자에서 비트 맵 매개 변수를 추가), I는이 지침에 추가 : 다음

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; 

를 I 노래 클래스 및 어댑터의 지침에 대한 주석 처리를 제거합니다.