Media Player 음악 재생 기 학습 3 SeekBar 추가
5385 단어 학습 총화
배치
<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);
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
20 - Ansible 상용 모듈 - yum 모듈즉, 인증 패 키 지 를 사용 하지 않 고 직접 설치 합 니 다.대응 하 는 yum 소스 가 gpg 인증 을 열지 않 은 경우 이 매개 변수의 값 을 yes 로 설정 해 야 합 니 다. enablerepo 인자: 패...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.