public class in extends Activity {
private static MediaPlayer audio_play;
private Button startButton;
private Button stopButton;
private SeekBar mSeekBar;
private TextView textvi;
private int a;
boolean abb;
private ProgressBar progressbar;
athread thread = new athread();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startButton = (Button) findViewById(R.id.playButton);
stopButton = (Button) findViewById(R.id.stopButton);
mSeekBar = (SeekBar) findViewById(R.id.seekbar);
textvi = (TextView) findViewById(R.id.text);
progressbar = (ProgressBar) findViewById(R.id.ProgressBar01);
progressbar.setVisibility(8); //GONE
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
progressbar.setVisibility(0); //VISIBLE
play();
abb = true;
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
progressbar.setVisibility(8); //GONE
abb = false;
stop();
}
});
}
@Override
protected void onPause() {
super.onPause();
progressbar.setVisibility(8); //GONE
stop();
}
public void play( ) {
stop();
audio_play = new MediaPlayer();
try{
audio_play.setDataSource("/sdcard/Kalimba.mp3"); // mp3파일 경로
audio_play.prepare(); // 준비
audio_play.setLooping(false); // 반복재생 false
audio_play.start(); // 시작
mSeekBar.setMax(audio_play.getDuration());
thread.setDaemon(true);
thread.start();
mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
a = audio_play.getCurrentPosition()/1000;
int m,s;
String min ="", sec="";
String timer = null;
m = a % 3600;
s = m % 60;
m = m / 60;
sec = s < 10 ? "0"+s : ""+s;
min = m < 10 ? "0"+m : ""+m;
timer = String.format("%s:%s", min,sec);
textvi.setText(new StringBuilder().append(timer));
// TODO Auto-generated method stub
}
});
}catch(IOException e){
Toast.makeText(in.this, "Error!!",
Toast.LENGTH_SHORT).show();
}
}
public void stop() {
if (audio_play != null) {
audio_play.stop(); // 중지
audio_play.release(); // 자원 반환
audio_play = null;
}
}
class athread extends Thread{
public void run(){
while(abb){
try{
Thread.sleep(200);
mSeekBar.setProgress(audio_play.getCurrentPosition());<<<<<<<<<<<<<<<<<<<<<<<이부분...
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
}
}
mp3 구동할려구 만들구 있는데요 플레이 까지는 되는데 스탑 버튼을 누르면은 화살표 부분이 문제라고 뜨네요..
java.lang.NullPointerException 이거랑 같이요...
제가 생각할때는 sleep(200)이거때문에 끌때 이게 안꺼져서 문제가 생기는거 같은데 알려주세요`~




stop가 호출시 audio_play는 이미 null처리가 되었기 때문에 스레드내부에서 사용할때 null exception에러가 나게 됩니다.
stop시 스레드도 종료 해주세요~ 아니면
public void run(){
while(abb){
try{
Thread.sleep(200);
if(mSeekBar != null)
mSeekBar.setProgress(audio_play.getCurrentPosition());<<<<<<<<<<<<<<<<<<<<<<<이부분...
else
mSeekBar.setProgress(0);
}
catch(InterruptedException e){
e.printStackTrace();
}
}
}
이정도로 예외처리 해주시던가