0
會員前輩。
最近學會做簡單的應用程序。
想問如何在網上閱讀音樂。
如何增加seekbar顯示進度?
然後增加當前播放時間和總時間的顯示。增加seekbar音樂播放顯示時間更新
我的代碼:
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import static tw.edu.ctust.f10308023.mismediaplayer.R.id.btnBackward;
import static tw.edu.ctust.f10308023.mismediaplayer.R.id.btnPlay;
public class Player_one extends AppCompatActivity {
private ImageButton ib1, ib2, ib3;
ImageView iv;
private MediaPlayer mediaPlayer;
private Handler myHandler = new Handler();
private int forwardTime = 5000;
private int backwardTime = 5000;
private double startTime = 0;
private double finalTime = 0;
private TextView tx1, tx2, tx3;
private int seekForwardTime = 5000; // 5000 milliseconds
private int seekBackwardTime = 5000; // 5000 milliseconds
String AudioURL = "http://k003.kiwi6.com/hotlink/hyzvh0ugcl/cytussaika.mp3";
MediaPlayer mediaplayer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_player_one);
iv = (ImageView) findViewById(R.id.imageView);
ib1 = (ImageButton) findViewById(btnPlay);//播放
ib2 = (ImageButton) findViewById(btnBackward);//倒轉
ib3 = (ImageButton) findViewById(R.id.btnForward);//快轉
tx1 = (TextView) findViewById(R.id.songCurrentDurationLabel);
tx2 = (TextView) findViewById(R.id.songTotalDurationLabel);
tx3 = (TextView) findViewById(R.id.songTitle);
tx3.setText("Cytus-saika");
View v = findViewById(R.id.player_footer_bg);
v.getBackground().setAlpha(75);//0~255透明度值
mediaplayer = new MediaPlayer();
mediaplayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
ib1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated catch block
if (mediaplayer.isPlaying()) {
mediaplayer.pause();
ib1.setImageResource(R.drawable.play);
} else {
mediaplayer.start();
ib1.setImageResource(R.drawable.pause);
}
try {
mediaplayer.setDataSource(AudioURL);
mediaplayer.prepare();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
ib2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// get current song position
int currentPosition =mediaplayer.getCurrentPosition();
// check if seekBackward time is greater than 0 sec
if(currentPosition - seekBackwardTime >= 0){
// forward song
mediaplayer.seekTo(currentPosition - seekBackwardTime);
}else{
// backward to starting position
mediaplayer.seekTo(0);
}
}
});
ib3.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick (View arg0){
// get current song position
int currentPosition = mediaplayer.getCurrentPosition();
// check if seekForward time is lesser than song duration
if (currentPosition + seekForwardTime <= mediaplayer.getDuration()) {
// forward song
mediaplayer.seekTo(currentPosition + seekForwardTime);
} else {
// forward to end position
mediaplayer.seekTo(mediaplayer.getDuration());
Toast.makeText(getApplicationContext(), "無法快轉", Toast.LENGTH_SHORT).show();
}
}
});
}
}