안드로이드 개발 질문/답변
(글 수 45,052)
현재 음악에 맞춰서 seekbar가 움직이는것 까지 구현을 하였습니다(thread를 이용)(밑에 소스 참조)
여기서 사용자가 seekbar 을 원하는 위치로 이동 했을때 거기서 부터 다시 노래 재생과 seekbar가
움직이게 하려고 하는데 seekbar를 움직이면 다시 원래 있던 자리로 이동하네요....
도움 부탁드립니다.
여기서 사용자가 seekbar 을 원하는 위치로 이동 했을때 거기서 부터 다시 노래 재생과 seekbar가
움직이게 하려고 하는데 seekbar를 움직이면 다시 원래 있던 자리로 이동하네요....
도움 부탁드립니다.
package dku.android.dankook;
import android.app.Activity; import android.media.AudioManager; import android.media.MediaPlayer; import android.media.MediaPlayer.OnCompletionListener; import android.os.Bundle; import android.os.Handler; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ProgressBar; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast;
public class thread extends Activity implements OnClickListener, OnCompletionListener, Runnable{ private static final String TAG = "TEST_DEBUG"; private Button b; private TextView tv; private SeekBar a ; private MediaPlayer mp; private AudioManager am; private int dur ; private Thread mp3t; private int move_song; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.main1); mp = new MediaPlayer(); mp.setOnCompletionListener(this); tv = (TextView)findViewById(R.id.tim); b = (Button)findViewById(R.id.but2); b.setOnClickListener(this); a = new SeekBar(this); mp = MediaPlayer.create(this, R.raw.rihanna); dur = mp.getDuration(); String tima = changeToMinutes(dur); tv.setText(tima); mp3t = new Thread(this); a = (SeekBar)findViewById(R.id.progress); a.setIndeterminate(false); am = (AudioManager)getSystemService(AUDIO_SERVICE); a.setOnSeekBarChangeListener(tmdqo); } private SeekBar.OnSeekBarChangeListener tmdqo = new SeekBar.OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub seekBar.setProgress(move_song); } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub seekBar.setProgress(move_song); } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { // TODO Auto-generated method stub } }; @Override public void onClick(View v) { if(v == b) { Toast.makeText(this, "PLAY", 500).show(); try { mp.start(); mp3t.start(); } catch (Exception e) { // TODO: handle exception } Log.d(TAG,"MP3 FILE STARTED."); }
} @Override public void onCompletion(MediaPlayer mp) { try { mp.stop(); mp.prepare(); mp.seekTo(0); mp3t.stop(); Toast.makeText(this, "STOP", 500).show(); } catch (Exception e) { // TODO: handle exception }
} private String changeToMinutes(int mseconds) { int min = 0; int sec = 0; String minStr = ""; String secStr = ""; min = (int) Math.floor(mseconds/(1000*60)); sec = (int) Math.floor((mseconds-(1000*60)*min)/1000); minStr = min < 10 ? "0"+min:""+min; secStr = sec < 10 ? "0"+sec:""+sec; return minStr+":"+secStr; } private int makePercent(int child,int parent) { int per = (int) Math.floor((child*100)/parent); return per; } private final Handler h = new Handler(); private boolean done = false; private final Runnable mp3Run = new Runnable() { public void run() { int currentDuration = mp.getCurrentPosition(); //time.setText(makePercent(currentDuration, myDuration)); //time.setText(currentDuration+"_"+myDuration+"_"+makePercent(currentDuration,myDuration)); a.setProgress(makePercent(currentDuration, dur)); //Toast.makeText(PlayingMp3.this, makePercent(currentDuration, myDuration)+"", 1000); } }; @Override public void run() { while(!done) { try { Thread.sleep(200); } catch (Exception e) { // TODO: handle exception } h.post(mp3Run); }
} }
2010.03.25 00:44:04
Seekbar를 조정할 때...음악을 pause 하시고... Seekbar의 움직임이 끝났을 때,
Seekbar의 현재 위치를 MediaPlayer 객체의 SeekTo로 위치를 다시 잡고
Start 해 주시면 될거에요... :)
2010.03.25 01:04:03
제가 만든 소스에 쓴 방식입니다.
@Override public void onStartTrackingTouch(SeekBar arg0) { clickBar = true; } @Override public void onStopTrackingTouch(SeekBar arg0) { clickBar = false; mMediaPlayer.seekTo(timeBar.getProgress() * 1000); }
저는 clickBar 라는 boolean값을 만들어서
바를 누르고 있는 상태에서는(clickBar가 true값일때) 쓰레드에서 setProgress(현재위치) 이부분을 잠시 못하게 해놨습니다.
그런식으로 안하면 바가 잡고있는위치와 현재 재생위치를 계속 왔다갔다 할껍니다^^;;;
바를 움직였을때 재생위치를 변경하는 명령어가 아예 없네요 -_-;;
바를 조정해도 재생위치가 변하질 않으니 바를 놓으면 당연히 다시 돌아가겠죠ㅎㅎ