最近在做仿网易云音乐播放器的实训项目,学习完后写此博客,一方面巩固自己理解,一方面方便各位浏览。
读完本文你将了解到:
- 如何利用MediaPlayer播放音乐
- 如何设置音乐进度条并实现自动滚动及手动定位
- 如何实现唱片转动功能
1.效果图
2.代码如下
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
//设置背景图片
<ImageView
android:id="@+id/listen_background_iv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@mipmap/img1" />
<RelativeLayout
android:id="@+id/listen_rl"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="1">
//设置返回按钮图片
<ImageView
android:id="@+id/listen_back1_iv"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:src="@mipmap/back1" />
//设置歌曲名
<TextView
android:id="@+id/listen_title_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="@id/listen_back1_iv"
android:text="成都"
android:textColor="#f9f7f7"
android:textSize="20sp" />
//设置歌手名
<TextView
android:id="@+id/listen_artist_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@id/listen_title_tv"
android:layout_below="@id/listen_title_tv"
android:layout_marginTop="5dp"
android:text="赵雷-"
android:textColor="#aeabab"
android:textSize="15sp" />
//设置专辑名
<TextView
android:id="@+id/listen_album_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/listen_artist_tv"
android:layout_toRightOf="@id/listen_artist_tv"
android:text="成都"
android:textColor="#aeabab"
android:textSize="15sp" />
//设置“分享”按钮(本代码中未实现)
<ImageView
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:src="@mipmap/share" />
//设置布局间的线
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:layout_below="@id/listen_artist_tv"
android:background="#bababa" />
</RelativeLayout>
//设置唱片圆盘
<ImageView
android:id="@+id/listen_changpian_img"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@mipmap/play_page_disc" />
//设置圆盘上的指针
<ImageView
android:id="@+id/listen_zhizhen_iv"
android:layout_width="100dp"
android:layout_height="150dp"