SeekBar and media player in android

前端 未结 12 1989
天涯浪人
天涯浪人 2020-11-28 03:13

I have a simple player and recorder. Everything works great but have a one problem. I want to add seek bar to see progress in playing record and use this seek bar to set pl

相关标签:
12条回答
  • 2020-11-28 04:06

    check this, you should give arguments in msecs, Dont just send progress to seekTo(int)

    and also check this getCurrentPostion() and getDuration().

    You can do some calcuations, ie., convert progress in msec like msce = (progress/100)*getDuration() then do seekTo(msec)

    Or else i have an easy idea, you don't need to change any code anywer else just add seekBar.setMax(mPlayer.getDuration()) once your media player is prepared.

    and here is link exactly what you want seek bar update

    0 讨论(0)
  • 2020-11-28 04:07

    I've used this tutorial with success, it's really simple to understand: www.androidhive.info/2012/03/android-building-audio-player-tutorial/

    This is the interesting part:

    /**
     * Update timer on seekbar
     * */
    public void updateProgressBar() {
        mHandler.postDelayed(mUpdateTimeTask, 100);
    }  
    
    /**
     * Background Runnable thread
     * */
    private Runnable mUpdateTimeTask = new Runnable() {
           public void run() {
               long totalDuration = mp.getDuration();
               long currentDuration = mp.getCurrentPosition();
    
               // Displaying Total Duration time
               songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));
               // Displaying time completed playing
               songCurrentDurationLabel.setText(""+utils.milliSecondsToTimer(currentDuration));
    
               // Updating progress bar
               int progress = (int)(utils.getProgressPercentage(currentDuration, totalDuration));
               //Log.d("Progress", ""+progress);
               songProgressBar.setProgress(progress);
    
               // Running this thread after 100 milliseconds
               mHandler.postDelayed(this, 100);
           }
        };
    
    /**
     *
     * */
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) {
    
    }
    
    /**
     * When user starts moving the progress handler
     * */
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // remove message Handler from updating progress bar
        mHandler.removeCallbacks(mUpdateTimeTask);
    }
    
    /**
     * When user stops moving the progress hanlder
     * */
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
        mHandler.removeCallbacks(mUpdateTimeTask);
        int totalDuration = mp.getDuration();
        int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
    
        // forward or backward to certain seconds
        mp.seekTo(currentPosition);
    
        // update timer progress again
        updateProgressBar();
    }
    
    0 讨论(0)
  • 2020-11-28 04:08

    The below code worked for me.

    I've created a method for seekbar

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        mp.start();
         getDurationTimer();
        getSeekBarStatus();
    
    
    }
    //Creating duration time method
    public void getDurationTimer(){
        final long minutes=(mSongDuration/1000)/60;
        final int seconds= (int) ((mSongDuration/1000)%60);
        SongMaxLength.setText(minutes+ ":"+seconds);
    
    
    }
    
    
    
     //creating a method for seekBar progress
    public void getSeekBarStatus(){
    
        new Thread(new Runnable() {
    
            @Override
            public void run() {
                // mp is your MediaPlayer
                // progress is your ProgressBar
    
                int currentPosition = 0;
                int total = mp.getDuration();
                seekBar.setMax(total);
                while (mp != null && currentPosition < total) {
                    try {
                        Thread.sleep(1000);
                        currentPosition = mp.getCurrentPosition();
                    } catch (InterruptedException e) {
                        return;
                    }
                    seekBar.setProgress(currentPosition);
    
                }
            }
        }).start();
    
    
    
    
    
        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            int progress=0;
    
            @Override
            public void onProgressChanged(final SeekBar seekBar, int ProgressValue, boolean fromUser) {
                if (fromUser) {
                    mp.seekTo(ProgressValue);//if user drags the seekbar, it gets the position and updates in textView.
                }
                final long mMinutes=(ProgressValue/1000)/60;//converting into minutes
                final int mSeconds=((ProgressValue/1000)%60);//converting into seconds
                SongProgress.setText(mMinutes+":"+mSeconds);
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
        });
    }
    

    SongProgress and SongMaxLength are the TextView to show song duration and song length.

    0 讨论(0)
  • 2020-11-28 04:14

    To create a 'connection' between SeekBar and MediaPlayer you need first to get your current recording max duration and set it to your seek bar.

    mSeekBar.setMax(mFileDuration/1000); // where mFileDuration is mMediaPlayer.getDuration();
    

    After you initialise your MediaPlayer and for example press play button, you should create handler and post runnable so you can update your SeekBar (in the UI thread itself) with the current position of your MediaPlayer like this :

    private Handler mHandler = new Handler();
    //Make sure you update Seekbar on UI thread
    MainActivity.this.runOnUiThread(new Runnable() {
    
        @Override
        public void run() {
            if(mMediaPlayer != null){
                int mCurrentPosition = mMediaPlayer.getCurrentPosition() / 1000;
                mSeekBar.setProgress(mCurrentPosition);
            }
            mHandler.postDelayed(this, 1000);
        }
    });
    

    and update that value every second.

    If you need to update the MediaPlayer's position while user drag your SeekBar you should add OnSeekBarChangeListener to your SeekBar and do it there :

            mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
                @Override
                public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {                
                    if(mMediaPlayer != null && fromUser){
                        mMediaPlayer.seekTo(progress * 1000);
                    }
                }
        });
    

    And that should do the trick! : )

    EDIT: One thing which I've noticed in your code, don't do :

    public MainActivity() {
        mFileName = Environment.getExternalStorageDirectory().getAbsolutePath();
        mFileName += "/audiorecordtest.3gp";
    }
    

    make all initialisations in your onCreate(); , do not create constructors of your Activity.

    0 讨论(0)
  • 2020-11-28 04:14

    After you initialize your MediaPlayer and SeekBar, you can do this :

     Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                mSeekBar.setProgress(mMediaPlayer.getCurrentPosition());
            }
        },0,1000);
    

    This updates SeekBar every second(1000ms)

    And for updating MediaPlayer, if user drag SeekBar, you must add OnSeekBarChangeListener to your SeekBar :

    mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                mMediaPlayer.seekTo(i);
            }
    
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
    
            }
    
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
    
            }
        });
    

    HAPPY CODING!!!

    0 讨论(0)
  • 2020-11-28 04:14
        int  pos  = 0;
        yourSeekBar.setMax(mPlayer.getDuration());
    

    After You start Your MediaPlayer i.e mplayer.start()

    Try this code

    while(mPlayer!=null){
             try {
                    Thread.sleep(1000);
                    pos  = mPlayer.getCurrentPosition();
                }  catch (Exception e) {
                    //show exception in LogCat
                }
                yourSeekBar.setProgress(pos);
    
       }
    

    Before you added this code you have to create xml resource for SeekBar and use it in Your Activity class of ur onCreate() method.

    0 讨论(0)
提交回复
热议问题