我最近发布了一个问题,询问SeekBars
是如何运作的。有人指示我访问这个网站/教程:http://android-mantra.blogspot.com/2013/09/seekbar-for-music-player.html
它真的帮了我很多帮助我理解了搜索栏是如何工作的,但我仍然有问题。当我点击播放按钮时,音乐将播放,但是当我寻找音乐时,搜索栏将保持在0位置,尽管音乐正在我想要的位置上运行。这是我在教程中基于(复制)的代码。
package com.example.danbrianarenas.musictesterv2;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
public class MainActivity extends Activity implements Runnable,View.OnClickListener,SeekBar.OnSeekBarChangeListener
{
SeekBar seekBar;
Button startMedia;
Button stopMedia;
MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
startMedia = (Button) findViewById(R.id.button1);
stopMedia = (Button) findViewById(R.id.button2);
startMedia.setOnClickListener(this);
stopMedia.setOnClickListener(this);
seekBar.setOnSeekBarChangeListener(this);
seekBar.setEnabled(false);
}
public void run()
{
int currentPosition = mp.getCurrentPosition();
int total = mp.getDuration();
while (mp !=null && currentPosition<total)
{
try
{
Thread.sleep(1000);
} catch (InterruptedException e) {
return;
} catch (Exception e){
return;
}
seekBar.setProgress(currentPosition);
}
}
public void onClick(View view){
if(view.equals(startMedia)){
if(mp == null){
mp=MediaPlayer.create(getApplicationContext(), R.raw.filc2);
seekBar.setEnabled(true);
}
if(mp.isPlaying()){
mp.pause();
startMedia.setText("Play");
}
else{
mp.start();
startMedia.setText("Pause");
seekBar.setMax(mp.getDuration());
new Thread(this).start();
}
}
if (view.equals(stopMedia) && mp!=null){
if (mp.isPlaying() || mp.getDuration()>0){
mp.stop();
mp = null;
startMedia.setText("play");
seekBar.setProgress(0);
}
}
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
try{
if(mp.isPlaying() || mp!=null){
if(fromUser)
mp.seekTo(progress);
}else if (mp==null){
Toast.makeText(getApplicationContext(),"Media is not running!",Toast.LENGTH_SHORT).show();
seekBar.setProgress(0);
}
}catch (Exception e){
Log.e("seek bar", "" + e);
seekBar.setEnabled(false);
}
}
@Override
public void onStartTrackingTouch(SeekBar seekBar){
//TODO Auto-generated method sub
}
@Override
public void onStopTrackingTouch(SeekBar seekBar){
//TODO Auto-generated method sub
}
}
PS:如果您需要XML文件,请告诉我。 :) TIA
答案 0 :(得分:0)
由于您没有在搜索栏上更改搜索栏的位置,因此更改您的代码并更改您的代码,它将起作用:
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser){
try{
if(mp.isPlaying() || mp!=null){
if(fromUser){
seekBar.setProgress(progress); // ************** you have to add this line
mp.seekTo(progress);
}}else if (mp==null){
Toast.makeText(getApplicationContext(),"Media is not running!",Toast.LENGTH_SHORT).show();
seekBar.setProgress(0);
}
}catch (Exception e){
Log.e("seek bar", "" + e);
seekBar.setEnabled(false);
}
}