[英]android force close on clicking on seekbar
i'm newbie to android programming. 我是android编程的新手。 i have the following code but i have a problem.
我有以下代码,但我有问题。 when i click on the seekbar without playing the sound it force close.
当我在不播放声音的情况下单击搜寻栏时,它会强制关闭。 i mean when i click the play button i can click and seek on the seekbar but without clicking the play button clicking on the seekbar will cause force close.
我的意思是,当我单击播放按钮时,我可以在搜索栏上单击并搜索,但是如果不单击播放按钮,则在搜索栏上单击会导致强制关闭。 what's the problem?!
有什么问题?!
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class MainActivity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{
private SeekBar seekBar;
private ImageButton startMedia;
private ImageButton pauseMedia;
private MediaPlayer mp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AudioControl();
}
public void AudioControl(){
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (ImageButton) findViewById(R.id.playbutton);
pauseMedia = (ImageButton) findViewById(R.id.pausebutton);
seekBar.setOnSeekBarChangeListener(this);
startMedia.setOnClickListener(this);
pauseMedia.setOnClickListener(this);
}
public void run() {
int currentPosition= 0;
int total = mp.getDuration();
while (mp!=null && currentPosition<total) {
try {
Thread.sleep(1000);
currentPosition= mp.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBar.setProgress(currentPosition);
}
}
public void onClick(View v) {
if (v.equals(startMedia)) {
if (mp != null && mp.isPlaying()) return;
if(seekBar.getProgress() > 0) {
mp.start();
return;
}
mp = MediaPlayer.create(MainActivity.this, R.raw.lone);
mp.start();
seekBar.setProgress(0);
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
if (v.equals(pauseMedia) && mp!=null) {
mp.pause();
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(fromUser) mp.seekTo(progress);
}
}
and i have another question: i wanna when the user clicks on play button the pause button become visible and when the user clicks on pause button it tuen invisble. 我还有一个问题:我想当用户单击播放按钮时,暂停按钮可见,而当用户单击暂停按钮时,它看不到。 what code should i wirte and please tell me where should i put this code.
我应该写什么代码,请告诉我该代码放在哪里。
Every method that manages the UI has to be called on the UI Thread itself: 必须在UI线程本身上调用管理UI的每个方法:
You can not run 你不能跑
seekBar.setProgress(currentPosition);
on a Thread different frome the UIThread. 在不同于UIThread的线程上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.