2

我正在制作一个自定义视频播放器,我正在使用 Surfaceview 和视频播放器,但是当我运行该应用程序时,它只显示黑屏并播放音频。

我的代码:

public class MainActivity extends Activity implements SurfaceHolder.Callback {

    private SeekBar seekbar;
    private ImageView ivPlay;
    private SurfaceView surfaceView;
    private SurfaceHolder surfaceHolder;
    private MediaPlayer mediaPlayer;
    private Handler mHandler;
    private Utilities utils;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initObjects();
        playVideo();
    }

    private void initObjects() {
        seekbar = (SeekBar) findViewById(R.id.sbVideo);
        seekbar.setOnSeekBarChangeListener(seekbarChangeListener);
        mHandler = new Handler();
        utils = new Utilities();
        ivPlay = (ImageView) findViewById(R.id.ivPlay);
        ivPlay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                if (mediaPlayer.isPlaying()) {
                    if (mediaPlayer != null) {
                        mediaPlayer.pause();
                        // Changing button image to play button
                        ivPlay.setImageResource(R.drawable.ic_media_play);
                    }
                } else {
                    // Resume song
                    if (mediaPlayer != null) {
                        mediaPlayer.start();
                        // Changing button image to pause button
                        ivPlay.setImageResource(R.drawable.ic_media_pause);
                    }
                }

            }
        });
        surfaceView = (SurfaceView) findViewById(R.id.svVideoSurface);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.addCallback(this);
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mediaPlayer = new MediaPlayer();
        mediaPlayer.setDisplay(surfaceHolder);
    }

    public void playVideo() {
        // Play song

        Uri uri = Uri
                .parse("android.resource://com.videoplayer.demo/raw/title_video1");

        try {

            mediaPlayer.reset();
            mediaPlayer.setDataSource(MainActivity.this, uri);

            mediaPlayer.prepare();
            mediaPlayer.start();

            // Changing Button Image to pause image
            ivPlay.setImageResource(R.drawable.ic_media_pause);

            // set Progress bar values
            seekbar.setProgress(0);
            seekbar.setMax(100);

            // Updating progress bar
            updateProgressBar();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private OnSeekBarChangeListener seekbarChangeListener = new OnSeekBarChangeListener() {

        @Override
        public void onStopTrackingTouch(SeekBar seekbar) {
            mHandler.removeCallbacks(mUpdateTimeTask);
            int totalDuration = mediaPlayer.getDuration();
            int currentPosition = utils.progressToTimer(seekbar.getProgress(),
                    totalDuration);

            // forward or backward to certain seconds
            mediaPlayer.seekTo(currentPosition);

            // update timer progress again
            updateProgressBar();

        }

        @Override
        public void onStartTrackingTouch(SeekBar seekbar) {
            // TODO Auto-generated method stub
            mHandler.removeCallbacks(mUpdateTimeTask);
        }

        @Override
        public void onProgressChanged(SeekBar seekbar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub

        }
    };

    @Override
    public void surfaceChanged(SurfaceHolder holder, int arg1, int arg2, int arg3) {
        mediaPlayer.setDisplay(holder);

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

    public void updateProgressBar() {
        mHandler.postDelayed(mUpdateTimeTask, 100);
    }

    private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            long totalDuration = mediaPlayer.getDuration();
            long currentDuration = mediaPlayer.getCurrentPosition();

            // Updating progress bar
            int progress = (int) (utils.getProgressPercentage(currentDuration,
                    totalDuration));
            // Log.d("Progress", ""+progress);
            seekbar.setProgress(progress);

            // Running this thread after 100 milliseconds
            mHandler.postDelayed(this, 100);
        }
    };

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mediaPlayer.release();
    };
}

我不知道我做错了什么,任何帮助都会很棒。

4

1 回答 1

0

试试这个,它会帮助你!

 @Override
 public void surfaceCreated(SurfaceHolder holder) {
    if(player == null) {
        player = new MediaPlayer();
    }
    player.setDisplay(holder);
 }
于 2017-10-06T05:59:48.927 回答