从列表视图中的URI播放audio,但Seekbar不在Android Listview项目中更新

  1. 我正在播放来自Uri的audio
  2. 单击每个ListView项目中的button。

问题: audio在 列表视图中 播放 ,但Seekbar仍在移动( 更新 )。

编辑:1

1. audio播放在每个ListView项目完美但Seekbar不工作(不更新)

请帮我解决这个问题。

我的Listviewarrays适配器类:

Adapter.class

private static final int UPDATE_FREQUENCY = 500; int progress=0; public View getView(final int position, View view, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.audio_listview, null, true); ListenAUdioButton = (Button) rowView.findViewById(R.id.ListenAudiobuttonxml); seek_bar_view = (SeekBar) rowView.findViewById(R.id.seek_bar); ListenAUdioButton.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // text_shown.setText("Playing..."); try { try { // get Internet status isInternetPresent = cd1.isConnectingToInternet(); // check for Internet status if (isInternetPresent) { if (!itemname3_AUDIO_FILE[position].equals("") || !itemname3_AUDIO_FILE[position].equals("null")) { System.out.println(" AUDIO FILE :-)" + itemname3_AUDIO_FILE[position]); player = new MediaPlayer(); player.setAudioStreamType(AudioManager.STREAM_MUSIC); player.setDataSource(context, Uri.parse(itemname3_AUDIO_FILE[position])); player.prepareAsync(); player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { @Override public void onPrepared(MediaPlayer mp) { try { mp.start(); seek_bar_view.setMax(player.getDuration()); updatePosition(); } catch (Exception e) { e.printStackTrace(); } } }); player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { @Override public void onCompletion(MediaPlayer mp) { stopPlay(); } }); MediaPlayer.OnErrorListener onError = new MediaPlayer.OnErrorListener() { @Override public boolean onError(MediaPlayer mp, int what, int extra) { // returning false will call the OnCompletionListener return false; } }; } else { Toast.makeText(getContext(), "Audio Not Found..!", Toast.LENGTH_SHORT).show(); } } else { Toast.makeText(getContext(), "Please Check Your Internet Connection..!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); Toast.makeText(getContext(), "Audio Not Found..!", Toast.LENGTH_SHORT).show(); } } catch (Exception e) { e.printStackTrace(); } } }); return rowView; } private void stopPlay() { player.stop(); player.reset(); // playButton.setImageResource(android.R.drawable.ic_media_play); handler.removeCallbacks(updatePositionRunnable); seek_bar_view.setProgress(0); // isStarted = false; } private final Handler handler = new Handler(); private final Runnable updatePositionRunnable = new Runnable() { public void run() { updatePosition(); } }; private void updatePosition() { handler.removeCallbacks(updatePositionRunnable); seek_bar_view.setProgress(progress); progress=getProgressPercentage(player.getCurrentPosition(),player.getDuration(); notifyDataSetChanged(); handler.postDelayed(updatePositionRunnable, UPDATE_FREQUENCY); } 

Solutions Collecting From Web of "从列表视图中的URI播放audio,但Seekbar不在Android Listview项目中更新"

你需要在每一秒钟更新你的seekbar位置(值)。

设置seekbar最大值

 seek_bar_view.setMax((int) player.getDuration()); 

并每秒更新一次以显示进度

 Handler mHandler = new Handler(); runOnUiThread(new Runnable() { @Override public void run() { seek_bar_view.setProgress((int) player.getCurrentPosition()); } mHandler.postDelayed(this,1000); } ); 

看到这个:

  seek_bar_view.setProgress(player.getCurrentPosition()); 

这里player.getCurrentPosition()返回毫秒的时间,你需要将其转换为int,然后将进度设置为seekBar。

尝试这个:

  public static int getProgressPercentage(long currentDuration, long totalDuration){ Double percentage = (double) 0; long currentSeconds = (int) (currentDuration / 1000); long totalSeconds = (int) (totalDuration / 1000); // calculating percentage percentage =(((double)currentSeconds)/totalSeconds)*100; // return percentage return percentage.intValue(); } 

现在得到你的SeekBar这样的百分比:

 int currentProgress=getProgressPercentage(player.getCurrentPosition(), player.getDuration()); seek_bar_view.setProgress(currentProgress); 

编辑:

对于您的具体情况,在一个ListView项目内:

每次更改seekbar的位置时,都需要通知适配器。 为此,最简单的方法是在POJO类中使用一个variables来设置适配器。

在你的POJO课堂内

  int progress=0; 

在您的适配器中,设置search栏进度

  seekbar.setProgress(progress); 

在您的适配器中,更改进度和通知适配器的值

  progress=getProgressPercentage(player.getCurrentPosition(), player.getDuration()); notifyDataSetChanged() 

//重新编辑:

 seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() { @Override public void onStopTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) { // TODO Auto-generated method stub notifyDataSetChanged(); } });