如何在手指上下滑动时增加和减少媒体播放器的声音?

时间:2016-09-16 09:58:28

标签: android

我是Android开发的新手,目前我正在开发简单的音乐播放器应用程序。这里的问题是如何在我的主要音乐播放器活动上滑动手指时增加和减少音乐播放器音量。

这是我的音乐播放器片段

public class Player extends Fragment implements WheelModel.Listener,MediaPlayer.OnCompletionListener,SeekBar.OnSeekBarChangeListener{

private ImageButton btnPlay;
private ImageButton btnForward;
private ImageButton btnBackward;
private ImageButton btnNext;
private ImageButton btnPrevious;
private ImageButton btnRepeat;
private ImageButton btnShuffle;
private SeekBar songProgressBar;
private TextView songTitleLabel;
private TextView songCurrentDurationLabel;
private TextView songTotalDurationLabel;
// Media Player
public static MediaPlayer mp,mp2;
VisualizerView mVisualizerView;
SliderView sl;
private Visualizer mVisualizer;
// Handler to update UI timer, progress bar etc,.
private Handler mHandler = new Handler();;
private SongsManager songManager;
private Utilities utils;
private int seekForwardTime = 5000; // 5000 milliseconds
private int seekBackwardTime = 5000; // 5000 milliseconds
private int currentSongIndex = 0;
private boolean isShuffle = false;
private boolean isRepeat = false;
private ArrayList<SongModel> songsList = new ArrayList<SongModel>();
int songIndex;
AudioManager audioManager;
SongAdapter songAdapter;

public Player(int position) {
    songIndex = position;
}

public Player() {

}


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View android = inflater.inflate(R.layout.player, container, false);
        ClickWheel wheel = (ClickWheel) android.findViewById(R.id.wheel);
        wheel.getModel().addListener(this);
        return android;

    }
