MediaPlayer:错误(1,-38)在Android中有错误

时间:2018-12-20 20:40:32

标签: android webview

我尝试使用默认的mediaPlayer通过URL播放视频。但是我有以下错误:

  

MediaPlayer:错误(1,-38)

我能理解为什么会有这个。我认为这是在我的代码问题或某些逻辑中。您可以在下面看到“我的代码”:

private VideoView videoView;
    private int position = 0;
    private MediaController mediaController;
    private static final String TEST_URL = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_player);
        videoView = (VideoView) findViewById(R.id.videoView);

        // Set the media controller buttons
        if (mediaController == null) {
            mediaController = new MediaController(this);

            // Set the videoView that acts as the anchor for the MediaController.
            mediaController.setAnchorView(videoView);


            // Set MediaController for VideoView
            videoView.setMediaController(mediaController);
        }


        try {
            // ID of video file.
            videoView.setVideoURI(Uri.parse(TEST_URL));
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
        }

        videoView.requestFocus();


        // When the video file ready for playback.
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {

            public void onPrepared(MediaPlayer mediaPlayer) {


                videoView.seekTo(position);
                if (position == 0) {
                    videoView.start();
                }

                // When video Screen change size.
                mediaPlayer.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                    @Override
                    public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {

                        // Re-Set the videoView that acts as the anchor for the MediaController
                        mediaController.setAnchorView(videoView);
                    }
                });
            }
        });

    }

    // Find ID corresponding to the name of the resource (in the directory raw).
    public int getRawResIdByName(String resName) {
        String pkgName = this.getPackageName();
        // Return 0 if not found.
        int resID = this.getResources().getIdentifier(resName, "raw", pkgName);
        Log.i("AndroidVideoView", "Res Name: " + resName + "==> Res ID = " + resID);
        return resID;
    }


    // When you change direction of phone, this method will be called.
    // It store the state of video (Current position)
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);

        // Store current position.
        savedInstanceState.putInt("CurrentPosition", videoView.getCurrentPosition());
        videoView.pause();
    }


    // After rotating the phone. This method is called.
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // Get saved position.
        position = savedInstanceState.getInt("CurrentPosition");
        videoView.seekTo(position);
    }

MediaPlayer:错误(1,-38)可能是某些编解码器出现此问题,但我的设备不想播放此视频

1 个答案:

答案 0 :(得分:0)

尝试此代码,首先在所有位置删除MediaController类。

在使用MediaController小部件导入MediaController类之后,添加以下行。

videoView.setMediaController(new MediaController(this));

在清单中的活动下方添加一行。[旋转设备视频以继续播放而不重启视频]

android:configChanges="keyboardHidden|orientation|screenSize"