mediaplayer错误:尝试对空对象引用调用虚拟方法“void android.media.mediaplayer.start()”

sgtfey8w  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(458)

我正在尝试制作一个应用程序来播放手机内部存储的音乐文件。
这是第一种在列表视图中列出所有歌曲的方法。

public void fileSearch() {
    String[] loadSongs = {MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME};
    Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, loadSongs, null, null, null);
    if (audioCursor != null) {
        if (audioCursor.moveToFirst()) {
            do {
                int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME);

                audioList.add(audioCursor.getString(audioIndex));
            } while (audioCursor.moveToNext());
        }
    }
    audioCursor.close();
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, audioList);
    listSongs.setAdapter(adapter);
    listSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectedSong(position);
        }
    });

}

此方法应播放用户在上一列表中选择的歌曲。

public void selectedSong(int position) {
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, android.R.id.text1, audioList);
    Toast.makeText(MainActivity.this, "You  have selected the song:" + adapter.getItem(position), Toast.LENGTH_SHORT).show();
    File songSelectedPath = getFileStreamPath(adapter.getItem(position));

    Uri uri = Uri.fromFile(songSelectedPath);
    Log.d("Adapter", "AdapterToStringPath: " + uri);
    Log.d("Pointer", "Pointer: " + position);

    MediaPlayer mediaPlayer = new MediaPlayer();
    mediaPlayer = MediaPlayer.create(this,uri);
    mediaPlayer.start();
}

错误来自这行mediaplayer=mediaplayer.create(this,uri);

Attempt to invoke virtual method 'void android.media.MediaPlayer.start()' on a null object reference
8qgya5xd

8qgya5xd1#

问题是您正在向mediaplayer添加无效路径。要获得正确的路径,您可以使用光标查找所选歌曲的名称并获取其路径。简而言之,我必须修改您方法的签名,以便直接发送文件名,而无需适配器再次传递此方法:

public void selectedSong(String songName) {
    String[] loadSongs = {MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.DISPLAY_NAME};
    String selection1 = MediaStore.Audio.Media.DISPLAY_NAME + " = ?";
    String[] selectionArgs = new String[]{songName};
    Cursor audioCursor = getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, loadSongs, selection1, selectionArgs, null);

    if (audioCursor != null) {
        if (audioCursor.moveToFirst()) {
            int audioIndex = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
            String path = audioCursor.getString(audioIndex);
            Log.e("TAG", "selectedSong: " + path);
            Uri uri = Uri.fromFile(new File(path));
            Log.d("Adapter", "AdapterToStringPath: " + uri);
            MediaPlayer mediaPlayer = new MediaPlayer();
            Log.e("TAG", "selectedSong: " + uri);
            mediaPlayer = MediaPlayer.create(this, uri);
            mediaPlayer.start();
        }
    }

在你的听众中,你可以这样称呼它:

listSongs.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                selectedSong(audioList.get(position));
            }
        });

我上面安装的方法可能无法正常工作让我们假设我们有一个相同的文件 Name 在不同的 Folders 在这种情况下,很有可能你在文件上弄错了,因为我们会得到 file path 从它的 name .
所以我建议你创建一个 Object 其中包含 Filename 以及 Path 并将其添加到 list of songs ,以显示需要 Dispalyed_Name 当你点击一首歌,你就会得到 Path 我觉得这样做比较容易。如果你不明白我的建议,请不要犹豫发表评论

相关问题