如何使用getExternalStorageDirectory()从内部存储加载多首歌曲?

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (124)

我正在开发定制的MediaPlayer应用程序,它必须从SD卡和内部存储加载歌曲。我到处寻找方法从内部存储加载歌曲,我查看的大部分帖子都是用Environment.getExternalStorageDirectory()用于加载单个MP3文件,而不是多个。我可以从SD卡(存储卡)加载所有歌曲,但不能从内部存储加载。是否有任何方法来加载存储在内部存储中的所有歌曲?

我的代码

 private void loadSongs() {
    Uri uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI;

    Uri uriPath;

    String MEDIA_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/";

    String selection = MediaStore.Audio.Media.IS_MUSIC + "!=0";
    Cursor cursor = getContentResolver().query(uri, null, selection, null, null);
    if (cursor != null) {
        if (cursor.moveToFirst()) {
            do {
                String name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
                String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
                String url = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));

                Log.i("DATA111", MEDIA_PATH);

                mediaMetadataRetrieve = new MediaMetadataRetriever();
                mediaMetadataRetrieve.setDataSource(MEDIA_PATH + "/Music/");

                art = mediaMetadataRetrieve.getEmbeddedPicture();

                if (art != null) {
                    songImage = BitmapFactory.decodeByteArray(art, 0, art.length);
                }

                SongInfo s = new SongInfo(name, artist, url, songImage);
                _songs.add(s);

            } while (cursor.moveToNext());
        }

        cursor.close();
        songAdapter = new SongAdapter(MainActivity.this, _songs);

    }
}

有人能告诉我如何在我的自定义媒体播放器中加载内部存储中的所有歌曲吗?顺便说一句,如果我错了,请纠正我

提问于
用户回答回答于
public void getSongs(Context context, String location){
    Uri songUri = ((location == "internal")?
            MediaStore.Audio.Media.INTERNAL_CONTENT_URI:
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);

    //Query devices for files (This class provides applications access to the content model.)
    ContentResolver contentResolver = context.getContentResolver();

    //This interface provides random read-write access to the result set returned by a database query.
    Cursor myCursor;


    //This is the information i want to get from all the songs on my device.
    //Columns for audio file that show up in multiple tables.
    String SONG_ID = MediaStore.Audio.Media._ID;
    String SONG_DATA = MediaStore.Audio.Media.DATA;
    String SONG_TITLE =  MediaStore.Audio.Media.DISPLAY_NAME;
    String SONG_ARTIST = MediaStore.Audio.Media.ARTIST;

    //This is what i'll retrieve from the song table.
    String[] songColumns = {
            SONG_ID,
            SONG_DATA,
            SONG_TITLE,
            SONG_ARTIST,
    };


    //Show music files only (IS_MUSIC returns Non-zero if the audio file is music).
    final String musicOnly = MediaStore.Audio.Media.IS_MUSIC+"=1";

    myCursor = contentResolver.query(songUri, songColumns, musicOnly, null, null);

    if (myCursor != null && myCursor.moveToFirst()) {
        int songId = myCursor.getInt(myCursor.getColumnIndexOrThrow(SONG_ID));
        String songData = myCursor.getString(myCursor.getColumnIndexOrThrow(SONG_DATA));
        String songTitle = myCursor.getString(myCursor.getColumnIndexOrThrow(SONG_TITLE));
        String songArtist = myCursor.getString(myCursor.getColumnIndexOrThrow(SONG_ARTIST));

        do {

            Song song = new Song(songId, songData);

            song.setTitle(songTitle);
            song.setArtist(songArtist);


            //Add the song to the global ArrayList 'songList'.
            songList.add(song);

        } while (myCursor.moveToNext());
    }else{
        //What to do if no songs are found?
    }
    myCursor.close();

歌曲类

public class Song implements Serializable {

  private long id;
  private String data;
  private String title = "";
  private String artist = "";


public Song(long songId, String songData){
    this.id = songId;
    this.data = songData;

}

public long getId(){
    return id;
}

public String getData(){return data;}

//Optional meta data

public void setTitle(String title){
    this.title = title;
}

public String getTitle() {
    return title;
}

public void setArtist(String artist){
    this.artist = artist;
}

public String getArtist() {
    return artist;
}

扫码关注云+社区

领取腾讯云代金券