Android小玩意儿-- 从头开发一个正经的MusicPlayer(一)

2023-12-09 21:30

本文主要是介绍Android小玩意儿-- 从头开发一个正经的MusicPlayer(一),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

之前从未接触过音乐播放器这块东西的开发。今天偶然想做一个自己的音乐播放器。算是练练手。既然要做,就要做一个正儿八经的App。很多网上的资料也是模模糊糊,不是很全,现在开始,自己摸索着尝试着一步一步的做一个看看。中途一定会有很多的Bug让人头痛,但是,有了这些除Bug的经验,以后才能走的更顺,学的也才会更多。现在废话少说,先来干活儿。

1·获取手机中的音乐资源

首先不做别的,先看看怎么获取手机中的音乐资源。没有资源,后面做出来的也都是空架子。
Android系统中,我们要获取音乐资源有一个很好的类供我们使用,就是MediaStore,用官方的解释这个类就是:
The Media provider contains meta data for all available media on both internal and external storage devices.
这句话说得明明白白,所有的可用的多媒体资源,包括多媒体数据库的所有信息,包括音频,视频和图像,不论是在手机设备的内部存储空间还是外部存储空间,你都可以用MediaStore来获取他!android把所有的多媒体数据库接口进行了封装,所有的数据库不用自己进行创建,直接调用利用ContentResolver去掉用那些封装好的接口就可以进行数据库的操作了!
有个这个类,获取手机中有关音乐的数据就方便多了。

首先,要得到一个ContentResolver实例,ContentResolver可以这样获取,利用一个Activity或者Service的Context即可。如下所示:
ContentResolver mResolver = ctx.getContentResolver();
上面的那个ctx的就是一个context,Activity.this就是那个Context,这个Context就相当于一个上下文环境。得到这个Context后就可以调用getContentResolver接口获取ContentResolver实例了。
ContentResolver实例获得后,就可以进行各种查询,我们要获得的是音频数据,音频数据增删改查的方法,视频和图像和音频非常类似。
下面开始讲述怎么在这些表上进行增删改查。
查询,代码如下所示:
Cursor cursor = resolver.query(_uri, prjs, selections, selectArgs, order);

ContentResolver的query方法接受几个参数,参数意义如下:

Uri:这个Uri代表要查询的数据库名称加上表的名称。这个Uri一般都直接从MediaStore里取得,例如我要取所有歌的信息,就必须利用MediaStore.Audio.Media. EXTERNAL _CONTENT_URI这个Uri。专辑信息要利用MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI这个Uri来查询,其他查询也都类似。

Prjs:这个参数代表要从表中选择的列,用一个String数组来表示。

Selections:相当于SQL语句中的where子句,就是代表你的查询条件。

selectArgs:这个参数是说你的Selections里有?这个符号是,这里可以以实际值代替这个问号。如果Selections这个没有?的话,那么这个String数组可以为null。

Order:说明查询结果按什么来排序。

上面就是各个参数的意义,它返回的查询结果一个Cursor,这个Cursor就相当于数据库查询的中Result,用法和它差不多。

增加,代码如下:

   ContentValues values = new ContentValues();values.put(MediaStore.Audio.Playlists.Members.PLAY_ORDER,0);resolver.insert(_uri, values);

  这个insert传递的参数只有两个,一个是Uri(同查询那个Uri),另一个是ContentValues。这个ContentValuses对应于数据库的一行数据,只要用put方法把每个列的设置好之后,直接利用insert方法去插入就好了。

  更新,代码如下:

    ContentResolver resolver = ctx.getContentResolver();Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;ContentValues values = new ContentValues();values.put(MediaStore.Audio.Media.DATE_MODIFIED, sid);resolver.update(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,values, where, selectionArgs);

上面update方法和查询还有增加里的参数都很类似,这里就不再重复叙述了,大家也可直接参考google的文档,那里也写的很清楚。

  删除,代码如下:

    ContentResolver resolver = ctx.getContentResolver();

   resolver.delete(MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI,where, selectionArgs);

