Android MediaPlayer 音乐播放器扫描 本地音乐、上一曲、下一曲切歌、播放本地音乐(上)

简介: Android MediaPlayer 音乐播放器扫描 本地音乐、上一曲、下一曲切歌、播放本地音乐(上)

Android MediaPlayer 本地音乐播放器


运行截图


项目请在真机(自己的手机)上测试运行,因为我不喜欢用虚拟机。

为了不浪费您的时间,先看一下运行的效果图,

一进去先进行音乐扫描,然后列表展示出来,点击即可播放。


20200319171049255.png

20200319171129159.png


源码地址


这个给不想浪费时间往下看的朋友,只因你的时间很宝贵。


前言


至于为什么写一个这样的Demo呢,因为有很多人学习Android就是对于手机应用感兴趣,而网络上的很多源码,一些开源项目的代码小白看不懂,小白能看懂的,有些博主又要用积分下载,痛定思痛,索性自己写一个,当然在写的过程中查阅了网络的资料,也加入了自己的想法,希望能帮到对手机音乐播放器这方面有想法的朋友,好了,话不多说,进入正题.:


代码解释


项目配置


1.权限配置:


打开AndroidManifest.xml


<!--文件读写权限  Android6.0 以后需要动态获取  10.0之后对文件的处理更复杂了-->
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


2.依赖引入:


先打开工程的build.gradle,


加入如下图中的代码

20200319173613856.png


    maven { url "https://jitpack.io" }
        mavenCentral()

然后打开项目的build.gradle,在dependencies闭包中加入以下代码依赖:

  //butterknife  绑定视图依赖BindView,告别findById,不过你还得安装一个butterknife插件才行
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    //Google Material控件,以及迁移到AndroidX下一些控件的依赖
    implementation 'com.google.android.material:material:1.0.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.1.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0'
    //RecyclerView最好的适配器,让你的适配器一目了然,告别代码冗余
    implementation 'com.github.CymChad:BaseRecyclerViewAdapterHelper:2.9.30'
    //权限请求框架
    implementation 'com.tbruyelle.rxpermissions2:rxpermissions:0.9.4@aar'
    implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
    implementation "io.reactivex.rxjava2:rxjava:2.0.0"
    //状态栏
    implementation 'com.readystatesoftware.systembartint:systembartint:1.0.3'


在android闭包中加入

compileOptions {
        sourceCompatibility = 1.8
        targetCompatibility = 1.8
    }


指定你的JDK版本,我的项目中用的是AndroidX,如果你没有用过的,建议你先去了解一下:


好了,下面看布局文件,这次是做的一个完成的项目,所以新建了一个工程,里面有一些样式和图片、图标,工程目录如下图,这里就不在做解释了:


20200319172721224.png


布局文件只有两个,activity_main.xml和item_music_rv_list.xml,activity_main这个是项目创建的时候赠送的,item_music_rv_list是为了显示歌曲信息而创建的


