简体   繁体   中英

how to fastforward the song when i press and hold the button

When i play song in music player when i press a button i am forwarding the song for certain time,i am implementing this way,but i need when i press and hold the fast forward button wan to move the song means acting as our real music player,how can i am implementing please help me how i implement the fast forward button functionality is same as our music player fast forward button.

public class MainActivity extends Activity implements OnCompletionListener,
            SeekBar.OnSeekBarChangeListener {

        private ImageButton btnDecreaseSound, btnIncSound, btnReverse, btnPlay, btnForward,
                btnPause;
        private TextView currenttime, endtime;

        private LinearLayout btnRedo, btnDelete;
        private SeekBar songProgressBar;
        private MediaPlayer mp;
        private SongsManager songManager;
        private Utilities utils;
        private int seekForwardTime = 5000; // 5000 milliseconds
        private int seekBackwardTime = 5000; // 5000 milliseconds
        private int currentSongIndex = 0;
        private SeekBar volumeSeekbar ;
        private AudioManager audioManager ;
        private Handler mHandler = new Handler();
        private ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);



            // Mediaplayer
            mp = new MediaPlayer();
            songManager = new SongsManager();
            utils = new Utilities();
             final SoundPool spool = null;
                final int soundID = 0;



             audioManager = (AudioManager) getSystemService(getApplicationContext().AUDIO_SERVICE);
            volumeSeekbar.setMax(audioManager
                    .getStreamMaxVolume(AudioManager.STREAM_MUSIC));
            volumeSeekbar.setProgress(audioManager
                    .getStreamVolume(AudioManager.STREAM_MUSIC)); 



            // Listeners
            songProgressBar.setOnSeekBarChangeListener(this); // Important
            mp.setOnCompletionListener(this); // Important

            // Getting all songs list
            songsList = songManager.getPlayList();

            // By default play first song

            btnPlay.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {


                    if (mp.isPlaying()) {
                        if (mp != null) {
                            mp.pause();
                            // Changing button image to play button
                            btnPlay.setImageResource(R.drawable.playbtn);
                        }
                    } else {
                        if (mp != null) {
                            mp.start();
                            //
                            btnPlay.setImageResource(R.drawable.pauseiconbtn);
                        }
                    }

                }
            });




            volumeSeekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() 
            {
                @Override
                public void onStopTrackingTouch(SeekBar arg0) 
                {
                }

                @Override
                public void onStartTrackingTouch(SeekBar arg0) 
                {
                }

                @Override
                public void onProgressChanged(SeekBar arg0, int progress, boolean arg2) 
                {
                    audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
                            progress, 0);
                }
            });


            btnReverse.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub

                    // get current song position
                    int currentPosition = mp.getCurrentPosition();
                    // check if seekBackward time is greater than 0 sec
                    if (currentPosition - seekBackwardTime >= 0) {
                        // forward song
                        mp.seekTo(currentPosition - seekBackwardTime);
                    } else {
                        // backward to starting position
                        mp.seekTo(0);
                    }

                }
            });

            btnForward.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    int currentPosition = mp.getCurrentPosition();
                    // check if seekForward time is lesser than song duration
                    if (currentPosition + seekForwardTime <= mp.getDuration()) {
                        // forward song
                        mp.seekTo(currentPosition + seekForwardTime);
                    } else {
                        // forward to end position
                        mp.seekTo(mp.getDuration());
                    }
                }
            });

        public void playSong(int songIndex) {
            // Play song
            try {
                mp.reset();
                mp.setDataSource(songsList.get(songIndex).get("songPath"));
                mp.prepare();
                mp.start();
                // Displaying Song title
                String songTitle = songsList.get(songIndex).get("songTitle");

                songProgressBar.setProgress(0);
                songProgressBar.setMax(100);

                // Updating progress bar
                updateProgressBar();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        public void updateProgressBar() {
            mHandler.postDelayed(mUpdateTimeTask, 100);
        }

        private Runnable mUpdateTimeTask = new Runnable() {
            public void run() {
                long totalDuration = mp.getDuration();
                long currentDuration = mp.getCurrentPosition();

                // Displaying Total Duration time
                // endtime.setText(""+utils.milliSecondsToTimer(totalDuration));
                // Displaying time completed playing
                currenttime
                        .setText("" + utils.milliSecondsToTimer(currentDuration));

                //

                String l = utils.milliSecondsToTimer(currentDuration);
                String l2 = utils.milliSecondsToTimer(currentDuration);

                long number = totalDuration - currentDuration;

                String timechange = utils.milliSecondsToTimer(number);

                System.out.println("l value: " + timechange);
                endtime.setText("-" + timechange);
                // 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();
        }

        /**
         * On Song Playing completed if repeat is ON play same song again if shuffle
         * is ON play random song
         * */
        @Override
        public void onCompletion(MediaPlayer arg0) {

            // check for repeat is ON or OFF

            playSong(0);
            currentSongIndex = 0;

        }

        @Override
        public void onDestroy() {
            super.onDestroy();
            mp.release();
        }

you can not override Home button for your custom application. that is reserved. Please google it regarding overriding home button

Please go to this link\\, you can get your answer here. Android Overriding home key

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM