从List Inside Listview中播放音频,但Seekbar未在Android Listview项目中更新

时间:2016-07-21 12:30:40

标签: android listview audio android-mediaplayer

  1. 我正在播放Uri的音频工作正常。
  2. 单击每个Listview项目中的按钮。
  3. 问题:音频正在列表视图中播放,但搜索栏仍未移动(正在更新)。

    修改:1

    1. 音频正在每个Listview项目中完美播放但是Seekbar无效(未更新)

    请帮我解决这个问题。

    我的 Listview数组适配器类:

    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);
    
        }
    

2 个答案:

答案 0 :(得分:4)

你需要每秒更新你的搜索栏位置(值)..

设置搜索栏最大值..

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);
                  }
    );

答案 1 :(得分:1)

见:

 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项目中:

每次更改搜索栏的位置时,都需要通知适配器。 为此,最简单的方法是在用于设置适配器的POJO类中获取一个变量。

在POJO课程中

   int progress=0;

在适配器中,设置搜索栏进度

   seekbar.setProgress(progress);

在适配器中,更改progress和notifyadapter的值

 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();

}        
});