@Override
public void onViewCreated(View v, Bundle savedInstanceState) {
    super.onViewCreated(v, savedInstanceState);

    btnPlay = (ImageButton)getView().findViewById(R.id.btnPlay);
    btnForward = (ImageButton) getView().findViewById(R.id.btnForward);
    btnBackward = (ImageButton) getView().findViewById(R.id.btnBackward);
    btnNext = (ImageButton) getView().findViewById(R.id.btnNext);
    btnPrevious = (ImageButton) getView().findViewById(R.id.btnPrevious);
    btnRepeat = (ImageButton) getView().findViewById(R.id.btnRepeat);
    btnShuffle = (ImageButton) getView().findViewById(R.id.btnShuffle);
    songProgressBar = (SeekBar) getView().findViewById(R.id.songProgressBar);
    songTitleLabel = (TextView) getView().findViewById(R.id.songTitle);
    songTitleLabel.setSelected(true);
    songCurrentDurationLabel = (TextView) getView().findViewById(R.id.songCurrentDurationLabel);
    songTotalDurationLabel = (TextView) getView().findViewById(R.id.songTotalDurationLabel);


    mp = new MediaPlayer();

    songManager = new SongsManager();
    utils = new Utilities();

    songProgressBar.setOnSeekBarChangeListener(this);
    mp.setOnCompletionListener(this);
    songsList = songManager.getPlayList();

    if (songIndex == 0) {
        playSong(0);
    } else {
        playSong(songIndex);
    }
    btnPlay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // check for already playing
            if (mp.isPlaying()) {
                if (mp != null) {
                    mp.pause();
                    btnPlay.setImageResource(R.drawable.btn_play);
                }
            } else {
                // Resume song
                if (mp != null) {
                    mp.start();
                    btnPlay.setImageResource(R.drawable.btn_pause);
                }
            }

        }
    });
    btnForward.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            int currentPosition = mp.getCurrentPosition();
            if(currentPosition + seekForwardTime <= mp.getDuration()){
                mp.seekTo(currentPosition + seekForwardTime);
            }else{
                mp.seekTo(mp.getDuration());
            }
        }
    });
    btnBackward.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            int currentPosition = mp.getCurrentPosition();
            if(currentPosition - seekBackwardTime >= 0){
                mp.seekTo(currentPosition - seekBackwardTime);
            }else{
                mp.seekTo(0);
            }

        }
    });
    btnNext.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(currentSongIndex < (songsList.size() - 1)){
                playSong(currentSongIndex + 1);
                currentSongIndex = currentSongIndex + 1;
            }else{
                playSong(0);
                currentSongIndex = 0;
            }

        }
    });
    btnPrevious.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(currentSongIndex > 0){
                playSong(currentSongIndex - 1);
                currentSongIndex = currentSongIndex - 1;
            }else{
                playSong(songsList.size() - 1);
                currentSongIndex = songsList.size() - 1;
            }

        }
    });
    btnRepeat.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(isRepeat){
                isRepeat = false;
                Toast.makeText(getActivity().getApplicationContext(), "Repeat is OFF", Toast.LENGTH_SHORT).show();
                btnRepeat.setImageResource(R.drawable.btn_repeat);
            }else{
                isRepeat = true;
                Toast.makeText(getActivity().getApplicationContext(), "Repeat is ON", Toast.LENGTH_SHORT).show();
                isShuffle = false;
                btnRepeat.setImageResource(R.drawable.btn_repeat_focused);
                btnShuffle.setImageResource(R.drawable.btn_shuffle);
            }
        }
    });
    btnShuffle.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            if(isShuffle){
                isShuffle = false;
                Toast.makeText(getActivity().getApplicationContext(), "Shuffle is OFF", Toast.LENGTH_SHORT).show();
                btnShuffle.setImageResource(R.drawable.btn_shuffle);
            }else{
                isShuffle= true;
                Toast.makeText(getActivity().getApplicationContext(), "Shuffle is ON", Toast.LENGTH_SHORT).show();
                isRepeat = false;
                btnShuffle.setImageResource(R.drawable.btn_shuffle_focused);
                btnRepeat.setImageResource(R.drawable.btn_repeat);
            }
        }
    });

}
public void playSong(int songIndex) {
    try {
        Log.e("playSong()...", "....is called");
        mp.reset();
        mp.setDataSource(songsList.get(songIndex).getSongPath());
        mp.prepare();
        songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
        String songTitle = songsList.get(songIndex).getSongTitle();
        songTitleLabel.setText(songTitle);
        btnPlay.setImageResource(R.drawable.btn_pause);
        mp.start();
        updateProgressBar();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    Log.i("song...songIndex...", "..." + songIndex);
    mHandler.removeCallbacks(mUpdateTimeTask);
    if(resultCode == 100){
        currentSongIndex = data.getExtras().getInt("songIndex");
        // play selected song
        playSong(currentSongIndex);
    }

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

        songTotalDurationLabel.setText(""+utils.milliSecondsToTimer(totalDuration));

        songCurrentDurationLabel.setText("" + utils.milliSecondsToTimer(currentDuration));

        int progress =(int)(utils.getProgressPercentage(currentDuration, totalDuration));

        songProgressBar.setProgress(progress);

        mHandler.postDelayed(this, 100);
    }
};
private void updateProgressBar() {
    mHandler.postDelayed(mUpdateTimeTask, 100);
}
@Override
public void onCompletion(MediaPlayer mp) {
    if(isRepeat){
        playSong(currentSongIndex);
    } else if(isShuffle){
        Random rand = new Random();
        currentSongIndex = rand.nextInt((songsList.size() - 1) - 0 + 1) + 0;
        playSong(currentSongIndex);
    } else{
        if(currentSongIndex < (songsList.size() - 1)){
            playSong(currentSongIndex + 1);
            currentSongIndex = currentSongIndex + 1;
        }else{
            playSong(0);
            currentSongIndex = 0;
        }
    }
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

}
public void onStartTrackingTouch(SeekBar seekBar) {
    mHandler.removeCallbacks(mUpdateTimeTask);
}
public void onStopTrackingTouch(SeekBar seekBar) {
    mHandler.removeCallbacks(mUpdateTimeTask);
    int totalDuration = mp.getDuration();
    int currentPosition = utils.progressToTimer(seekBar.getProgress(), totalDuration);
    mp.seekTo(currentPosition);
    updateProgressBar();
}
@Override
public void onDialPositionChanged(WheelModel sender, int nicksChanged) {
    getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
    mp2 =MediaPlayer.create(getActivity(),R.raw.djlazer);
    mp2.start();
}}

如果有人知道这些的逻辑,请帮助我。

提前感谢...

1 个答案:

答案 0 :(得分:0)

用户松开手指时的事件是MotionEvent.ACTION_UP。我不知道是否有任何指导禁止使用View.OnTouchListener而不是onClick(),这很可能取决于具体情况。 这是我的示例工作代码:

{{1}}