activity_main.xml代码:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:background="@mipmap/img_main_bg_1"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <!--Toolbar-->
    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:contentInsetLeft="@dimen/activity_horizontal_margin"
        app:popupTheme="@style/AppTheme.PopupOverlay">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="聆听音乐"
            android:textColor="@color/common_black"
            android:textSize="18sp" />
        <TextView
            android:visibility="gone"
            android:id="@+id/tv_clear_list"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="right"
            android:foreground="@drawable/bg_white"
            android:layout_marginRight="@dimen/dp_16"
            android:padding="@dimen/dp_15"
            android:text="清空"
            android:textColor="@color/common_black" />
    </androidx.appcompat.widget.Toolbar>
    <!--扫描歌曲的布局-->
    <LinearLayout
        android:id="@+id/scan_lay"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:orientation="vertical">
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@mipmap/img_scan_logo" />
        <Button
            android:id="@+id/btn_scan"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_20"
            android:background="@drawable/selector_scan_btn"
            android:text="开始扫描"
            android:textColor="#FFF"
            android:textSize="22sp" />
    </LinearLayout>
    <!--扫描之后,歌曲的展示和播放布局-->
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <!--播放歌曲时,显示你播放的是哪一首歌,跑马灯效果-->
        <LinearLayout
            android:visibility="gone"
            android:id="@+id/play_state_lay"
            android:layout_marginTop="@dimen/dp_1"
            android:gravity="center"
            android:background="@color/half_transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <ImageView
                android:id="@+id/play_state_img"
                android:background="@mipmap/list_pause_state"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
            <TextView
                android:gravity="center"
                android:text="歌曲信息"
                android:ellipsize="marquee"
                android:focusableInTouchMode="true"
                android:focusable="true"
                android:marqueeRepeatLimit="marquee_forever"
                android:singleLine="true"
                android:textColor="@color/white"
                android:id="@+id/tv_play_song_info"
                android:orientation="vertical"
                android:layout_width="@dimen/dp_150"
                android:layout_height="wrap_content"/>
        </LinearLayout>
        <!--歌曲的展示列表,都什么年代了,你还在用ListView吗?-->
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/rv_music"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>
        <!--底部歌曲播放控制布局-->
        <LinearLayout
            android:layout_marginTop="@dimen/dp_4"
            android:orientation="vertical"
            android:background="@color/half_transparent"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <LinearLayout
                android:gravity="center_vertical"
                android:layout_alignParentBottom="true"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <!--播放时间-->
                <TextView
                    android:id="@+id/tv_play_time"
                    android:text="00:00"
                    android:layout_marginLeft="@dimen/dp_4"
                    android:textSize="@dimen/sp_14"
                    android:textColor="@color/white"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
                <!--歌曲播放进度条 ,样式是自定义,因为原生的太丑了-->
                <SeekBar
                    android:layout_marginLeft="@dimen/dp_10"
                    android:layout_marginRight="@dimen/dp_10"
                    android:id="@+id/time_seekBar"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:layout_height="wrap_content"
                    android:max="100"
                    android:maxHeight="2dp"
                    android:minHeight="2dp"
                    android:progress="0"
                    android:progressDrawable="@drawable/seekbar_style"
                    android:thumb="@drawable/thumb" />
                <!--总时间-->
                <TextView
                    android:id="@+id/tv_total_time"
                    android:layout_marginRight="@dimen/dp_4"
                    android:text="00:00"
                    android:textSize="@dimen/sp_14"
                    android:textColor="@color/white"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"/>
            </LinearLayout>
            <LinearLayout
                android:padding="@dimen/dp_10"
                android:gravity="center"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <!--上一曲-->
                <ImageView
                    android:id="@+id/btn_previous"
                    android:background="@mipmap/icon_previous"
                    android:layout_width="@dimen/dp_40"
                    android:layout_height="@dimen/dp_40" />
                <!--播放或暂停-->
                <ImageView
                    android:layout_marginLeft="@dimen/dp_20"
                    android:layout_marginRight="@dimen/dp_20"
                    android:id="@+id/btn_play_or_pause"
                    android:background="@mipmap/icon_pause"
                    android:layout_width="@dimen/dp_44"
                    android:layout_height="@dimen/dp_44" />
                <!--下一曲-->
                <ImageView
                    android:id="@+id/btn_next"
                    android:background="@mipmap/icon_next"
                    android:layout_width="@dimen/dp_40"
                    android:layout_height="@dimen/dp_40"/>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


item_music_rv_list.xml


<?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="wrap_content"
    android:id="@+id/item_music"
    android:background="@color/color_transparent_10"
    android:foreground="@drawable/bg_white"
    android:orientation="vertical"
    android:layout_marginBottom="@dimen/dp_1"
    android:gravity="center_vertical">
    <LinearLayout
        android:gravity="center_vertical"
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:gravity="center_vertical"
            android:padding="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <TextView
                android:id="@+id/tv_position"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="2dp"
                android:text="1"
                android:textColor="@color/white"
                android:textSize="16sp" />
            <LinearLayout
                android:layout_marginLeft="@dimen/dp_10"
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <LinearLayout
                    android:gravity="center_vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/tv_song_name"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="歌曲名"
                        android:maxLines="1"
                        android:textColor="@color/white"
                        android:textSize="@dimen/sp_18" />
                </LinearLayout>
                <LinearLayout
                    android:layout_marginTop="@dimen/dp_4"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">
                    <TextView
                        android:id="@+id/tv_singer"
                        android:layout_width="0dp"
                        android:layout_weight="1"
                        android:text="歌手"
                        android:ellipsize="end"
                        android:maxLines="1"
                        android:layout_height="wrap_content"
                        android:textColor="@color/white"
                        android:textSize="14sp" />
                    <TextView
                        android:id="@+id/tv_duration_time"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="12dp"
                        android:textColor="@color/white"
                        android:text="时间"
                        android:textSize="14sp" />
                </LinearLayout>
            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>


布局其实没有什么好讲解的,就是显示和隐藏的控制。



