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、动态请求权限2、悬浮窗权限说明3、检查动态权限4、申请动态权限5、权限设置完毕后

Android里面的Service种类以及启动方式

《Android里面的Service种类以及启动方式》Android中的Service分为前台服务和后台服务,前台服务需要亮身份牌并显示通知,后台服务则有启动方式选择,包括startService和b... 目录一句话总结:一、Service 的两种类型:1. 前台服务(必须亮身份牌)2. 后台服务(偷偷干

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Python中实现进度条的多种方法总结

《Python中实现进度条的多种方法总结》在Python编程中,进度条是一个非常有用的功能,它能让用户直观地了解任务的进度,提升用户体验,本文将介绍几种在Python中实现进度条的常用方法,并通过代码... 目录一、简单的打印方式二、使用tqdm库三、使用alive-progress库四、使用progres

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

流媒体平台/视频监控/安防视频汇聚EasyCVR播放暂停后视频画面黑屏是什么原因?

视频智能分析/视频监控/安防监控综合管理系统EasyCVR视频汇聚融合平台,是TSINGSEE青犀视频垂直深耕音视频流媒体技术、AI智能技术领域的杰出成果。该平台以其强大的视频处理、汇聚与融合能力,在构建全栈视频监控系统中展现出了独特的优势。视频监控管理系统EasyCVR平台内置了强大的视频解码、转码、压缩等技术,能够处理多种视频流格式,并以多种格式(RTMP、RTSP、HTTP-FLV、WebS

Android实现任意版本设置默认的锁屏壁纸和桌面壁纸(两张壁纸可不一致)

客户有些需求需要设置默认壁纸和锁屏壁纸  在默认情况下 这两个壁纸是相同的  如果需要默认的锁屏壁纸和桌面壁纸不一样 需要额外修改 Android13实现 替换默认桌面壁纸: 将图片文件替换frameworks/base/core/res/res/drawable-nodpi/default_wallpaper.*  (注意不能是bmp格式) 替换默认锁屏壁纸: 将图片资源放入vendo

Android平台播放RTSP流的几种方案探究(VLC VS ExoPlayer VS SmartPlayer)

技术背景 好多开发者需要遴选Android平台RTSP直播播放器的时候,不知道如何选的好,本文针对常用的方案,做个大概的说明: 1. 使用VLC for Android VLC Media Player(VLC多媒体播放器),最初命名为VideoLAN客户端,是VideoLAN品牌产品,是VideoLAN计划的多媒体播放器。它支持众多音频与视频解码器及文件格式,并支持DVD影音光盘,VCD影

android-opencv-jni

//------------------start opencv--------------------@Override public void onResume(){ super.onResume(); //通过OpenCV引擎服务加载并初始化OpenCV类库,所谓OpenCV引擎服务即是 //OpenCV_2.4.3.2_Manager_2.4_*.apk程序包,存