在媒体播放器中更改视频时出现故障

时间:2019-04-26 05:10:18

标签: android android-mediaplayer android-textureview

我的要求是更改视频播放器中带有动画的视频。当一个人点击“更改”按钮时,媒体播放器中的视频将进行新的更改,而旧视频将以动画放大结束。我可以在纹理视图的帮助下实现此功能,但遇到一个问题,即更改视频时会出现故障。有什么办法可以使它更流畅?

这是我的代码

    public class VideoVieww extends Activity implements TextureView.SurfaceTextureListener  {

    private TextureView textureView;
    private MediaPlayer mMediaPlayer;
    private Button mButton;
    private Animation zoomAnimation;
    private SurfaceTexture mSurfaceTexture;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.texture_layout);

        textureView = findViewById(R.id.textureView);
        textureView.setSurfaceTextureListener(this);
        zoomAnimation = AnimationUtils.loadAnimation(VideoVieww.this, R.anim.zomm_out);

        mButton =  findViewById(R.id.change);
        mButton.setOnClickListener(view -> {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
                mMediaPlayer = null;
            }
            textureView.startAnimation(zoomAnimation);
                try {
                    mMediaPlayer = new MediaPlayer();
                    mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.broadchurch));
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    Surface surface = new Surface(mSurfaceTexture);
                    mMediaPlayer.setSurface(surface);
                    mMediaPlayer.prepare();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (SecurityException e) {
                    e.printStackTrace();
                } catch (IllegalStateException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        });
        zoomAnimation.setAnimationListener(new Animation.AnimationListener(){
            @Override
            public void onAnimationStart(Animation arg0) {
            }
            @Override
            public void onAnimationRepeat(Animation arg0) {
            }
            @Override
            public void onAnimationEnd(Animation arg0) {
                mMediaPlayer.start();
            }


        });
    }



    @Override
    public void onBackPressed() {
        super.onBackPressed();
        mMediaPlayer.release();
        mMediaPlayer=null;
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) {
        mSurfaceTexture=surfaceTexture;
        Surface surface = new Surface(surfaceTexture);
        try {
            mMediaPlayer= new MediaPlayer();
            mMediaPlayer.setDataSource(VideoVieww.this, Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.sid));
            mMediaPlayer.setSurface(surface);
            mMediaPlayer.prepare();
            mMediaPlayer.start();
//            mMediaPlayer.setLooping(true);
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {

    }
}

任何帮助将不胜感激!!!

0 个答案:

没有答案