关闭Android

时间:2016-01-11 01:25:38

标签: java android

我是Android编程的新手,我一直在开发一个可以播放,暂停音频和使用Seekbar来操作音量和媒体当前位置的媒体应用程序。当我运行应用程序时,我在添加后关闭了音频Seekbar进度值为MediaPlayer。

import android.content.Context;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.MediaController;
import android.widget.SeekBar;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

MediaPlayer mediaPlayer;
AudioManager audioManager;
boolean isPlaying;
public void songPlay(View view)
{
    if(isPlaying) {
        mediaPlayer.start();
        isPlaying = false;

    }
}
public void songPause(View view)
{
    mediaPlayer.pause();
    isPlaying = true;
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mediaPlayer = MediaPlayer.create(this,R.raw.nfy);
    isPlaying = true;
    audioManager= (AudioManager) getSystemService(Context.AUDIO_SERVICE);
    int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
    int currentVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);

    SeekBar seekBar = (SeekBar) findViewById(R.id.volumeBar);
    seekBar.setMax(maxVolume);
    seekBar.setProgress(currentVolume);
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });


    final SeekBar scrubber = (SeekBar) findViewById(R.id.playBar);
    scrubber.setMax(mediaPlayer.getDuration());
    new Timer().scheduleAtFixedRate(new TimerTask() {
        @Override
        public void run() {
            scrubber.setProgress(mediaPlayer.getCurrentPosition());
        }
    },0,1000);

    scrubber.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(mediaPlayer!=null && fromUser)
            mediaPlayer.seekTo(progress);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

1 个答案:

答案 0 :(得分:0)

几乎在那里,尝试用以下内容替换您的onProgressChanged

@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
            if(fromUser) {
              mediaPlayer.seekTo(progress);
            }
        }