有了这个基础,那么就可以操纵数据库的音乐数据了。
MainAcitvity.java获取数据库数据部分的代码

    Cursor mMusicCursor =this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,null, null,null, MediaStore.Audio.AudioColumns.TITLE);int indexTitle = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);int indexArtist = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);int indexTotalTime = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION);int indexPath = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);for(mMusicCursor.moveToFirst(); !mMusicCursor.isAfterLast(); mMusicCursor.moveToNext()) {String strTitle = mMusicCursor.getString(indexTitle);String strArtist = mMusicCursor.getString(indexArtist);String strTotalTime = mMusicCursor.getString(indexTotalTime);String strPath = mMusicCursor.getString(indexPath);}

这样就从数据库中拿出来了每一音频文件的信息

2·把信息进行封装

信息取出来了下一步就是要封装到一个对象里,并把每一个对象装到一个容器类中,方便我们使用。
首先要思考的是:用什么样的类来封装数据? 这个由封装的对象来决定。我们要封装的是Music,它有歌曲名,歌手名,歌曲时间,歌曲的路径地址。OK.可以进行封装了。
Music.java
package com.zharma.data;

    public class Music {private String musicName;private String musicArtist;private String musicPath;private String musicDuration;public Music(String musicName, String musicArtist, String musicPath, String musicDuration) {this.musicName = musicName;this.musicArtist = musicArtist;this.musicPath = musicPath;this.musicDuration = musicDuration;}public String getMusicName() {return musicName;}public String getMusicArtist() {return musicArtist;}public String getMusicPath() {return musicPath;}public String getMusicDuration() {return musicDuration;}}

有了封装对象的类,下面就做一个容器专门来装这些Music的对象。
MusicList.java
package com.zharma.data;

    import java.util.ArrayList;public class MusicList {private static ArrayList<Music> musicArray = new ArrayList<Music>();private MusicList() {}public static ArrayList<Music> getMusicList() {return musicArray;}}

这里我们把封装Music对象的列表ArrayList做成static的静态成员变量,这样他就成了这个类的公用变量,在第一次使用的时候就被初始化,也就是说对于这个MusicList类的所有new出来的对象,这个musicArray 被共享并且只有一份。

有了这两个类,下面就可以把音乐数据封装起来了。
用MianActivity.java的一个类来封装这段代码。

       private void initMusicList() {musicArrayList = MusicList.getMusicList();if(musicArrayList.isEmpty()){Cursor mMusicCursor = this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,MediaStore.Audio.AudioColumns.TITLE);int indexTitle = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);int indexArtist = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);int indexTotalTime = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION);int indexPath = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);for (mMusicCursor.moveToFirst(); !mMusicCursor.isAfterLast(); mMusicCursor.moveToNext()) { String strTitle = mMusicCursor.getString(indexTitle);String strArtist = mMusicCursor.getString(indexArtist);String strTotoalTime = mMusicCursor.getString(indexTotalTime);String strPath = mMusicCursor.getString(indexPath);if (strArtist.equals("<unknown>"))strArtist = "艺术家";Music music = new Music(strTitle, strArtist, strPath, strTotoalTime);musicArrayList.add(music);}}}

到这里,就封装好了。数据库的音乐数据已经装进了我们的容器里。
接下来要做的就是把容器里的数据使用Adapter来逐一显示到Activity中。

3·使用Adapter连接后端数据和前端显示

先来介绍一下Adapter
Adapter
Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。在常见的View(ListView,GridView)等地方都需要用到Adapter。如下图直观的表达了Data、Adapter、View三者的关系:
121815506135900.jpg

