【问题标题】:How to use getExternalStorageDirectory() to load multiple songs from internal storage如何使用 getExternalStorageDirectory() 从内部存储加载多首歌曲
【发布时间】:2018-06-05 11:06:08
【问题描述】:

我正在开发自定义媒体播放器应用程序,该应用程序必须从 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);

    }
}

在这里你可以看到我正在使用MEDIA_PATH,这是一个字符串类型变量,其中Environment.getExternalStorageDirectory() 用于加载多首歌曲,但我得到的只是IllegalArgumentException 错误(是的,我可以清楚地看到我在得到IllegalArgumentException,这意味着路径有问题,我试图修复它,但我无法修复)。

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

【问题讨论】:

  • @AndroidTeam 我试过你给的这个方法。当我运行他的应用程序时,它不显示 mp3 文件,而只显示 .ogg 文件(所有文件都是 .ogg,没有文件是 mp3)。你有什么解决方案可以让我的音乐播放器加载 mp3 而不是 .ogg
  • @AndroidTeam 我的手机有 64GB 内部存储空间,没有 SD 卡插槽。当我运行你给我的代码时,它会使我的应用程序崩溃。但是当我将Uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; 更改为Uri = MediaStore.Audio.Media.INTERNAL_CONTENT_URI; 时,它会加载所有.ogg 文件,但不会加载mp3 文件。

标签: java android android-mediaplayer


【解决方案1】:
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;
}

【讨论】:

  • 我的代码和这个类似。我有一部内部空间为 64GB 的手机,每当我使用此语句 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI) 启动应用程序时,它都会崩溃,但是当我使用此语句 @Vince @AndroidTeam MediaStore.Audio.Media.INTERNAL_CONTENT_URI) 启动它时,它不会加载一首歌曲(MP3 文件),而只会加载一些.ogg 文件。有没有办法解决这个问题?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-09
  • 1970-01-01
  • 1970-01-01
  • 2021-04-25
  • 2018-03-14
相关资源
最近更新 更多