各位前辈。
最近学习做简单的应用。
想知道如何在网上阅读音乐。
如何增加搜索栏显示进度?
然后增加当前播放时间和总时间的显示。
我的代码:
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();
}
}
});
}
}
答案 0 :(得分:0)
我建议你看一下this link。这是一个非常详细的教程,介绍如何更新搜索栏并相应地保留时间。
虽然它是从本地播放音频,但您始终可以将音频文件的网址传递给 MusicPlayer 类。
希望这适合你!