Android 音乐播放器(暂停、下一首、上一首、进度条、拉动进度条)

2024-03-05 13:52

本文主要是介绍Android 音乐播放器(暂停、下一首、上一首、进度条、拉动进度条),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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"><ScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_above="@id/local"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="20dp"><com.example.myapplication.MyListVIewandroid:id="@+id/video_list"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout></ScrollView><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="100dp"android:id="@+id/local"android:background="#ECC8C8"android:layout_alignParentBottom="true"><ImageViewandroid:id="@+id/img"android:layout_width="40dp"android:layout_height="40dp"android:src="@drawable/bold"android:layout_marginLeft="10dp"android:layout_centerVertical="true"/><TextViewandroid: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"/><TextViewandroid: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"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_centerHorizontal="true"><SeekBarandroid:id="@+id/time"android:layout_width="300dp"android:layout_height="wrap_content"android:text="3:30"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="15sp"android:id="@+id/t2"android:layout_marginRight="20dp"/></LinearLayout><TextViewandroid: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"/><TextViewandroid: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"/><TextViewandroid: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;}@Overridepublic int getCount() {return list.size();}@Overridepublic Object getItem(int position) {return position;}@Overridepublic long getItemId(int position) {return position;}@Overridepublic 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"><RelativeLayoutandroid:layout_width="match_parent"android:layout_height="100dp"android:background="@drawable/shape10"android:orientation="horizontal"android:paddingLeft="30dp"android:paddingRight="30dp"><TextViewandroid: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"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"android:layout_centerVertical="true"android:layout_marginLeft="50dp"><TextViewandroid: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"/><LinearLayoutandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="horizontal"android:layout_marginTop="10dp"><TextViewandroid: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"/><TextViewandroid: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"/><TextViewandroid: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><TextViewandroid: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() {@Overridepublic 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);}}};@Overrideprotected 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic 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() {@Overridepublic void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {if (mediaPlayer != null && fromUser) {mediaPlayer.seekTo(progress);}}public void onStartTrackingTouch(SeekBar seekBar) {time.removeCallbacks(mRunnable);}@Overridepublic 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("暂停");}}
}

这篇关于Android 音乐播放器(暂停、下一首、上一首、进度条、拉动进度条)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


原文地址:
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.chinasem.cn/article/776722

相关文章

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络