当我按住按钮时如何快进歌曲

时间:2014-02-14 06:26:49

标签: android android-music-player

当我在音乐播放器中播放歌曲时,我按下按钮我将歌曲转发一段时间,我正在实现这种方式,但我需要当我按住快进按钮时,移动歌曲意味着充当我们真正的音乐播放器,我如何实现请帮助我如何实现快进按钮功能与我们的音乐播放器快进按钮相同。

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

1 个答案:

答案 0 :(得分:0)

您无法覆盖自定义应用程序的“主页”按钮。这是保留的。请关注覆盖主页按钮

请转到此链接\,您可以在此处获得答案。 Android Overriding home key