2017-12-16 9 views
0

나는 안드로이드에서 커서가 어떻게 작동하는지 매우 새롭다. 현재 앨범의 이름과 아티스트를 검색 할 수 있지만 장르를 검색 할 수는 없습니다. 주변을 둘러 보았고이를 수행 할 방법을 찾을 수 없습니다. 방금 앨범의 모든 노래를 확인하고, 가장 일반적인 장르를 찾고,이를 기반으로 앨범 장르를 지정해야합니까, 아니면이를 위해보다 우아한 방법이 있습니까? 일반적으로 앨범의 모든 노래는 동일한 장르를 갖지만 항상 그런 것은 아닙니다.MediaStore로 앨범 장르 가져 오기

감사합니다.

+0

에 수집 된 모든 데이터를 표시하는 데 사용됩니다 당신이 https://developer.android.com/reference/android/media/MediaMetadataRetriever ([MediaMetaDataRetriever]와 보셨습니까 .html)? 'MediaMetadataRetriever media = new MediaMetadataRetriever(); media.setDataSource ("file.mp3"); media.extractMetadata (METADATA_KEY_GENRE); ' –

+0

앨범 장르가 아닌 노래 장르에 해당합니다. – Eamonn

답변

1

당신이 원한다면 당신은 MetaDataRetriever을 사용할 수 있습니다 ... 그것은 (내 경험) 노래에서 메타 데이터를 검색 할 수있는보다 완벽한 방법 (이 링크 https://developer.android.com/reference/android/media/MediaMetadataRetriever.html 더 볼 수 있습니다) ... 나는이 같은 것을 썼다 내 사업이 당신을 도울 수 있기를 바랍니다 :

public void MetSongRetriever() { 

    //MediaPlayerPlay is the context, I shared a function i wrote myself 
    //Just like myUri is the path of the song (external storage in my case) 
    mMediaData.setDataSource(MediaPlayerPlay.this, myUri); 

    try { 

     mNameSong.setText(mMediaData.extractMetadata(MediaMetadataRetriever.METADATA_KEY_TITLE)); 
     mBandName.setText(mMediaData.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ARTIST)); 
     art = metaRetriver.getEmbeddedPicture(); 
     Bitmap songImage = BitmapFactory.decodeByteArray(art, 0, art.length); 
     mCoverImage.setImageBitmap(songImage);    

     mAlbumName.setText(metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_ALBUM)); 
     mGenre.setText(metaRetriver.extractMetadata(MediaMetadataRetriever.METADATA_KEY_GENRE)); 

    } 
    catch (Exception e) { 

     //set all the text Uknown, like "Unknown title" and so on 
     //And the background for the image grey 
    } 
} 

업데이트 : 내가 제대로 이해하면 글쎄, 당신은 제목, 앨범, 밴드, 장르 등의 정보를 보여주고 싶어하고 그래서 각 노래에 오른쪽 목록? 저는 보통 ListView를 사용합니다 ... ListView +를 사용하여 정보를 정리하는 + 사용자 정의 클래스를 만들고 ArrayAdapter를 사용하여 클래스 구조를 상속하고 ListView에 표시 할 수 있습니다 (제목, 밴드 앨범 내 경우에 노래가 저장된 경로, 외부 저장, 당신은)이 라인을 다음 당신이 원하는

public class Songs { 

public String mSongname, mBandName, mAlbumName, mPathSong; 


public Songs(String songName, String bandName, String albumName, String pathSong) { 

    mSongname = songName; 
    mBandName = bandName; 
    mAlbumName = albumName; 
    mPathSong = pathSong; 

} 


public void setSongname(String songname) { 
    mSongname = songname; 
} 

public void setBandName(String bandName) { 
    mBandName = bandName; 
} 

public void setAlbumName(String albumName) { 
    mAlbumName = albumName; 
} 

public void setPathSong(String pathSong) { 
    mPathSong = pathSong; 
} 


public String getSongname() { 

    return mSongname; 
} 

public String getBandName() { 

    return mBandName; 
} 

public String getAlbumName() { 

    return mAlbumName; 
} 

public String getPathSong() { 

    return mPathSong; 
} 

SongsAdapter.java

Songs.java을 추가 할 수 있습니다

public class SongsAdapter extends ArrayAdapter<Songs> { public SongsAdapter(Context context, ArrayList<Songs> mySongs) { super(context, 0, mySongs); } @Override public View getView(int position, View convertView, ViewGroup parent) { //Storing the data from the current position Songs mySongs = getItem(position); //Check if the convertview is reused, otherwise i load it if(convertView == null) { convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_songs, parent, false); } //Filling the structure with data TextView SongTitle = (TextView) convertView.findViewById(R.id.ID_item_songs_title); TextView BandName = (TextView) convertView.findViewById(R.id.ID_item_songs_band); TextView AlbumName = (TextView) convertView.findViewById(R.id.ID_item_songs_album); TextView PathName = (TextView) convertView.findViewById(R.id.ID_item_songs_path); //Inserting the data in the template view SongTitle.setText(mySongs.mSongname); BandName.setText(mySongs.mBandName); AlbumName.setText(mySongs.mAlbumName); PathName.setText(mySongs.mPathSong); //Return of the view for visualisation purpose return convertView; } } 

다음 하나는 내가

public void getMusicList() { 

    int i = 0; 

    //I used the content resolver to get access to another part of the device, in my case the external storage 
    ContentResolver mContentResolver = getContentResolver(); 

    //Taking the external storage general path 
    Uri mSongUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 

    //The cursor is used to point to a certain row in the temp database, so you can store just parts of it and not the whole database (efficiency maxed) 
     Cursor mSongCursor = getContentResolver().query(mSongUri, null, null, null, null, null); 

    if ((mSongCursor != null) && mSongCursor.moveToFirst()) { 

     //Gathering the index of each column for each info i want 
     int mTempSongTitle = mSongCursor.getColumnIndex(MediaStore.Audio.Media.TITLE); 
     int mTempBand = mSongCursor.getColumnIndex(MediaStore.Audio.Media.ARTIST); 
     int mTempAlbum = mSongCursor.getColumnIndex(MediaStore.Audio.Media.ALBUM); 
     int mTempLocationPath = mSongCursor.getColumnIndex(MediaStore.Audio.Media.DATA); 


     do { 

      //Gathering the real info based on the index 
      String mCurrentTitle = mSongCursor.getString(mTempSongTitle); 
      String mCurrentBand = mSongCursor.getString(mTempBand); 
      String mCurrentAlbum = mSongCursor.getString(mTempAlbum); 
      String mCurrentPath = mSongCursor.getString(mTempLocationPath); 

      mListOfPaths[i] = mCurrentPath; 
      i++; 

      Songs newSong = new Songs(mCurrentTitle, mCurrentBand, mCurrentAlbum, mCurrentPath); 
      adapter.add(newSong); 


     } while (mSongCursor.moveToNext()); 
    } 
} 
(난 그냥, 문자열 배열에있는 모든 노래 '경로를 저장하는 다른 활동에 사용할 수 있도록, 당신을 위해 유용하지 인덱스를 사용) 데이터를 검색하는 데 사용되는 기능입니다

그리고이 마지막 기능이 마지막으로 이전 기능

 public void doStuff() { 

    mSongNames = (ListView) findViewById(R.id.ID_MPM_listView); 
    mSongNames.setAdapter(adapter); 

    getMusicList(); 

    //The event it will occur when you click on an item of the listView 

    mSongNames.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { 


      Intent mMediaPlayerPlayIntent = new Intent(MediaPlayerMain.this, MediaPlayerPlay.class); 

      mMediaPlayerPlayIntent.putExtra("ExtraPosition", position); 
      mMediaPlayerPlayIntent.putExtra("StringArray", mListOfPaths); 

      startActivity(mMediaPlayerPlayIntent); 

     } 
    }); 
} 
+0

고마워요! 나는 이것을 지금 시험해보고 내가 어떻게 나아갈지를 볼 것이다. 상세하고 잘 주석 처리 된 코드 스 니펫에 감사드립니다. – Eamonn

+0

도와 주셔서 감사합니다! 나는 누군가가 나에게 내가 원하는 것을 임시적으로 말해 줄 때 appreaciate한다. 그래서 나는 똑같이하는 경향이있다! 이게 작동하는지 알려주세요 :) – alesssz

+0

MediaMetadataRetriever에 대한 연구를 조금했는데 유용하고 나중에 나중에 문제를 해결할 수 있다고 생각합니다. 목록 (특히 RecyclerView)에 앨범 정보를 표시해야합니다. 특히 사용자 기기의 모든 앨범은 Google Play 뮤직과 유사합니다. MediaMetadataRetriever를 사용하여이 작업을 수행 할 수 있습니까? 아니면 개별 앨범/노래에만 해당합니까? – Eamonn

관련 문제