显示SD卡上的所有音乐

发布于 2024-12-28 16:15:43 字数 1468 浏览 4 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浪推晚风 2025-01-04 16:15:43

安德烈亚斯的答案是执行此操作的正确方法,但该代码无法获取绝对文件路径。
这会导致mMediaPlayer.prepare();抛出IOException:准备失败。状态=0x1

下面是获取文件路径和文件名的代码:

private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mMediaPlayer = new MediaPlayer();

    ListView mListView = (ListView) findViewById(R.id.listView1);

    mMusicList = getAudioList();

    ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, mMusicList);
    mListView.setAdapter(mAdapter);

    mListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
    try {
        playSong(mAudioPath[arg2]);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
   });
}

private String[] getAudioList() {
    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,
            "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

    int count = mCursor.getCount();

    String[] songs = new String[count];
    String[] mAudioPath = new String[count];
    int i = 0;
    if (mCursor.moveToFirst()) {
        do {
            songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            i++;
        } while (mCursor.moveToNext());
    }   

    mCursor.close();

    return songs;
}

现在我们有了绝对路径,我们不需要再次获取路径。所以:

 private void playSong(String path) throws IllegalArgumentException,
    IllegalStateException, IOException {

    Log.d("ringtone", "playSong :: " + path);

    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(path);       
//mMediaPlayer.setLooping(true);
    mMediaPlayer.prepare();
    mMediaPlayer.start();
}

使用:

playSong(mAudioPath[arg2]);

而不是:

playSong(mMusicList[arg2]);

请确保在列表视图 OnItemClickListener 中

。要仅获取曲目的标题(看起来比带有扩展名的整个文件名更优雅),请使用:

`MediaStore.Audio.Media.TITLE`

而不是:

`MediaStore.Audio.Media.DISPLAY_NAME`

Andreas answer is the right way to do this but that code does not get the absolute file path.
This causes the mMediaPlayer.prepare(); to throw IOException: Prepare failed. status=0x1.

Here is the code to get the file path along with the file name:

private String[] mAudioPath;
private MediaPlayer mMediaPlayer;
private String[] mMusicList;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mMediaPlayer = new MediaPlayer();

    ListView mListView = (ListView) findViewById(R.id.listView1);

    mMusicList = getAudioList();

    ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_list_item_1, mMusicList);
    mListView.setAdapter(mAdapter);

    mListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
    try {
        playSong(mAudioPath[arg2]);
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    }
   });
}

private String[] getAudioList() {
    final Cursor mCursor = getContentResolver().query(
            MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
            new String[] { MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.DATA }, null, null,
            "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

    int count = mCursor.getCount();

    String[] songs = new String[count];
    String[] mAudioPath = new String[count];
    int i = 0;
    if (mCursor.moveToFirst()) {
        do {
            songs[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
            mAudioPath[i] = mCursor.getString(mCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
            i++;
        } while (mCursor.moveToNext());
    }   

    mCursor.close();

    return songs;
}

Now that we have the absolute path, we don't need to get the path again. So:

 private void playSong(String path) throws IllegalArgumentException,
    IllegalStateException, IOException {

    Log.d("ringtone", "playSong :: " + path);

    mMediaPlayer.reset();
    mMediaPlayer.setDataSource(path);       
//mMediaPlayer.setLooping(true);
    mMediaPlayer.prepare();
    mMediaPlayer.start();
}

Make sure to use:

playSong(mAudioPath[arg2]);

instead of:

playSong(mMusicList[arg2]);

in the listview OnItemClickListener.

To get only the title of the track (Looks more elegant than the whole file name with extension) use:

`MediaStore.Audio.Media.TITLE`

instead of:

`MediaStore.Audio.Media.DISPLAY_NAME`
阳光①夏 2025-01-04 16:15:43

我不完全确定是什么原因导致了您提到的问题,但请尝试此代码。

private MediaPlayer mMediaPlayer;
private String[] mMusicList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mMediaPlayer = new MediaPlayer();

  ListView mListView = (ListView) findViewById(R.id.listView1);

  mMusicList = getMusic();

  ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
  android.R.layout.simple_list_item_1, mMusicList);
  mListView.setAdapter(mAdapter);

  mListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
      try {
        playSong(mMusicList[arg2]);
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalStateException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  });
}

private String[] getMusic() {
  final Cursor mCursor = managedQuery(
  MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  new String[] { MediaStore.Audio.Media.DISPLAY_NAME }, null, null,
  "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

  int count = mCursor.getCount();

  String[] songs = new String[count];
  int i = 0;
  if (mCursor.moveToFirst()) {
    do {
      songs[i] = mCursor.getString(0);
      i++;
    } while (mCursor.moveToNext());
  }

  mCursor.close();

  return songs;
}

private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
  String extStorageDirectory = Environment.getExternalStorageDirectory()
  .toString();

  path = extStorageDirectory + File.separator + path;

  mMediaPlayer.reset();
  mMediaPlayer.setDataSource(path);
  mMediaPlayer.prepare();
  mMediaPlayer.start();
}

I'm not entirely sure exactly what causes the problems you mention, but try this code.

private MediaPlayer mMediaPlayer;
private String[] mMusicList;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  mMediaPlayer = new MediaPlayer();

  ListView mListView = (ListView) findViewById(R.id.listView1);

  mMusicList = getMusic();

  ArrayAdapter<String> mAdapter = new ArrayAdapter<String>(this,
  android.R.layout.simple_list_item_1, mMusicList);
  mListView.setAdapter(mAdapter);

  mListView.setOnItemClickListener(new OnItemClickListener() {

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
    long arg3) {
      try {
        playSong(mMusicList[arg2]);
      } catch (IllegalArgumentException e) {
        e.printStackTrace();
      } catch (IllegalStateException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  });
}

private String[] getMusic() {
  final Cursor mCursor = managedQuery(
  MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
  new String[] { MediaStore.Audio.Media.DISPLAY_NAME }, null, null,
  "LOWER(" + MediaStore.Audio.Media.TITLE + ") ASC");

  int count = mCursor.getCount();

  String[] songs = new String[count];
  int i = 0;
  if (mCursor.moveToFirst()) {
    do {
      songs[i] = mCursor.getString(0);
      i++;
    } while (mCursor.moveToNext());
  }

  mCursor.close();

  return songs;
}

private void playSong(String path) throws IllegalArgumentException,
IllegalStateException, IOException {
  String extStorageDirectory = Environment.getExternalStorageDirectory()
  .toString();

  path = extStorageDirectory + File.separator + path;

  mMediaPlayer.reset();
  mMediaPlayer.setDataSource(path);
  mMediaPlayer.prepare();
  mMediaPlayer.start();
}
仙女 2025-01-04 16:15:43

那里的代码有超级错误。

getView 没有任何意义。 gc() 在那里做什么?!所有 moveToPosition() 调用是怎么回事?

这是重写的代码

        public View getView(int position, View convertView, ViewGroup parent) {
              TextView tv = null;
              String id = null;
              if (convertView == null) {
                    tv = new TextView(getApplicationContext());
              } else
                    tv = (TextView) convertView;

              music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
              id = musiccursor.getString(music_column_index);
              music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
              id += " Size(KB): " + musiccursor.getString(music_column_index);

              tv.setText(id);
              return tv;
        }

The code there is super-buggy.

The getView doesn't make any sense. What is that gc() doing there?! What's with the all moveToPosition() calls.

Here's the code rewritten

        public View getView(int position, View convertView, ViewGroup parent) {
              TextView tv = null;
              String id = null;
              if (convertView == null) {
                    tv = new TextView(getApplicationContext());
              } else
                    tv = (TextView) convertView;

              music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
              id = musiccursor.getString(music_column_index);
              music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
              id += " Size(KB): " + musiccursor.getString(music_column_index);

              tv.setText(id);
              return tv;
        }
晚雾 2025-01-04 16:15:43

试试这个代码

public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
musiccursor.moveToPosition(position);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
return tv;
}

try this code

public View getView(int position, View convertView, ViewGroup parent) {
System.gc();
String id = null;
TextView tv;
if (convertView == null) {
tv = new TextView(mContext.getApplicationContext());
} else{
tv = (TextView) convertView;
}
musiccursor.moveToPosition(position);
music_column_index = musiccursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);
id = musiccursor.getString(music_column_index);
music_column_index = musiccursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE);
id += " Size(KB):" + musiccursor.getString(music_column_index);
tv.setText(id);
return tv;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文