【发布时间】:2011-03-28 17:43:21
【问题描述】:
可能重复:
Android: How to stop media (mp3) in playing when specific milliseconds come?
我有一个 mp3 文件,我想在其中播放特定的单词。我有一个开始时间(6889 毫秒)和结束时间(7254 毫秒),所以它们的持续时间是 365 毫秒。我想播放我的 mp3 文件的 365 毫秒部分。
当我运行这些代码时,它应该播放“He”这个词,但它播放“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);
}
});
}
}
【问题讨论】:
-
不要问(几乎)相同的问题:stackoverflow.com/questions/5454452/…
-
感谢您将其链接到我的其他问题 :)
标签: android media-player