您好我是Android的新手,我正在通过示例学习。我正在尝试创建一个活动,其中包含我的原始文件夹中所有歌曲的列表视图,底部是媒体播放器控件。到目前为止,我已经完成了所有工作,但我似乎无法让SeekBar
停止强行关闭。
以下是代码:
public class music extends ListActivity implements Runnable {
private ArrayList<sound> mSounds = null;
private soundadapter mAdapter = null;
private ImageButton playbtn;
private SeekBar seekbar;
private int total;
private MediaPlayer mp = null;
private TextView selelctedFile = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music);
selelctedFile = (TextView) findViewById(R.id.selectedfile);
seekbar = (SeekBar) findViewById(R.id.seekbar);
seekbar.setProgress(0);
// create a simple list
mSounds = new ArrayList<sound>();
sound s = new sound();
s.setDescription("Rudolph The Red Nosed Reindeer");
s.setSoundResourceId(R.raw.rudolphtherednosereindeer);
mSounds.add(s);
s = new sound();
s.setDescription("Battery");
s.setSoundResourceId(R.raw.battery);
mSounds.add(s);
mAdapter = new soundadapter(this, R.layout.listitem, mSounds);
setListAdapter(mAdapter);
playbtn = (ImageButton) findViewById(R.id.play);
playbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
try {
if (mp.isPlaying()) {
mp.pause();
playbtn.setImageResource(android.R.drawable.ic_media_play);
} else {
mp.start();
playbtn.setImageResource(android.R.drawable.ic_media_pause);
}
} catch (Exception e) {
e.printStackTrace();
// TODO: handle exception
}
}
});
}
@Override
public void onListItemClick(ListView parent, View v, int position, long id) {
sound s = (sound) mSounds.get(position);
if (mp != null) {
mp.reset();
mp.release();
}
mp = MediaPlayer.create(this, s.getSoundResourceId());
selelctedFile.setText(s.getDescription());
playbtn.setImageResource(android.R.drawable.ic_media_pause);
mp.start();
total = mp.getDuration();
seekbar.setMax(total);
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
}
@Override
public void onStartTrackingTouch(SeekBar seekbar) {
// TODO Auto-generated method stub
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
if (fromUser) {
mp.seekTo(progress);
seekBar.setProgress(progress);
}
}
});
Thread currentThread = new Thread(this);
currentThread.start();
}
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (mp != null) {
int currentPosition = mp.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
threadHandler.sendMessage(msg);
Thread.sleep(100);
}
}
catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
private Handler threadHandler = new Handler() {
public void handleMessage(Message msg) {
// super.handleMessage(msg);
// txt.setText(Integer.toString(msg.what));
seekbar.setProgress(msg.what);
}
};
@Override
protected void onPause() {
// TODO Auto-generated method stub
mp.stop();
mp.release();
mp = null;
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(mp != null) {
mp.stop();
mp.release();
mp = null;
}
}
}
这是我在不同歌曲上点击几次时不断出现的错误:
04-14 02:53:00.452: W/dalvikvm(27452): threadid=19: thread exiting with uncaught exception (group=0x40018560)
04-14 02:53:00.466: E/AndroidRuntime(27452): FATAL EXCEPTION: Thread-22
04-14 02:53:00.466: E/AndroidRuntime(27452): java.lang.IllegalStateException
04-14 02:53:00.466: E/AndroidRuntime(27452): at android.media.MediaPlayer.getCurrentPosition(Native Method)
04-14 02:53:00.466: E/AndroidRuntime(27452): at net.cybercore.collapsingfromwithin.music.run(music.java:145)
04-14 02:53:00.466: E/AndroidRuntime(27452): at java.lang.Thread.run(Thread.java:1019)
线错误145是:
int currentPosition = mp.getCurrentPosition();
我不能为我的生活弄清楚为什么它可以播放3到4次然后它会杀死应用程序。
感谢任何帮助。我已经查看了其他几个网站,例如http://www.androidhive.info/2012/03/android-building-audio-player-tutorial/和http://www.androiddevblog.net/android/playing-audio-in-android
**
** 我想我修好了。感谢您的帮助,我发现Thread using for seekbar on android mediaplayer所以我将其更改为
@Override
public void run() {
// TODO Auto-generated method stub
try {
while (mp != null) {
int currentPosition = mp.getCurrentPosition();
Message msg = new Message();
msg.what = currentPosition;
threadHandler.sendMessage(msg);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
System.out.println("interrupt exeption" + e);
}
}
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("My exeption" + e);
}
}
我仍然得到错误,但他们没有杀死我的应用程序。我不认为这是正确的方法,但它的工作。
答案 0 :(得分:0)
准备媒体播放器时,应准备好它。
MediaPlayer对象必须先进入Prepared状态才能开始播放。 有两种方法(同步与异步)可以达到Prepared状态:调用prepare()(同步),一旦方法调用返回就将对象传递给Prepared状态,或者调用prepareAsync()(异步),它在调用返回后首先将对象转移到Preparation状态(几乎正确地发生),同时内部播放器引擎继续处理其余的准备工作,直到准备工作完成。当准备完成或者prepare()调用返回时,如果事先通过setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)注册了OnPreparedListener,则内部播放器引擎会调用OnPreparedListener接口的用户提供的回调方法onPrepared()。
阅读here
所以你应该在设备播放后调用mp.prepare()。
你也应该确保播放媒体播放器以运行run方法。我首先补充一下 mp.isPlaying()到while行。
while (mp != null && mp.isPlaying()) {
...
}
IllegalStateException意味着您处于非法状态以调用该方法,例如,如果播放器已停止。
我不确定,但我认为这会在您暂停音乐时停止播放方法。所以你应该尽量避免这种情况。我创建一个布尔值来识别播放器正在播放或暂停并在此时使用它。