gpt4 book ai didi

android - 如何获取音频流的持续时间并从任何点继续音频流

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:00:07 27 4
gpt4 key购买 nike

描述:

我有以下音频播放器代码。我可以通过单击进度条(在 0-to-mediaplayer.getDuration() 之间)从任何持续时间继续音频播放。它非常适合音频播放。

音频流问题:

  • 当我从互联网服务器(例如 s3-bucket)流式传输音频文件时它开始正确流式传输。
  • 但是 mediaPlayer.getDuration() 和 mediaPlayer.getCurrentPosition()返回错误的值。在流媒体开始时mediaPlayer.getCurrentPosition() 返回 5 小时。
  • 因此,我无法继续从指定的流持续时间(0 到流持续时间)。

问题:

  1. 如何获取音频流的持续时间
  2. 如何从指定的持续时间继续音频流。为了例如,对于一个时长为 10 分钟的文件,我想从 6 号开始播放分钟。

代码:

public class MyAudioPlayer extends Activity 
implements OnClickListener{


MediaPlayer mediaPlayer = null;
private boolean isPaused=false;
private boolean isStop = true;

String filePath = null;
String productName = null;

ImageButton btnPlay = null;
ImageButton btnPause = null;
ImageButton btnReset = null;
ImageButton btnStop = null;

AudioManager audioManager = null;
SeekBar volControl = null;
SeekBar progressControl = null;
TextView progressText = null;
long durationInMillis = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ltd_audio_player);

volControl = (SeekBar)findViewById(R.id.player_volume);
progressControl = (SeekBar)findViewById(R.id.player_seekbar);
progressText = (TextView) findViewById(R.id.player_progress_text);

btnPlay = (ImageButton) findViewById(R.id.ic_player_play);

btnPause = (ImageButton) findViewById(R.id.ic_player_pause);

btnReset = (ImageButton) findViewById(R.id.ic_player_reset);

btnStop = (ImageButton) findViewById(R.id.ic_player_stop);

btnPlay.setOnClickListener(this);
btnPause.setOnClickListener(this);
btnReset.setOnClickListener(this);
btnStop.setOnClickListener(this);

filePath = getIntent().getExtras().getString("localPath");

this.setPlayer();
this.resetAndStartPlayer();


}

@Override
protected void onResume() {
super.onResume();
isPaused=false;
progressText.postDelayed(onEverySecond, 1000);
}

@Override
protected void onPause() {
super.onPause();

isPaused=true;
}
private void setProgressControl() {
int maxVolume = mediaPlayer.getDuration();
int curVolume = mediaPlayer.getCurrentPosition();

progressControl.setMax(maxVolume);
progressControl.setProgress(curVolume);
progressControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {

@Override
public void onProgressChanged(SeekBar seekbar, int progress, boolean fromUser) {
mediaPlayer.seekTo(progress);
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}
});
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.ic_player_play:
if(isStop==true){
try {
mediaPlayer.prepareAsync();
} catch (Exception e) {
e.printStackTrace();
}
}else{
mediaPlayer.start();
isStop = true;
}
break;
case R.id.ic_player_pause:
mediaPlayer.pause();
break;
case R.id.ic_player_reset:
mediaPlayer.seekTo(0);
break;
case R.id.ic_player_stop:
isStop = true;
progressControl.setProgress(0);
mediaPlayer.stop();
break;
}

}
private void resetAndStartPlayer(){
try {
if(filePath!=null){
mediaPlayer.setDataSource(filePath);
mediaPlayer.prepareAsync();
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void setPlayer(){

getWindow().setFormat(PixelFormat.UNKNOWN);
mediaPlayer = new MediaPlayer();

mediaPlayer.setOnBufferingUpdateListener(new OnBufferingUpdateListener() {

@Override
public void onBufferingUpdate(MediaPlayer mp, int percent) {
progressControl.setSecondaryProgress((progressControl.getMax()/100)*percent);

}
});
mediaPlayer.setOnPreparedListener(new OnPreparedListener() {

@Override
public void onPrepared(MediaPlayer mp) {
mediaPlayer.start();
isStop=false;
durationInMillis = mediaPlayer.getDuration();
MyAudioPlayer.this.setProgressControl();
}
});
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
mediaPlayer.release();
super.onDestroy();
}
protected void setProgressText() {
durationInMillis = mediaPlayer.getDuration();
int curVolume = mediaPlayer.getCurrentPosition();
long HOUR = 60*60*1000;
if(progressText!=null){
if(durationInMillis>HOUR){
progressText.setText(String.format("%1$tH:%1$tM:%1$tS", new Date(curVolume))
+" / "+String.format("%1$tH:%1$tM:%1$tS", new Date(durationInMillis)));
}else{
progressText.setText(String.format("%1$tM:%1$tS", new Date(curVolume))
+" / "+String.format("%1$tM:%1$tS", new Date(durationInMillis)));
}
}
}
private Runnable onEverySecond=new Runnable() {
public void run() {

if (mediaPlayer!=null) {
progressControl.setProgress(mediaPlayer.getCurrentPosition());

MyAudioPlayer.this.setProgressText();
}

if (!isPaused) {
progressText.postDelayed(onEverySecond, 1000);
}
}
};
}

时间显示在进度条上方。

时间:'当前持续时间'/'总持续时间'

enter image description here

最佳答案

希望它能解决您的问题。

1)音频流的时长和进度

我查看了您的代码,您的代码在计算时间方面存在重大错误。您创建新的日期(durationInMillis)。日期添加了您的所在地,即 GMT+XX 小时,这就是为什么您在流式传输开始时获得 5 小时的原因。您应该使用以下方法来计算 currentProgress/duration。

protected void setProgressText() {

final int HOUR = 60*60*1000;
final int MINUTE = 60*1000;
final int SECOND = 1000;

int durationInMillis = mediaPlayer.getDuration();
int curVolume = mediaPlayer.getCurrentPosition();

int durationHour = durationInMillis/HOUR;
int durationMint = (durationInMillis%HOUR)/MINUTE;
int durationSec = (durationInMillis%MINUTE)/SECOND;

int currentHour = curVolume/HOUR;
int currentMint = (curVolume%HOUR)/MINUTE;
int currentSec = (curVolume%MINUTE)/SECOND;

if(durationHour>0){
System.out.println(" 1 = "+String.format("%02d:%02d:%02d/%02d:%02d:%02d",
currentHour,currentMint,currentSec, durationHour,durationMint,durationSec));
}else{
System.out.println(" 1 = "+String.format("%02d:%02d/%02d:%02d",
currentMint,currentSec, durationMint,durationSec));
}
}

2) 流的清理。

MediaPlayer 允许清理音频流。我已经在我的一个项目中实现了它,但这需要一些时间。从另一个位置恢复音频流需要一些时间。

关于android - 如何获取音频流的持续时间并从任何点继续音频流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8972394/

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