一、核心功能
- 获取播放列表
- 播放网络音频
- 进度条及时间
- 切换及暂停
二、功能说明
- 播放列表后台实现
<?php
include("conn.php");
$res = mysqli_query($link, 'select * from mediaList');
$i = 0;
echo '[';
while ($row = mysqli_fetch_assoc($res)) {
if ($i != 0) {
echo ',';
}
echo '{"music":"'.$url.$row['music'].'"}';
$i++;
}
echo ']';
mysqli_close($link);
- Android端显示
- Android端播放
- 暂停
- 划屏及按键可实现切换切换
三、主要代码
- AndroidManifest.xml中,给予访问网络资源权限
<uses-permission android:name="android.permission.INTERNET"/>
- 获取播放列表
public class MediaPlay {
String musicUrl = "";
String[] musicList;
public MediaPlay() {}
public String[] getMusicList() {
HttpURLConnection conn = null;
BufferedReader buf = null;
StringBuilder getRes = new StringBuilder();
String[] res = null;
try {
URL url = new URL(musicUrl);
conn = (HttpURLConnection) url.openConnection();
if (conn.getResponseCode() == 200) {
buf = new BufferedReader(new InputStreamReader(conn.getInputStream()));
for (String s = buf.readLine(); s != null; s = buf.readLine()) {
getRes.append(s);
}
JSONArray jsonArray = new JSONArray(getRes.toString());
List<String> list = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
list.add(jsonArray.getJSONObject(i).getString("music"));
}
res = list.toArray(new String[list.size()]);
buf.close();
}
} catch (Exception e) {}
musicList = res;
return res;
}
public String[] getMusicNameList(String[] musicList) {
int len = musicList.length;
String[] musicNameList = new String[len];
for (int i = 0; i < len; i++) {
String str = musicList[i];
musicNameList[i] = str.substring(str.lastIndexOf("/")+1);
}
return musicNameList;
}
}
- 主界面显示页
ListView mediaList;
String[] playList;
MediaPlay mp = new MediaPlay();
Bundle bundle = new Bundle();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mediaPlay();
}
public void waitForRes() {
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().penaltyLog().build());
}
public void mediaPlay() {
mediaList = findViewById(R.id.mediaList);
waitForRes();
playList = mp.getMusicList();
try {
String[] musicNameList = mp.getMusicNameList(playList);
mediaList.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, musicNameList));
mediaList.setOnItemClickListener(new MusicChoice());
} catch (Exception e) {}
}
class MusicChoice implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(HelperActivity.this, MediaActivity.class);
bundle.putString("index",String.valueOf(i));
bundle.putStringArray("playList",playList);
intent.putExtras(bundle);
startActivity(intent);
}
}
- 播放音频页
组件 | ID |
---|
ConstraintLayout | mediaActivity | | |
TextView | musicName | playTime | |
SeekBar | seekBar | | |
ImageButton | playBtn | nextBtn | prevBtn |
ConstraintLayout mediaActivity;
int oldY, newY, oldX, newX, thresholdX = 700, thresholdY = 700;
MediaPlay mp = new MediaPlay();
MediaPlayer player = new MediaPlayer();
int index;
String[] playList, mn;
TextView musicName, playTime;
SeekBar seekBar;
ImageButton playBtn, nextBtn, prevBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_media);
Toast.makeText(MediaActivity.this, "左划下一首\n右划下一首\n上划退出", Toast.LENGTH_LONG).show();
mediaActivity = findViewById(R.id.mediaActivity);
mediaActivity.setOnTouchListener(new OnTouch());
play();
}
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
int msec = player.getCurrentPosition(), sec = msec/1000%60, min = msec/1000/60,
totalSec = player.getDuration()/1000, totalMin = totalSec/60;
seekBar.setProgress(msec);
playTime.setText(min+"m"+sec+"s/"+totalMin+"m"+(totalSec%60)+"s");
handler.postDelayed(runnable, 100);
}
};
public void play() {
Bundle getMusicInfo = this.getIntent().getExtras();
index = Integer.parseInt(getMusicInfo.getString("index"));
playList = getMusicInfo.getStringArray("playList");
musicName = findViewById(R.id.musicName);
mn = mp.getMusicNameList(playList);
seekBar = findViewById(R.id.seekBar);
playTime = findViewById(R.id.playTime);
playBtn = findViewById(R.id.playBtn);
playBtn.setOnClickListener(new OnPlayClick());
nextBtn = findViewById(R.id.nextBtn);
nextBtn.setOnClickListener(new OnPlayClick());
prevBtn = findViewById(R.id.prevBtn);
prevBtn.setOnClickListener(new OnPlayClick());
setPlayer();
}
public void resetPlay() {
if (player.isPlaying()) {
player.stop();
player.reset();
handler.removeCallbacks(runnable);
}
}
public void setPlayer() {
resetPlay();
musicName.setText(mn[index]);
try {
player.setDataSource(playList[index]);
player.prepare();
player.start();
seekBar.setMax(player.getDuration());
handler.post(runnable);
} catch (IOException e) {
e.printStackTrace();
}
}
public void nextPlay() {
index = (index + 1) % playList.length;
setPlayer();
}
public void prevPlay() {
index = (index - 1 + playList.length) % playList.length;
setPlayer();
}
class OnTouch implements View.OnTouchListener {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
oldY = (int) motionEvent.getY();
oldX = (int) motionEvent.getX();
return true;
}
if (motionEvent.getAction() == MotionEvent.ACTION_UP) {
newY = (int) motionEvent.getY();
newX = (int) motionEvent.getX();
if (oldY - newY > thresholdY) {
resetPlay();
startActivity(new Intent(MediaActivity.this, MainActivity.class));
return true;
}
if (oldX - newX > thresholdX) {
nextPlay();
return true;
}
if (newX - oldX > thresholdX) {
prevPlay();
return true;
}
return true;
}
return false;
}
}
class OnPlayClick implements View.OnClickListener {
@Override
public void onClick(View view) {
if (view == playBtn) {
try {
if (player.isPlaying()) {
player.pause();
} else {
player.start();
handler.post(runnable);
}
} catch (Exception e) {}
} else if (view == nextBtn) {
nextPlay();
} else if (view == prevBtn) {
prevPlay();
}
}
}