Media Player 음악 재생 기 학습 3 SeekBar 추가

5385 단어 학습 총화
SeekBar 추가
배치
<SeekBar
        android:id="@+id/sb"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

2.서비스 에 추가 하 는 방법:
MusicService . java
private void addSeekBar() {


        //     ,           
        Timer timer = new Timer();
        //     
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                int duration = player.getDuration();
                int currentPosition = player.getCurrentPosition();

                Message msg = MainActivity.handler.obtainMessage();
                Bundle bundle = new Bundle();
                bundle.putInt("duration", duration);
                bundle.putInt("currentPosition", currentPosition);
                msg.setData(bundle);

                MainActivity.handler.sendMessage(msg);
            }
        }, 5, 500);

    }

getDuration()방법 으로 시간 을 얻 을 수 있 으 며,getCurrentPosition()을 통 해 현재 재생 위 치 를 얻 을 수 있 습 니 다.handler 를 사용 하고 Bundle 로 전달 해 야 합 니 다.MainActivity.java 에서 정 의 된 Handler 입 니 다.마지막 으로 500 밀리초 간격 으로 진 도 를 업데이트 하고 타이머 타 이 머 를 사용 했다.
  public static Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle bundle = msg.getData();
            int duration = bundle.getInt("duration");
            int currentPosition = bundle.getInt("currentPosition");

            sb.setMax(duration);
            sb.setProgress(currentPosition);
        }
    };

SeekBar 에 미끄럼 감청 설정 하기:
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sb = (SeekBar) findViewById(R.id.sb);
        sb.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            //    
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            //        ,    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            //    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                int progress = seekBar.getProgress();
                mi.seekTo(progress);
            }
        });
}

서비스 라 는 중간 자 를 통 해 데 이 터 를 전달 하기 때문에 서비스 에 seekTo()방법 을 추가 해 야 합 니 다.
 private void seekTo(int progress) {
        player.seekTo(progress);
    }

인터페이스 에 추상 적 인 방법 추가:
public interface MusicInterface {
    void play();

    void pause();

    void stop();

    void continuePlay();

    void seekTo(int progress);
}

좋은 웹페이지 즐겨찾기