1.编写主页面,使用listview组件放置音乐列表信息
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
android:background="#D8D8D8">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="@id/local"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="20dp">
<com.example.myapplication.MyListVIew
android:id="@+id/video_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/local"
android:background="#ECC8C8"
android:layout_alignParentBottom="true">
<ImageView
android:id="@+id/img"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/bold"
android:layout_marginLeft="10dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="告白气球"
android:layout_toRightOf="@id/img"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:textColor="#000"
android:textSize="15sp"/>
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="周杰伦"
android:layout_toRightOf="@id/img"
android:layout_marginLeft="20dp"
android:layout_below="@id/song"
android:textColor="#000"
android:textSize="12sp"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_centerHorizontal="true">
<SeekBar
android:id="@+id/time"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:text="3:30"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:id="@+id/t2"
android:layout_marginRight="20dp"/>
</LinearLayout>
<TextView
android:id="@+id/next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="下一曲"
android:textSize="12sp"
android:textColor="#000"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"/>
<TextView
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:textSize="12sp"
android:textColor="#000"
android:layout_toLeftOf="@id/next"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"/>
<TextView
android:id="@+id/up"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="上一曲"
android:textSize="12sp"
android:textColor="#000"
android:layout_toLeftOf="@id/stop"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"/>
</RelativeLayout>
</RelativeLayout>
2.写Bean类、Adapter适配器
package com.example.myapplication;
import java.io.Serializable;
public class VideoBean implements Serializable {
private int id;
private String song;
private String singer;
private String album;
private String time;
private int path;
public VideoBean(int id, String song, String singer, String album, String time, int path) {
this.id = id;
this.song = song;
this.singer = singer;
this.album = album;
this.time = time;
this.path = path;
}
public String getTime() {
return time;
}
public String getAlbum() {
return album;
}
public int getId() {
return id;
}
public int getPath() {
return path;
}
public String getSinger() {
return singer;
}
public String getSong() {
return song;
}
}
package com.example.myapplication;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;
public class VideoAdapter extends BaseAdapter {
private List<VideoBean> list;
private Context context;
public VideoAdapter(List<VideoBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
convertView= LayoutInflater.from(context).inflate(R.layout.adaper_video,parent,false);
}
VideoBean bean=list.get(position);
TextView id=convertView.findViewById(R.id.id);
TextView song=convertView.findViewById(R.id.song);
TextView singer=convertView.findViewById(R.id.singer);
TextView album=convertView.findViewById(R.id.album);
TextView time=convertView.findViewById(R.id.time);
id.setText(bean.getId()+"");
song.setText(bean.getSong());
singer.setText(bean.getSinger());
album.setText(bean.getAlbum());
time.setText(bean.getTime());
return convertView;
}
}
3.写listview的子布局页面
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="@drawable/shape10"
android:orientation="horizontal"
android:paddingLeft="30dp"
android:paddingRight="30dp">
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="35sp"
android:textColor="#000"
android:layout_centerVertical="true"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
>
<TextView
android:id="@+id/song"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="告白气球"
android:textSize="25sp"
android:textColor="#000"
android:layout_gravity="center"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<TextView
android:id="@+id/singer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="许嵩"
android:textSize="20sp"
android:textColor="#000"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="|"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"/>
<TextView
android:id="@+id/album"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="专辑名称"
android:textSize="20sp"
android:textColor="#000"
android:layout_marginLeft="10dp"
android:layout_gravity="center"/>
</LinearLayout>
</LinearLayout>
<TextView
android:id="@+id/time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:layout_alignParentRight="true"
android:text="4:20"
android:textColor="#000"
android:layout_alignParentBottom="true"
android:layout_marginBottom="15dp"/>
</RelativeLayout>
</LinearLayout>
下载几个mp3音乐导入raw中
4.编写主页面java文件
package com.example.myapplication;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.media.MediaParser;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
import com.bm.library.PhotoView;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private TextView song,name,next,stop,up,t1,t2;
private ListView video_list;
private List<VideoBean> data;
private MediaPlayer mediaPlayer;
private int id=0;
private SeekBar time;
private Handler mHandler = new Handler();
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
if (mediaPlayer != null && mediaPlayer.isPlaying()) {
int mCurrentPosition = mediaPlayer.getCurrentPosition(); // 获取当前播放位置
//总时长
int duration = mediaPlayer.getDuration();
// 计算剩余时长
int timeLeft = duration - mCurrentPosition;
int minutes = timeLeft / 1000 / 60;
int seconds = (timeLeft / 1000) % 60;
t2.setText(String.format(Locale.getDefault(), "%02d:%02d", minutes, seconds));
time.setProgress(mCurrentPosition);
mHandler.postDelayed(this, 1000);
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
data=new ArrayList<>();
data.add(new VideoBean(1,"七月上","Jam","七月上","3:10",R.raw.a1));
data.add(new VideoBean(2,"大摇大摆迎春来","大张伟","七月上","2:54",R.raw.a2));
data.add(new VideoBean(3,"抱歉","科德夏萍","陈慧琳","3:56",R.raw.a3));
data.add(new VideoBean(4,"白色恋人","游鸿铭","游鸿铭","4:48",R.raw.a4));
video_list.setAdapter(new VideoAdapter(data,MainActivity.this));
video_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
VideoBean bean=data.get(position);
song.setText(bean.getSong());
name.setText(bean.getSinger());
isplaying();
mediaPlayer=MediaPlayer.create(MainActivity.this,bean.getPath());
mediaPlayer.start();
t2.setText("00:00");
time.setMax(mediaPlayer.getDuration());
mHandler.post(mRunnable);
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mediaPlayer!=null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause();
stop.setText("开始");
mHandler.post(mRunnable);
} else {
stop.setText("暂停");
mediaPlayer.start();
mHandler.post(mRunnable);
}
}else {
Toast.makeText(MainActivity.this, "暂未播放音乐", Toast.LENGTH_SHORT).show();
}
}
});
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
isplaying();
id = (id % data.size()) + 1;
if (id >= data.size()) {
id = 0;
}
VideoBean bean = data.get(id);
song.setText(bean.getSong());
name.setText(bean.getSinger());
mediaPlayer = MediaPlayer.create(MainActivity.this, bean.getPath());
mediaPlayer.start();
time.setMax(mediaPlayer.getDuration());
mHandler.post(mRunnable);
}
});
up.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
isplaying();
id = id-1;
// 如果索引超出了列表大小,将其重置为0(列表的第一首歌曲)
if (id <0) {
id = data.size() - 1;
}
VideoBean bean = data.get(id);
song.setText(bean.getSong());
name.setText(bean.getSinger());
mediaPlayer = MediaPlayer.create(MainActivity.this, bean.getPath());
mediaPlayer.start();
time.setMax(mediaPlayer.getDuration());
mHandler.post(mRunnable);
}
});
time.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mediaPlayer != null && fromUser) {
mediaPlayer.seekTo(progress);
}
}
public void onStartTrackingTouch(SeekBar seekBar) {
time.removeCallbacks(mRunnable);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
time.postDelayed(mRunnable, 1000);
}
});
}
private void init(){
song=findViewById(R.id.song);
name=findViewById(R.id.name);
next=findViewById(R.id.next);
stop=findViewById(R.id.stop);
up=findViewById(R.id.up);
time=findViewById(R.id.time);
video_list=findViewById(R.id.video_list);
t2=findViewById(R.id.t2);
}
private void isplaying(){
if (mediaPlayer!= null) {
if (mediaPlayer.isPlaying()) {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
mHandler.removeCallbacks(mRunnable);
stop.setText("暂停");
}
}
}
文章来源地址https://www.toymoban.com/news/detail-838788.html
文章来源:https://www.toymoban.com/news/detail-838788.html
到了这里,关于Android 音乐播放器(暂停、下一首、上一首、进度条、拉动进度条)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!