如何解决QCMediaPlayer报mediaplayer NOT present错误?ListView播放MP3失败求助
Hey there, let's break down your problem with playing MP3 files from a ListView and that annoying QCMediaPlayer error. I’ve run into this exact issue before, so here’s what’s going wrong and how to fix it step by step:
That error typically pops up when the system can’t initialize or access the underlying media player service. Most often, it’s due to resource leaks from unmanaged MediaPlayer instances or incorrect setup of the player itself. If you’re creating new MediaPlayer objects without releasing old ones, you’ll eventually exhaust system resources, leading to this error.
Your mention of passing paths via Uri is key—this is where many developers trip up:
- Local files (Android 7.0+): Forget
Uri.fromFile()—it’ll trigger aFileUriExposedException. Instead, useFileProviderto generate a content Uri. You’ll need to configure a provider in your manifest and useFileProvider.getUriForFile()to get the valid Uri. - Raw/assets files: If your MP3 is in the
res/rawfolder, use this Uri format:Uri.parse("android.resource://" + getPackageName() + "/raw/your_file_name")(skip the.mp3suffix here). For assets, you’ll need to copy the file to internal storage first, then use its Uri. - External storage files: Ensure you have the right permissions (Android 12-:
READ_EXTERNAL_STORAGE; Android 13+:READ_MEDIA_AUDIO) and fetch the Uri viaContentResolverinstead of hardcoding the file path.
Mismanaging the MediaPlayer’s lifecycle is the #1 cause of this error. Stick to this flow:
// Initialize in your ListView item click listener (or use a single global instance) MediaPlayer mediaPlayer = new MediaPlayer(); try { mediaPlayer.setDataSource(getApplicationContext(), yourValidUri); // Use prepareAsync() to avoid blocking the UI thread mediaPlayer.prepareAsync(); mediaPlayer.setOnPreparedListener(mp -> mp.start()); mediaPlayer.setOnErrorListener((mp, what, extra) -> { // Log detailed error codes to debug further Log.e("MediaPlayerIssue", "Error: " + what + ", Extra: " + extra); mp.release(); return true; }); } catch (IOException e) { e.printStackTrace(); mediaPlayer.release(); } // Critical: Release resources when you're done (e.g., in onDestroy() or when switching items) @Override protected void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { if (mediaPlayer.isPlaying()) mediaPlayer.stop(); mediaPlayer.release(); mediaPlayer = null; } }
Pro tip: Reuse a single MediaPlayer instance instead of creating a new one for each ListView item. This prevents resource leaks that cause the QCMediaPlayer error.
Double-check that your app can actually access the MP3 file:
- For external storage, make sure you’ve requested the runtime permission (don’t just declare it in the manifest).
- For internal app storage (like
getFilesDir()), no extra permissions are needed—but confirm the file path is correct (useLog.d()to print the path and verify it exists).
To rule out ListView-related bugs, create a simple test: add a button to your activity that plays the target MP3 directly. If it works, the issue is in your ListView click logic (e.g., passing the wrong Uri or not handling the player correctly in the adapter). If it still fails, the problem is with the file itself or permission setup.
内容的提问来源于stack exchange,提问作者Ravi Patil

