我正在尝试播放res / raw / filename.mp3中的音频。我从在线教程中学习并创建了这个代码。它正在播放,暂停,停止而没有任何干扰但是搜索栏不能正常工作。它的移动,但我想通过搜索栏进行前进和后退使用。当我拖动搜索栏时,它会跟随并移动,然后返回到当前的播放时间。我必须解决这个问题,请帮帮我怎么办?提前谢谢。
Java代码:
import java.util.concurrent.TimeUnit;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
public TextView songName,startTimeField,endTimeField;
private MediaPlayer mediaPlayer;
private double startTime = 0;
private double finalTime = 0;
private Handler myHandler = new Handler();;
private int forwardTime = 5000;
private int backwardTime = 5000;
private SeekBar seekbar;
private ImageButton playButton,pauseButton;
public static int oneTimeOnly = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
songName = (TextView)findViewById(R.id.textView4);
startTimeField =(TextView)findViewById(R.id.textView1);
endTimeField =(TextView)findViewById(R.id.textView2);
seekbar = (SeekBar)findViewById(R.id.seekBar1);
playButton = (ImageButton)findViewById(R.id.imageButton5);
pauseButton = (ImageButton)findViewById(R.id.imageButton1);
songName.setText("Thingyan songs.mp3");
mediaPlayer = MediaPlayer.create(this, R.raw.nn);
seekbar.setClickable(false);
pauseButton.setEnabled(false);
}
@SuppressLint("NewApi")
public void play(View view){
Toast.makeText(getApplicationContext(), "Playing sound",
Toast.LENGTH_SHORT).show();
mediaPlayer.start();
finalTime = mediaPlayer.getDuration();
startTime = mediaPlayer.getCurrentPosition();
if(oneTimeOnly == 0){
seekbar.setMax((int) finalTime);
oneTimeOnly = 1;
}
endTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) finalTime),
TimeUnit.MILLISECONDS.toSeconds((long) finalTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) finalTime)))
);
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(UpdateSongTime,100);
pauseButton.setEnabled(true);
playButton.setEnabled(false);
}
private Runnable UpdateSongTime = new Runnable() {
@SuppressLint("NewApi")
public void run() {
startTime = mediaPlayer.getCurrentPosition();
startTimeField.setText(String.format("%d min, %d sec",
TimeUnit.MILLISECONDS.toMinutes((long) startTime),
TimeUnit.MILLISECONDS.toSeconds((long) startTime) -
TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.
toMinutes((long) startTime)))
);
seekbar.setProgress((int)startTime);
myHandler.postDelayed(this, 100);
}
};
public void pause(View view){
Toast.makeText(getApplicationContext(), "Pausing sound",
Toast.LENGTH_SHORT).show();
mediaPlayer.pause();
pauseButton.setEnabled(false);
playButton.setEnabled(true);
}
public void forward(View view){
int temp = (int)startTime;
if((temp+forwardTime)<=finalTime){
startTime = startTime + forwardTime;
mediaPlayer.seekTo((int) startTime);
}
else{
Toast.makeText(getApplicationContext(),
"Cannot jump forward 5 seconds",
Toast.LENGTH_SHORT).show();
}
}
public void rewind(View view){
int temp = (int)startTime;
if((temp-backwardTime)>0){
startTime = startTime - backwardTime;
mediaPlayer.seekTo((int) startTime);
}
else{
Toast.makeText(getApplicationContext(),
"Cannot jump backward 5 seconds",
Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}`
Xml代码:
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" xmlns:android1="http://schemas.android.com/apk/res/android">
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="300dp" />
<ImageButton
android:id="@+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="22dp"
android:layout_x="0dp"
android:layout_y="350dp"
android:onClick="rewind"
android:src="@drawable/leftone" />
<ImageButton
android:id="@+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="14dp"
android:layout_x="250dp"
android:layout_y="350dp"
android:onClick="forward"
android:src="@drawable/rightone" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="160dp"
android:layout_y="18dp"
android:text="TextView" />
<ImageButton
android:id="@+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="165dp"
android:layout_y="350dp"
android:onClick="play"
android:src="@drawable/playfive" />
<ImageButton
android1:id="@+id/imageButton1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_x="85dp"
android1:layout_y="350dp"
android1:onClick="pause"
android1:src="@drawable/pauseone" />
<Button
android1:id="@+id/button1"
android1:layout_width="150dp"
android1:layout_height="wrap_content"
android1:layout_x="0dp"
android1:layout_y="430dp"
android1:text="" />
<TextView
android1:id="@+id/textView1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_x="30dp"
android1:layout_y="445dp"
android1:text="@string/inital_Time"
android1:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android1:id="@+id/button2"
android1:layout_width="150dp"
android1:layout_height="wrap_content"
android1:layout_x="170dp"
android1:layout_y="430dp"
android1:text="" />
<TextView
android1:id="@+id/textView2"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_x="200dp"
android1:layout_y="445dp"
android1:text="@string/inital_Time"
android1:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android1:id="@+id/textView3"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_x="30dp"
android1:layout_y="15dp"
android:textColor="#000000"
android1:text="@string/hello_world"
android1:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android1:id="@+id/imageView1"
android1:layout_width="match_parent"
android1:layout_height="238dp"
android1:layout_x="0dp"
android1:layout_y="62dp"
android1:src="@drawable/water" />
<TextView
android1:id="@+id/textView5"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_x="200dp"
android1:layout_y="475dp"
android1:text="Total Duration" />
<TextView
android1:id="@+id/textView6"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:layout_x="40dp"
android1:layout_y="475dp"
android1:text="Playing Time" />
</AbsoluteLayout>
答案 0 :(得分:0)
您需要SeekBar.OnSeekBarChangeListener
:
YOUR_CLASS implements SeekBar.OnSeekBarChangeListener{
......................
......................
seekbar.setOnSeekBarChangeListener(this); // Important
@Override
public void onProgressChanged(SeekBar seekBar,
int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mHandler.removeCallbacks(UpdateSongTime);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
myHandler.removeCallbacks(UpdateSongTime);
int currentPosition = seekBar.getProgress();
// forward or backward to certain seconds
mp.seekTo(currentPosition);
// update timer progress again
myHandler.postDelayed(UpdateSongTime,100);
}
}