文档介绍:黑马程序员安卓教程:音乐播放器之常见API
Android自带多媒体播放器,下面我们通过一个具体的DEMO来实现播放音乐的一系列功能即播放,暂停,继续,重播,停止。具体实现如下:
新建工程
【File】à【new】à【Android Application Project】命名为:14_音乐播放器常见API
编写布局
根据需求编写布局文件,效果如图1-1所示:
图1-1所示
图1-1对应的xml文件如例1-1:
例1-1
<LinearLayout xmlns:android="http://schemas./apk/res/android"
xmlns:tools="http://schemas./tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="play"
android:text="播放" />
<Button
android:onClick="pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停" />
<Button
android:onClick="resume"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="继续" />
<Button
android:onClick="replay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="重播" />
<Button
android:onClick="stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止" />
<SeekBar
android:id="@+id/sb"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
例1-1在布局页面上定义了一系列功能按钮,当点击按钮时便执行相对应的点击事件,各个按钮对应的点击事件是MediaPlayer生命周期的函数,对于MediaPlayer生命周期的回调函数,我们在后边的文档有详细的介绍。在播放音乐时,进度条Se