我们的数据就是靠Adapter来显示到UI上的。
下面用SimpleAdapter来显示数据。
这段代码也封装在MainActivity.java的一个方法体里。

    private void initListView() {SimpleAdapter simpleAdapter;//构造一个列表来装Map对象,用map将music对象封装成键-值对List<Map<String, String>> list_map = new ArrayList<Map<String, String>>();HashMap<String, String> map;//for(Music music : musicArrayList) {map = new HashMap<String, String>();map.put("musicName", music.getMusicName());map.put("musicArtist", music.getMusicArtist());list_map.add(map);}String[] from = new String[] {"musicName", "musicArtist"};int[] to = { R.id.listview_tv_title_item, R.id.listview_tv_artist_item };simpleAdapter = new SimpleAdapter(this, list_map, R.layout.listview,from, to);listView.setAdapter(simpleAdapter);}

关于SimpleAdapter的构造函数的描述:
public SimpleAdapter (Context context, List<? extends Map<String, ?>>** data, int resource, String[] from, int[] to**)

Added in API level 1
Constructor

Parameters
context The context where the View associated with this SimpleAdapter is running
data A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
resource Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
from A list of column names that will be added to the Map associated with each item.
to The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.

listview.xml布局文件:

       <?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content" android:orientation="vertical"><TextView android:id="@+id/listview_tv_title_item"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="5dp"android:textSize="18sp"/><TextView android:id="@+id/listview_tv_artist_item"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginLeft="5dp"android:layout_marginBottom="5dp"android:textSize="16sp"/></LinearLayout>

activity_main.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>

         <LinearLayoutandroid:id="@+id/main_volumeLayout"android:layout_width="fill_parent"android:layout_height="wrap_content"><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="horizontal" ></LinearLayout><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:gravity="center"android:orientation="horizontal" ><TextViewandroid:id="@+id/main_tv_volumeText"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_weight="1"android:text="音量 :100%"android:textColor="#ffffffff"android:textSize="15dp" /><SeekBarandroid:id="@+id/main_sb_volumebar"android:layout_width="82dp"android:layout_height="wrap_content"android:maxHeight="5dip"android:minHeight="5dip"android:progressDrawable="@drawable/seekbar_style"android:thumb="@drawable/seekbar_thumb" /></LinearLayout></LinearLayout><ListViewandroid:id="@+id/main_listview"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_above="@+id/linearLayout1"android:layout_below="@id/main_volumeLayout"android:fastScrollEnabled="true"android:layout_marginLeft="10dip"android:layout_marginRight="10dip"android:background="@drawable/widget_bg"android:cacheColorHint="#00000000" /><LinearLayoutandroid:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_alignParentBottom="true"android:layout_marginBottom="10dip"android:layout_marginLeft="10dip"android:layout_marginRight="10dip"android:background="@drawable/widget_bg"android:orientation="vertical" ><LinearLayoutandroid:id="@+id/linearLayout2"android:layout_width="fill_parent"android:layout_height="wrap_content"android:gravity="center" ><ImageButtonandroid:id="@+id/main_ibtn_pre"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dip"android:background="@drawable/button_previous" /><ImageButtonandroid:id="@+id/main_ibtn_play"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dip"android:background="@drawable/button_play" /><ImageButtonandroid:id="@+id/main_ibtn_stop"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dip"android:background="@drawable/button_stop" /><ImageButtonandroid:id="@+id/main_ibtn_next"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_margin="10dip"android:background="@drawable/button_next" /></LinearLayout><SeekBarandroid:id="@+id/main_seekBar"android:layout_width="fill_parent"android:layout_height="wrap_content"android:paddingLeft="10dip"android:paddingRight="10dip" /><RelativeLayoutandroid:id="@+id/relativeLayout2"android:layout_width="fill_parent"android:layout_height="wrap_content" ><TextViewandroid:id="@+id/main_tv_curtime"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentLeft="true"android:text="00:00" /><ImageView android:layout_toRightOf="@+id/main_tv_curtime"android:layout_marginLeft="5dp"android:id="@+id/main_iv_sleep"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@drawable/sleep_timer"android:background="@drawable/sleep_timer"/><TextViewandroid:id="@+id/main_tv_totaltime"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_alignParentRight="true"android:text="00:00" /></RelativeLayout></LinearLayout></RelativeLayout>

在MianActivity.java中的代码如下:
package com.zharma.greatlovemusic;

    import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.Timer;import com.zharma.data.Music;import com.zharma.data.MusicList;import android.support.v7.app.ActionBarActivity;import android.database.Cursor;import android.os.Bundle;import android.provider.MediaStore;import android.view.Menu;import android.view.MenuItem;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ListView;import android.widget.RelativeLayout;import android.widget.SeekBar;import android.widget.SimpleAdapter;import android.widget.TextView;public class MainActivity extends ActionBarActivity {// 显示组件private ListView listView;private RelativeLayout root_Layout;//歌曲列表对象private ArrayList<Music> musicArrayList;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);findViews();initMusicList();initListView();}void findViews() {listView = (ListView) findViewById(R.id.main_listview);root_Layout = (RelativeLayout) findViewById(R.id.relativeLayout1);}/**初始化音乐列表对象*/private void initMusicList() {musicArrayList = MusicList.getMusicList();//避免重复添加音乐if(musicArrayList.isEmpty()){Cursor mMusicCursor = this.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,MediaStore.Audio.AudioColumns.TITLE);int indexTitle = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);int indexArtist = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);int indexTotalTime = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION);int indexPath = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);/**通过mMusicCursor游标遍历数据库,并将Music类对象加载带ArrayList中*/for (mMusicCursor.moveToFirst(); !mMusicCursor.isAfterLast(); mMusicCursor.moveToNext()) { String strTitle = mMusicCursor.getString(indexTitle);String strArtist = mMusicCursor.getString(indexArtist);String strTotoalTime = mMusicCursor.getString(indexTotalTime);String strPath = mMusicCursor.getString(indexPath);if (strArtist.equals("<unknown>"))strArtist = "艺术家";Music music = new Music(strTitle, strArtist, strPath, strTotoalTime);musicArrayList.add(music);}}}/**设置适配器并初始化listView*/private void initListView() {List<Map<String, String>> list_map = new ArrayList<Map<String, String>>();HashMap<String, String> map;SimpleAdapter simpleAdapter;for (Music music : musicArrayList) {map = new HashMap<String, String>();map.put("musicName", music.getMusicName());map.put("musicArtist", music.getMusicArtist());list_map.add(map);} String[] from = new String[] { "musicName", "musicArtist" };int[] to = { R.id.listview_tv_title_item, R.id.listview_tv_artist_item };simpleAdapter = new SimpleAdapter(this, list_map, R.layout.listview,from, to);listView.setAdapter(simpleAdapter);}}

转载于:https://www.cnblogs.com/zharma/p/4571338.html

这篇关于Android小玩意儿-- 从头开发一个正经的MusicPlayer(一)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/475123

相关文章

SpringBoot开发中十大常见陷阱深度解析与避坑指南

《SpringBoot开发中十大常见陷阱深度解析与避坑指南》在SpringBoot的开发过程中,即使是经验丰富的开发者也难免会遇到各种棘手的问题,本文将针对SpringBoot开发中十大常见的“坑... 目录引言一、配置总出错?是不是同时用了.properties和.yml?二、换个位置配置就失效?搞清楚加

Android DataBinding 与 MVVM使用详解

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

Python中对FFmpeg封装开发库FFmpy详解

《Python中对FFmpeg封装开发库FFmpy详解》:本文主要介绍Python中对FFmpeg封装开发库FFmpy,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录一、FFmpy简介与安装1.1 FFmpy概述1.2 安装方法二、FFmpy核心类与方法2.1 FF

Android ViewBinding使用流程

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

基于Python开发Windows屏幕控制工具

《基于Python开发Windows屏幕控制工具》在数字化办公时代,屏幕管理已成为提升工作效率和保护眼睛健康的重要环节,本文将分享一个基于Python和PySide6开发的Windows屏幕控制工具,... 目录概述功能亮点界面展示实现步骤详解1. 环境准备2. 亮度控制模块3. 息屏功能实现4. 息屏时间

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

使用Python开发一个现代化屏幕取色器

《使用Python开发一个现代化屏幕取色器》在UI设计、网页开发等场景中,颜色拾取是高频需求,:本文主要介绍如何使用Python开发一个现代化屏幕取色器,有需要的小伙伴可以参考一下... 目录一、项目概述二、核心功能解析2.1 实时颜色追踪2.2 智能颜色显示三、效果展示四、实现步骤详解4.1 环境配置4.

Python使用smtplib库开发一个邮件自动发送工具

《Python使用smtplib库开发一个邮件自动发送工具》在现代软件开发中,自动化邮件发送是一个非常实用的功能,无论是系统通知、营销邮件、还是日常工作报告,Python的smtplib库都能帮助我们... 目录代码实现与知识点解析1. 导入必要的库2. 配置邮件服务器参数3. 创建邮件发送类4. 实现邮件

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

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

基于Python开发一个有趣的工作时长计算器

《基于Python开发一个有趣的工作时长计算器》随着远程办公和弹性工作制的兴起,个人及团队对于工作时长的准确统计需求日益增长,本文将使用Python和PyQt5打造一个工作时长计算器,感兴趣的小伙伴可... 目录概述功能介绍界面展示php软件使用步骤说明代码详解1.窗口初始化与布局2.工作时长计算核心逻辑3