gpt4 book ai didi

android - Seekbar在媒体播放器实现中没有进展

转载 作者:行者123 更新时间:2023-12-03 02:12:39 26 4
gpt4 key购买 nike

我尝试使用此代码来实现使用seekbar的音频播放。但是seekbar没有被调用。有人可以告诉我我在哪里做错了吗?我想将录制时间限制设置为2分钟。这是我的代码,

   @Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);

setContentView(R.layout.option4);

startPlaying = (Button) findViewById(R.id.play);
stopPlaying = (Button) findViewById(R.id.stop);
startRecord = (Button) findViewById(R.id.start);
seekBar = (SeekBar) findViewById(R.id.seek_bar);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setEnabled(false);


startRecord.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

ditchMediaRecorder();
mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
mFileName += "/audiorecordtest.3gp";
File outputFile=new File(mFileName);
if(outputFile.exists()){
outputFile.delete();
}
mRecorder=new MediaRecorder();
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);

mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
mRecorder.setOutputFile(mFileName);
try {
mRecorder.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


mRecorder.start();
}
});



startPlaying.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
ditchMediaPlayer();
mPlayer=new MediaPlayer();
try {
mPlayer.setDataSource(mFileName);
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
mPlayer.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
seekBar.setEnabled(true);
seekBar.setMax(mPlayer.getDuration());

mPlayer.start();


new Thread().start();

}
});

stopPlaying.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
if(mRecorder!=null){
mRecorder.stop();
}
}
});

}


private void ditchMediaPlayer() {
if(mPlayer!=null){
mPlayer.release();
}
}


private void ditchMediaRecorder() {
// TODO Auto-generated method stub
if(mRecorder!=null){
mRecorder.release();
}

}

@Override
public void run() {
int currentPosition = mPlayer.getCurrentPosition();
int total = mPlayer.getDuration();
System.out.println("In run");
while (mPlayer != null && currentPosition < total) {
try {
Thread.sleep(1000);
System.out.println("In run try block");
currentPosition = mPlayer.getCurrentPosition();
} catch (InterruptedException e) {
return;
} catch (Exception e) {
return;
}
seekBar.setProgress(currentPosition);
}

}

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
try {
System.out.println("In progres");
if (mPlayer.isPlaying() || mPlayer != null) {
if (fromUser)
mPlayer.seekTo(progress);
} else if (mPlayer== null) {

seekBar.setProgress(0);
}
} catch (Exception e) {
Log.e("seek bar", "" + e);
seekBar.setEnabled(false);
}
}

音频将被录制并播放良好,但我的搜索栏无法控制。我尝试了不同的方法,但没有成功。请帮助我!

谢谢!!!

最佳答案

这对我有用:

public void forwardSong() {
if (mPlayer != null) {
int currentPosition = mPlayer.getCurrentPosition();
if (currentPosition + seekForwardTime <= mPlayer.getDuration()) {
mPlayer.seekTo(currentPosition + seekForwardTime);
} else {
mPlayer.seekTo(mPlayer.getDuration());
}
}
}

另外,请查看以下链接:
  • Android Audio Seekbar Example 1
  • Andorid Audio Seekbar Example 2
  • 关于android - Seekbar在媒体播放器实现中没有进展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22760241/

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