android - 无法在 android 11 上的 exoplayer 上播放视频
问题描述
以下代码似乎在 android 10 及更低版本上运行良好 - 但在 android 11 上视频无法播放,我收到以下错误
onPlayerError - 源错误
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG,"onCreate");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_play);
playerView = findViewById(R.id.playerView);
exoPlayer = new SimpleExoPlayer.Builder(this).build();
playerView.setPlayer(exoPlayer);
if (getIntent() != null)
{
String videoPath = getIntent().getExtras().getString(VIDEO_PATH);
Log.d(TAG,"intent not null");
Log.d(TAG,"videoPath = "+videoPath);
Log.d(TAG,"video URI from file= "+Uri.fromFile(new File(videoPath)));
Uri videoURI = FileProvider.getUriForFile(
this, VideoPlayerActivity.this.getPackageName() + ".provider",
new File(videoPath));
Log.d(TAG,"video URI from FileProvider = "+videoURI);
MediaItem mediaItem = MediaItem.fromUri(
Uri.fromFile(new File(videoPath))
);
exoPlayer.setMediaItem(mediaItem);
exoPlayer.prepare();
exoPlayer.play();
exoPlayer.addListener(new Player.Listener() {
@Override
public void onPlayerError(ExoPlaybackException error) {
Log.e(TAG,"onPlayerError - "+error.getMessage());
}
@Override
public void onMetadata(Metadata metadata) {
Log.d(TAG,"onMetadata - "+metadata.toString());
}
});
}
else
{
Log.e(TAG,"something is null");
}
}
我尝试使用这两种方法获取视频的 URI,但都不起作用。有什么办法让它工作吗?
在以前的活动中保存视频的代码 -
String fileName = SR_PreferencesHelper.getString(SR_PreferencesHelper.KEY_FILE_NAME_PREFIX, "recording") + "_" + new SimpleDateFormat(SR_PreferencesHelper.getString(SR_PreferencesHelper.KEY_FILE_NAME_FOMART, Config.itemsFileNameFomart[0].getValue())).format(new Date()) + ".mp4";
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Video.Media.RELATIVE_PATH, Environment.DIRECTORY_MOVIES + File.separator + context.getString(R.string.app_name));
contentValues.put(MediaStore.Video.Media.TITLE, fileName);
contentValues.put(MediaStore.MediaColumns.DISPLAY_NAME, fileName);
contentValues.put(MediaStore.MediaColumns.MIME_TYPE, "video/mp4");
contentValues.put(MediaStore.Video.Media.IS_PENDING, 1);
Uri mUri = context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, contentValues);
此外,此功能工作正常 -
public void openMediaUsingOtherApps(String filePath)
{
try
{
Uri fileUri = FileProvider.getUriForFile(
getContext(), getContext().getPackageName() + ".provider",
new File(filePath));
Intent openVideoIntent = new Intent();
openVideoIntent.setAction(Intent.ACTION_VIEW)
.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_NEW_TASK)
.setDataAndType(
fileUri,
getContext().getContentResolver().getType(fileUri));
getContext().startActivity(openVideoIntent);
}
catch (Exception e)
{
Log.e(TAG,"openMediaUsingOtherApps, msg - "+e.getMessage());
}
}
解决方案
推荐阅读
- typescript - 如何在 Backlogs 窗口中获取选定的 Backlog 项目?
- java - 如何更改 TestNG 报告中的异常消息
- java - 如何检查一个数字的每个数字是否大于或等于另一个数字?
- javascript - 如何禁用默认视频控件并显示自定义搜索栏
- c# - 仅删除列表中的第一个字符
- blockchain - 如何确保每个人都有正确有效的块状
- c# - 使用 Math.Round 函数在 C# 中的 Excel 工作表中将输入的数字转换为只有 2 个小数
- mysql - MySQL 显示第 n 个日期
- php - 如何使用 postman 为 web 服务设置 .php 文件的 url 和参数?
- bootstrap-4 - 为什么主容器在 nav 后面?