我有一个简单的玩家。 要在SeekBar和MediaPlayer之间创建一个“连接”,我在任何播放的地方触摸了搜索栏,并与播放器同步搜索栏。在您看来,问题出在哪里? 我该怎么办? 如果你有一个这方面的例子分享它
我的代码:
public class MediaPlaybackActivity extends Activity implements OnClickListener {
SeekBar seek_bar;
Button play_button, pause_button;
MediaPlayer player;
Handler seekHandler = new Handler();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
getInit();
seekUpdation();
}
public void getInit() {
seek_bar = (SeekBar) findViewById(R.id.seek_bar);
play_button = (Button) findViewById(R.id.play_button);
pause_button = (Button) findViewById(R.id.pause_button);
play_button.setOnClickListener(this);
pause_button.setOnClickListener(this);
player = MediaPlayer.create(this, R.raw.win8);
seek_bar.setMax(player.getDuration());
seek_bar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seek_bar) {
}
@Override
public void onStartTrackingTouch(SeekBar seek_bar) {
}
@Override
public void onProgressChanged(SeekBar seek_bar, int progress, boolean fromUser) {
if(player != null && fromUser){
player.seekTo(progress * 1000);
}
}
});
}
Runnable run = new Runnable() {
@Override
public void run() {
seekUpdation();
}
};
public void seekUpdation() {
seek_bar.setProgress(player.getCurrentPosition());
seekHandler.postDelayed(run, 1000);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play_button:
player.start();
break;
case R.id.pause_button:
player.pause();
}
}}
答案 0 :(得分:-1)
使用以下方法
AudioManager audioManager;
private int maxVolume;
private int curVolume;
/**
* To initialize the seek bar
*/
private void initialiseSeekBar() {
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
mSeekBarVolume.setMax(maxVolume);
mSeekBarVolume.setProgress(curVolume);
mSeekBarVolume
.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar arg0) {
}
@Override
public void onStartTrackingTouch(SeekBar arg0) {
}
@Override
public void onProgressChanged(SeekBar arg0, int arg1,
boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC,
arg1, 0);
}
});
}