最近初学安卓,在Android Studio里面做播放本地视频功能时遇到了,路径正确却弹出了“无法播放此视频”的情况(网络视频可以正常播放)。
解决办法:
由于安卓10更新了一些东西,需要在AndroidManifest.xml的application里面加入android:requestLegacyExternalStorage=“true”,这样就可以正常播放了。
附部分代码:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:requestLegacyExternalStorage="true">
</application>
private void initVideoPath() {
Uri uri = Uri.parse(Environment.getExternalStorageDirectory()+"/movie.mp4");
videoView.setMediaController(new MediaController(this));
videoView.setVideoURI(uri);
videoView.start();
//重复播放视频
videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mPlayer) {
mPlayer.start();
mPlayer.setLooping(true);
}
});
/*显示路径*/
//Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show();
}