gpt4 book ai didi

java - 尝试在我的录音机的同一按钮上实现播放/暂停功能

转载 作者:行者123 更新时间:2023-12-01 11:11:01 25 4
gpt4 key购买 nike

我一直在 stackoverflow 上搜索与我的问题相关的答案,但到目前为止还没有找到任何内容。这是我的代码 fragment ,我不确定 onCreate 中的 onClick 方法是否走在正确的轨道上,因为我对 Java 还比较陌生。

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

duration = (TextView) findViewById(R.id.audioDuration);
seekbar = (SeekBar) findViewById(R.id.seek_bar);

outputFile = Environment.getExternalStorageDirectory().getAbsolutePath() + "/recording.3gp";

Button b1 = (Button) findViewById(R.id.btnRecord);
b1.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
startRecording();
break;
case MotionEvent.ACTION_UP:
stopRecording();
seekbar.clearAnimation();
break;
}
return false;
}
});
final Button play = (Button) findViewById(R.id.play);
play.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){

}
});
}
public void playRecording(View view){
try{
mediaPlayer.setDataSource(outputFile);
}
catch(IOException e){
e.printStackTrace();
}
try{
mediaPlayer.prepare();
}
catch (IOException e){
e.printStackTrace();
}
mediaPlayer.start();
timeElapsed = mediaPlayer.getCurrentPosition();
durationHandler.postDelayed(updateSeekBarTime, 100);
Toast.makeText(getApplicationContext(), "Playing Audio..", Toast.LENGTH_LONG).show();
}

最佳答案

做法很简单 - 只需添加 play.setText("Play/Pause") 即可。对于您的代码,您可以这样做 -

final Button play = (Button) findViewById(R.id.play);
boolean isPlaying = false;
play.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){
if (isPlaying) {
play.setText("Pause");
isPlaying = false;
//ADD CODE TO PAUSE HERE
} else {
play.setText("Play");
isPlaying = true;
//ADD CODE TO PLAY HERE
}
}
});

有什么问题就尽管问吧。另外,如果这对您有用,请将此答案标记为已选择

关于java - 尝试在我的录音机的同一按钮上实现播放/暂停功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32372577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com