首页 > 解决方案 > 无法在 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());
        }
    }

标签: androidexoplayer

解决方案


推荐阅读