相关文章
|
11月前
|
存储 缓存 Android开发
安卓Jetpack Compose+Kotlin, 使用ExoPlayer播放多个【远程url】音频,搭配Okhttp库进行下载和缓存,播放完随机播放下一首
这是一个Kotlin项目,使用Jetpack Compose和ExoPlayer框架开发Android应用,功能是播放远程URL音频列表。应用会检查本地缓存,如果文件存在且大小与远程文件一致则使用缓存,否则下载文件并播放。播放完成后或遇到异常,会随机播放下一首音频,并在播放前随机设置播放速度(0.9到1.2倍速)。代码包括ViewModel,负责音频管理和播放逻辑,以及UI层,包含播放和停止按钮。
|
7月前
|
缓存 Java Shell
Android 系统缓存扫描与清理方法分析
Android 系统缓存从原理探索到实现。
238 15
Android 系统缓存扫描与清理方法分析
|
11月前
|
XML 存储 数据库
如何使用Android Studio创建一个基本的音乐播放器应用
如何使用Android Studio创建一个基本的音乐播放器应用
452 0
|
9月前
|
Android开发
Android 利用MediaPlayer实现音乐播放
本文提供了一个简单的Android MediaPlayer音乐播放示例,包括创建PlayerActivity、配置AndroidManifest.xml和activity_player.xml布局,以及实现播放和暂停功能的代码。
129 0
Android 利用MediaPlayer实现音乐播放
|
9月前
|
编解码 网络协议 开发工具
Android平台如何实现多路低延迟RTSP|RTMP播放?
本文档详细介绍了大牛直播SDK在Android平台上实现RTSP与RTMP流媒体播放及录像功能的技术细节。早在2015年,SDK的第一版就已经支持了多实例播放,并且通过简单的实例封装就能轻松实现。文档中提供了代码示例,展示了如何开启播放、停止播放以及开始和停止录像等功能。此外,SDK还提供了丰富的配置选项,例如设置录像目录、文件大小限制、转码选项等。总结部分列出了该SDK的关键特性,包括但不限于高稳定性和低延迟的播放能力、多实例支持、事件回调、硬解码支持、网络状态监控以及复杂的网络环境处理等。这些功能使得SDK能够应对各种应用场景,特别是在对延迟和稳定性有极高要求的情况下表现优异。
201 5
|
9月前
|
编解码 网络协议 vr&ar
Android平台下VR头显如何低延迟播放4K以上超高分辨率RTSP|RTMP流
这段内容讲述了VR头显中实现高分辨率视频播放的技术背景与实现方法,并强调了其重要性。高分辨率对于提升VR体验至关重要,它能提供更清晰的画面、增强沉浸感、补偿透镜放大效应,并维持宽广视场角下的图像质量。文中提到的大牛直播SDK具备极低的延迟(200-400ms),支持多种协议与格式,并具有丰富的功能特性,如多实例播放、事件回调、视频及音频格式支持等。此外,提供了基于Unity的播放器示例代码,展示了如何配置播放参数并开始播放。最后,作者指出此类技术在远程控制、虚拟仿真等应用场景中的重要意义。
|
11月前
|
存储 Android开发 Kotlin
Kotlin开发安卓app,在使用 MediaPlayer 播放 res/raw 中的音乐时遇到突然中断的问题,而 onErrorListener 没有接收到任何报错
在使用 Android MediaPlayer 播放 res/raw 中的音乐时遇到中断问题,可能的原因包括资源问题、媒体文件编码格式、生命周期管理和设备资源配置。要排查问题,检查音频文件是否正确包含,格式编码是否支持,MediaPlayer 是否正确管理及释放,以及设备是否有足够存储和配置。通过设置 onErrorListener 日志和确保在 onDestroy 中释放资源来调试。如果文件过大,考虑使用 AssetManager。遵循这些步骤可帮助诊断并解决播放中断的问题。
|
Android开发 Java
Android VideoView播放视频控制:开始、暂停、快进(3)
Android VideoView播放视频控制:开始、暂停、快进(3) 本文在附录参考文章(1)的基础上增加的Android VideoView播放视频时候的控制。
2035 0
|
2月前
|
JavaScript Linux 网络安全
Termux安卓终端美化与开发实战:从下载到插件优化,小白也能玩转Linux
Termux是一款安卓平台上的开源终端模拟器,支持apt包管理、SSH连接及Python/Node.js/C++开发环境搭建,被誉为“手机上的Linux系统”。其特点包括零ROOT权限、跨平台开发和强大扩展性。本文详细介绍其安装准备、基础与高级环境配置、必备插件推荐、常见问题解决方法以及延伸学习资源,帮助用户充分利用Termux进行开发与学习。适用于Android 7+设备,原创内容转载请注明来源。
419 76
|
3月前
|
JavaScript 搜索推荐 Android开发
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡
106 8
【01】仿站技术之python技术,看完学会再也不用去购买收费工具了-用python扒一个app下载落地页-包括安卓android下载(简单)-ios苹果plist下载(稍微麻烦一丢丢)-客户的麻将软件需要下载落地页并且要做搜索引擎推广-本文用python语言快速开发爬取落地页下载-优雅草卓伊凡