Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> Android 視頻播放器進度的處理,android進度

Android 視頻播放器進度的處理,android進度

編輯:關於android開發

Android 視頻播放器進度的處理,android進度


在前面的項目中添加SeekBar

 <SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

相關的代碼如下:

public class MainActivity extends Activity implements OnClickListener {

    private EditText et_path;
    private Button bt_play, bt_replay, bt_pause, bt_stop;
    private SurfaceView sv;

    private SeekBar sb;
    private MediaPlayer mediaPlayer;
    private int currentPosition;
    private Boolean isplaying;

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

        sv = (SurfaceView) findViewById(R.id.sv);
        et_path = (EditText) findViewById(R.id.et_path);
        bt_play = (Button) findViewById(R.id.bt_play);
        bt_replay = (Button) findViewById(R.id.bt_replay);
        bt_pause = (Button) findViewById(R.id.bt_pause);
        bt_stop = (Button) findViewById(R.id.bt_stop);
        sb = (SeekBar) findViewById(R.id.sb);
        
        sb.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int process = seekBar.getProgress();
                if(mediaPlayer != null && mediaPlayer.isPlaying()) {
                    mediaPlayer.seekTo(process);
                }
                
            }
            
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // TODO Auto-generated method stub
                
            }
            
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                    boolean fromUser) {
                // TODO Auto-generated method stub
                
            }
        });

        bt_pause.setOnClickListener(this);
        bt_play.setOnClickListener(this);
        bt_replay.setOnClickListener(this);
        bt_stop.setOnClickListener(this);

        sv.getHolder().addCallback(new Callback() {

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                System.out.println("holder被銷毀了");

                if (mediaPlayer != null && mediaPlayer.isPlaying()) {
                    currentPosition = mediaPlayer.getCurrentPosition();
                    stop();
                }
            }

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                System.out.println("holder被創建了");
                if (currentPosition > 0) {
                    play(currentPosition);
                }

            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format,
                    int width, int height) {
                System.out.println("holder的大小變化了");
            }
        });

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.bt_play:
            play(0);
            break;
        case R.id.bt_replay:
            replay();
            break;
        case R.id.bt_stop:
            stop();
            break;
        case R.id.bt_pause:
            pause();
            break;
        default:
            break;
        }
    }

    /**
     * 暫停音樂
     */
    private void pause() {
        if ("繼續".equals(bt_pause.getText().toString().trim())) {
            mediaPlayer.start();
            bt_pause.setText("暫停");
            return;
        }
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.pause();
            bt_pause.setText("繼續");
            return;
        }
    }

    /**
     * 重新播放
     */
    private void replay() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.seekTo(0);
            return;
        }
        play(0);
    }

    /**
     * 停止播放音樂
     */
    private void stop() {
        if (mediaPlayer != null && mediaPlayer.isPlaying()) {
            mediaPlayer.stop();
            mediaPlayer.release(); // 記得釋放資源
            mediaPlayer = null;
            bt_play.setEnabled(true);
            isplaying = false;
        }
    }

    /**
     * 播放音樂
     */
    private void play(final int currentPosition) {
        String path = et_path.getText().toString().trim();
        try {
            mediaPlayer = new MediaPlayer();
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

            /* 設置Video影片以SurfaceHolder播放 */
            mediaPlayer.setDisplay(sv.getHolder());

            mediaPlayer.setDataSource(path);
            mediaPlayer.prepare(); // might take long! (for buffering, etc)
            //mediaPlayer.prepareAsync();

            mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                    int max = mediaPlayer.getDuration();
                    sb.setMax(max);
                    mediaPlayer.seekTo(currentPosition);
                    new Thread() {
                        public void run() {
                            isplaying = true;
                            while (isplaying) {
                                int position = mediaPlayer.getCurrentPosition();
                                sb.setProgress(position);
                                try {
                                    Thread.sleep(500);
                                } catch (InterruptedException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }
                            }

                        };

                    }.start();

                }
            });
            mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
                    bt_play.setEnabled(true);
                }
            });

            bt_play.setEnabled(false);
        } catch (Exception e) {
            Toast.makeText(this, "播放失敗", 0).show();
            e.printStackTrace();
        }

    }

}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved