跳到主要内容

Android音乐播放器

layout_alignParentBottom:对齐父控件底部

添加读取sd卡的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

注意,高版本不能之间这样取得权限,否则会闪退

使用参考

20170226130924615

取得的路径是如下的格式,需要手动转成可以使用的路径

loadLocalMusic: /storage/emulated/0/netease/cloudmusic/Music/杨栋梁 - 我的心在海对面.mp3

MediaPlayer

mediaplayer 播放器 播放完毕回调

mMediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Log.d("tag", "播放完毕");
//根据需要添加自己的代码。。。
}
});

RecyclerView

滚到特定位置上

rv.getLayoutManager().scrollToPosition(positionInTheAdapter).

错误处理

  1. MediaPlayer error: pause called in state 64 暂停了已经暂停的MediaPlayer

  2. 如果报找不到文件的错可能是因为当前的sdk版本选择的太高了

Handler 异步通信

Android 异步通信 参考 jhdhaid

就是说主线程与子线程的通信媒介 = Handler

dajdjaldjlajdsla

使用方式

Handler使用方式 因发送消息到消息队列的方式不同而不同 共分为2种:使用Handler.sendMessage()、使用Handler.post()

排序

按照拼音排序参考

特效

点击之后的涟漪效果

android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"

Tips:如果你给CardView设置了点击事件,就不需要设置android:clickable="true"了

SeekBar进度条

为了监听SeekBar的拖动情况,我们可以为它绑定一个onSeekBarChangeListener监听器

CardView

参考 本质就是一个自带很多效果的ui控件(CardView继承自FrameLayout,它是一个带圆角背景和阴影的FrameLayout.) 要实现圆角,阴影等使用这个就很适合了 XML attributes 属性名 | 作用 ------- | ------- cardBackgroundColor | 背景颜色 cardCornerRadius | 边角半径 cardElevation | 阴影大小 cardMaxElevation | 最大仰角 cardPreventCornerOverlap | 边角重叠 cardUseCompatPadding |
contentPadding |
contentPaddingBottom |
contentPaddingLeft |
contentPaddingRight |
contentPaddingTop |

CardView样式

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:padding="10dp"
app:cardBackgroundColor="@color/colorMusicItem"
app:cardCornerRadius="15dp"
app:cardElevation="1dp">

...
</androidx.cardview.widget.CardView>