可能重复:
Android: How to stop media (mp3) in playing when specific milliseconds come?Android:如何在特定的时间内停止播放媒体(mp3)?
我有一个mp3文件,我想在其中发挥一个特定的单词。我有一个开始时间(6889毫秒)和结束时间(7254毫秒),所以他们的持续时间是365毫秒。我想播放我的mp3文件的365 ms部分。
当我运行这些代码时,它会播放单词“他”,但播放“He wan”。 “他”之后的单词是“通缉令”。我只想玩“他”这个词。那可能吗?
package com.example.playword;
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.widget.TextView;
public class PlayWord extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Handler mHandler = new Handler();
final TextView labelTxt = (TextView) findViewById(R.id.labelTxt);
labelTxt.setText("Playing word...");
final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.nicholas);
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mPlayer.seekTo(6889); //this is the start time of the word i want to play
mPlayer.start();
//how can I end it at 7254?
mHandler.post(new Runnable(){
public void run(){
//7254 is the end time of the word i want to play
//7410 is the start time of another word
if(mPlayer.getCurrentPosition() > 7254 && mPlayer.getCurrentPosition() < 7410){
labelTxt.setText("Stop: " + mPlayer.getCurrentPosition());
mPlayer.stop();
}
mHandler.postDelayed(this, 1);
}
});
}
}
别问了(几乎)同样的问题:http://stackoverflow.com/questions/5454452/android-how-to-stop-media-mp3-in-playing-when-specific-milliseconds-来吧 – 2011-03-28 11:21:43
谢谢你把它链接到我的其他问题:) – Kris 2011-03-28 23:33:37