MediaPlayer setLooping(true)不起作用

时间:2016-03-21 15:03:04

标签: android android-mediaplayer textureview

我使用MediaPlayer显示视频。我需要循环它。我使用本机方法setLooping(),但它不起作用。视频结束了,就是这样。这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_full_screen);

        TextureView videoViewFullScreen = (TextureView) findViewById(R.id.video_screen);

        path = getIntent().getExtras().getString(SettingsActivity.FILE_PATH);
        cycle = getIntent().getExtras().getBoolean(SettingsActivity.CYCLE);

        position = getIntent().getIntExtra(FullScreenActivity.POSITION, -1);

        videoViewFullScreen.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {

                final Surface surf = new Surface(surface);
                try {
                    mediaPlayerFullScreen = new MediaPlayer();
                    mediaPlayerFullScreen.setDataSource(path);
                    mediaPlayerFullScreen.setSurface(surf);
                    if (cycle) {
                        mediaPlayerFullScreen.setLooping(true);
                    }

                    mediaPlayerFullScreen.prepareAsync();

                    mediaPlayerFullScreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                        @Override
                        public void onPrepared(MediaPlayer mediaPlayer) {
                            if (position != -1) {
                                Log.d("Position", "Position Full " + position);
                                mediaPlayerFullScreen.seekTo(position);
                            }

                            mediaPlayer.start();
                        }
                    });
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

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

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                mediaPlayerFullScreen.stop();
                mediaPlayerFullScreen.release();
                mediaPlayerFullScreen = null;
                return true;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {
            }
        });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mediaPlayerFullScreen != null) {
            mediaPlayerFullScreen.stop();
            mediaPlayerFullScreen.release();
            mediaPlayerFullScreen = null;
        }
    }

我看了所有类似的问题并尝试了所有在这里找到的答案,但没有一个能帮助我。 我有Android版本4.4.2,固件版本4.5,内核版本3.3.0 有人能帮助我吗?还有另一种循环视频的方法吗?

1 个答案:

答案 0 :(得分:0)

我找到了setLooping(true)的替代品,这对我有用:

                    mediaPlayerFullScreen.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                    @Override
                    public void onCompletion(MediaPlayer mp) {
                        mediaPlayerFullScreen.reset();
                        try {
                            mediaPlayerFullScreen.setDataSource(path);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        mediaPlayerFullScreen.setSurface(surf);
                        mediaPlayerFullScreen.prepareAsync();

                        mediaPlayerFullScreen.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                            @Override
                            public void onPrepared(MediaPlayer mediaPlayer) {
                                mediaPlayer.start();
                            }
                        });
                    }
                });