Android VideoView播放MKV视频无声音的问题解决求助
Hey there! Let's figure out why your VideoView-based Android player is playing MKV video but no sound—this is a super common issue, and I’ve got a few solid fixes for you.
VideoView relies on Android’s native MediaPlayer under the hood, and MKV files often use audio codecs that the default MediaPlayer doesn’t support natively. Think codecs like DTS, FLAC, or even some less common AAC variants. The video part (usually H.264) works because that’s widely supported, but the audio codec flies under the native player’s radar—hence the silent playback.
Solution 1: First, Confirm the MKV’s Audio Codec
Before diving into code changes, let’s diagnose the root cause. Grab a desktop tool like MediaInfo to inspect your MKV file. Check the "Audio" section—if it lists DTS or FLAC, that’s almost certainly why the sound isn’t playing.
Solution 2: Switch to ExoPlayer (Recommended Fix)
The most reliable long-term solution is to replace VideoView with ExoPlayer, Google’s advanced media player library. It supports way more codecs than the native MediaPlayer, including almost all MKV audio formats. Here’s a quick implementation example:
First, add the ExoPlayer dependencies to your module-level build.gradle file:
implementation "com.google.android.exoplayer:exoplayer-core:2.19.1" // Use the latest version implementation "com.google.android.exoplayer:exoplayer-ui:2.19.1"
Then set up the player in your Activity or Fragment:
import com.google.android.exoplayer2.SimpleExoPlayer import com.google.android.exoplayer2.MediaItem import com.google.android.exoplayer2.ui.PlayerView import android.net.Uri import androidx.appcompat.app.AppCompatActivity import android.os.Bundle class VideoPlayerActivity : AppCompatActivity() { private lateinit var exoPlayer: SimpleExoPlayer override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_video_player) // Initialize ExoPlayer exoPlayer = SimpleExoPlayer.Builder(this).build() val playerView = findViewById<PlayerView>(R.id.player_view) playerView.player = exoPlayer // Load your MKV file val videoUri = Uri.parse("file:///sdcard/your_video.mkv") // Replace with your actual file path val mediaItem = MediaItem.fromUri(videoUri) exoPlayer.setMediaItem(mediaItem) exoPlayer.prepare() exoPlayer.play() } // Don't forget to release the player to free up resources override fun onDestroy() { super.onDestroy() exoPlayer.release() } }
This should get your MKV files playing with both video and audio seamlessly.
Solution 3: Transcode the MKV File (Quick Fix for One-Off Cases)
If you only need to support a small number of MKV files, you can transcode them to use a universally supported audio codec like AAC. Tools like HandBrake make this easy: just select AAC as the audio codec during encoding, and the resulting file will play with sound in VideoView.
Solution 4: Check Device/Android Version Support
Keep in mind that codec support can vary across Android versions and devices. Newer devices (Android 10+) might support more codecs than older ones (e.g., some can handle FLAC in MKV). Testing your app on multiple devices can help you confirm if the issue is universal or device-specific.
内容的提问来源于stack exchange,提问作者Imran Khan




