Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Audio Playing from URI Inside Listview , But Seekbar is not Updating in Android Listview item

  1. I am Playing a Audio from Uri Its Working Fine.
  2. Clicking a Button from Each Listview item.

Problem :Audio is Playing in the Listview ,but still Seekbar is not Moving(Updating).

EDIT:1

1.Audio is Playing in the Each Listview Item Perfectly,But Seekbar is Not Working(Not Updating).

Please Help me top solve this Issue.

My Listview Array adapter Class:

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

    }
like image 947
Kumar Avatar asked Jul 21 '16 12:07

Kumar


2 Answers

you need to update your seekbar position(value) at every second..

To set seekbar Max value..

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

and update it every second to show progress

Handler mHandler = new Handler();
    runOnUiThread(new Runnable() {
                      @Override
                      public void run() {
                          seek_bar_view.setProgress((int) player.getCurrentPosition());
                      }
                      mHandler.postDelayed(this,1000);
                  }
    );
like image 124
Uttam Panchasara Avatar answered Nov 07 '22 03:11

Uttam Panchasara


See this:

 seek_bar_view.setProgress(player.getCurrentPosition()); 

here player.getCurrentPosition() returns the time in millis , you need to convert this to int and then set the progress to seekBar.

Try this:

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

and now get the percentage for your SeekBar like this:

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

seek_bar_view.setProgress(currentProgress); 

Edited:

For your specific case, inside a ListView item:

You will need to notify the adapter each time you change the position of seekbar. For this, the simplest approach would be to take a variable inside the POJO class you are using to set the adapter.

Inside your POJO class

   int progress=0;

In your adapter,set the seekbar progress

   seekbar.setProgress(progress);

In your adapter,change the value of progress and notifyadapter

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

//Re-Edited:

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

}        
});        
like image 23
karan vs Avatar answered Nov 07 '22 02:11

karan vs