我试图通过 MediaPlayer
创建一个可以在指定时间内播放mp3文件的应用例如:
开始时间: 00:42
结束时间: 01:23
我的问题是:
- 是否可以通过MM:SS格式的textview指定开始和结束时间?
谢谢!
答案 0 :(得分:1)
启动MediaPlayer并加载文件后,您可以在使用特定时间后暂停或停止播放器
while(mPlayer.isPlaying()) {
if(mPlayer.getCurrentPosition() > 7254 && mPlayer.getCurrentPosition() < 7410 ){
labelTxt.setText("Stop: " + mPlayer.getCurrentPosition() );
mPlayer.stop();
break;
}
}
答案 1 :(得分:0)
使用此代码
MediaPlayer mp;
Runnable stopPlayerTask = new Runnable(){
@Override
public void run() {
mp.pause();
}};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText etStartTime = (EditText)findViewById(R.id.edittext_start_time);
EditText etEndTime = (EditText)findViewById(R.id.edittext_end_time);
String StartTime = etStartTime.getText().toString();
String EndTime = etEndTime.getText().toString();
long min = Integer.parseInt(StartTime .substring(0, 2));
long sec = Integer.parseInt(StartTime .substring(3));
long minEnd = Integer.parseInt(StartTime .substring(0, 2));
long secEnd = Integer.parseInt(StartTime .substring(3));
long startLong = (min * 60L) + sec;
long endLong = (minEnd * 60) + secEnd;
int starIntTime = (int) startLong;
int endIntTime = (int) endLong;
mp = MediaPlayer.create(this, R.raw.my_sound_file);
mp.seekTo(starIntTime);
mp.start();
Handler handler = new Handler();
handler.postDelayed(stopPlayerTask, endIntTime);
}