1
我是Android編程的新手,我一直在開發媒體應用程序,它可以播放,暫停音頻並使用Seekbar來操作音量和媒體當前位置。當我運行該應用程序時,我有在將Seekbar進度值添加到MediaPlayer後關閉音頻。在Android中關閉音頻問題
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) {
}
});
}
謝謝!它現在工作正常 – adarshjaya12
嗨@AdarshJayakumar如果我的答案有助於解決你的問題,你能接受我的答案嗎? – fruqi
完成!無論如何感謝您的回答 – adarshjaya12