我正在尝试使用搜索栏监听器播放一首歌。但是当我尝试拖动进度条时,它正在从歌曲的开头播放。它需要从拖拽的位置发挥作用。
这是我的代码:
import java.util.Scanner;
public class cCode {
public static void main(String args[]) {
System.out.println("Enter country code\nChoices: IND, USA, JPN, NZ, WI");
Scanner cc = new Scanner(System.in);
String txt = cc.nextLine();
switch (txt) {
case "IND":
System.out.println(txt + " refers to INDIA");
break;
case "USA":
System.out.println(txt + " refers to UNITED STATES");
break;
case "JPN":
System.out.println(txt + " refers to JAPAN");
break;
case "NZ":
System.out.println(txt + " refers to NEW ZEALAND");
break;
case "WI":
System.out.println(txt + " refers to WEST INDIES");
break;
default:
System.out.println("Invalid choice");
}
}}
如何解决此问题。提前谢谢..
答案 0 :(得分:4)
我在这里做了一些技巧:
将MediaPlayer实例移至init()方法
更改了此行
int mCurrentPosition = mMediaPlayer.getCurrentPosition()/ 1000;
为:
int mCurrentPosition = mp.getCurrentPosition();
mp.seekTo(进度* 1000);
为:
mp.seekTo(进度);
最后工作代码将是:
public class MainActivity extends Activity {
private TextView prairText;
private AssetFileDescriptor descriptor;
private SeekBar seekBar;
private MediaPlayer mp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Keep screen active
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
init();
// Play song
if (mp != null) {
mp.start();
}
final Handler mHandler = new Handler();
//Make sure you update Seek bar on UI thread
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
if(mp != null){
int mCurrentPosition = mp.getCurrentPosition();
seekBar.setProgress(mCurrentPosition);
}
mHandler.postDelayed(this, 1000);
}
});
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mp != null && fromUser) {
mp.seekTo(progress);
mp.start();
findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
}
}
});
findViewById(R.id.play_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null) {
mp.start();
findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
}
}
});
findViewById(R.id.pauseButton).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null) {
mp.pause();
findViewById(R.id.pauseButton).setVisibility(View.INVISIBLE);
findViewById(R.id.play_button).setVisibility(View.VISIBLE);
}
mp.pause();
}
});
findViewById(R.id.restart_button).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (mp != null) {
mp.seekTo(0);
mp.start();
findViewById(R.id.play_button).setVisibility(View.INVISIBLE);
findViewById(R.id.pauseButton).setVisibility(View.VISIBLE);
}
}
});
private void init() {
mp = new MediaPlayer();
mp.reset();
prairText = (TextView) findViewById(R.id.prairText);
seekBar = (SeekBar) findViewById(R.id.seekBar1);
try {
descriptor = getAssets().openFd("mysong.mp3");
mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
descriptor.close();
mp.prepare();
} catch (Exception e) {
e.printStackTrace();
}
int duration = mp.getDuration();
seekBar.setMax(duration);
}
}
注意:不要忘记将mysong.mp3放入assets
文件夹