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协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

基于Redis自动过期的流处理暂停机制

《基于Redis自动过期的流处理暂停机制》基于Redis自动过期的流处理暂停机制是一种高效、可靠且易于实现的解决方案,防止延时过大的数据影响实时处理自动恢复处理,以避免积压的数据影响实时性,下面就来详... 目录核心思路代码实现1. 初始化Redis连接和键前缀2. 接收数据时检查暂停状态3. 检测到延时过

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

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

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