可能重複:
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