极简音乐播放器(一)

博主以前看到有许多学习android的人都做了音乐播放器,也就尝试着做了一个,播放器按钮背景图片资源由博客小巫的音乐播放器上抠出来的,代码纯自敲,本播放器纯用于学习,不喜勿喷,谢谢...

只讲思路,源代码结尾附上

1、播放器界面布局

极简音乐播放器(一)_第1张图片


    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background"
    android:orientation="vertical" >

            android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:orientation="horizontal" >

                    android:id="@+id/btn_pre"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/pre_selector" />

                    android:id="@+id/btn_repeat"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:background="@drawable/repeat_seletor" />

                    android:id="@+id/cb_playorpause"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:button="@null"
            android:background="@drawable/playorpause_seletor" />

                    android:id="@+id/btn_shuffle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/shuffle_seletor" />

                    android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/next_seletor" />
   

            android:id="@+id/lv_music"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_weight="1" >
   

            android:id="@+id/sb_music"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

            android:layout_width="fill_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:orientation="horizontal"
        >

                    android:id="@+id/iv_music"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:src="@drawable/ic_launcher" />

                    android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            android:padding="8dp"
             >
                            android:layout_height="wrap_content"
                android:text="未知"
                android:textSize="16dp"
                android:textColor="#FFFFFF"
                android:layout_weight="1"
                android:id="@+id/tv_music_name"
               />
                            android:layout_height="wrap_content"
                android:text="00:00"
                android:textSize="16dp"
                android:textColor="#FFFFFF"
                android:layout_weight="1"
                android:id="@+id/tv_music_duration"/>
       
   

2、对ListView进行适配,通过内容提供者获取歌曲资源并进行一定的封装,刷新歌曲列表
3、注册歌曲列表的子条目的监听事件,实现点击一首歌曲,播放该歌曲。子条目点击事件中,发送一个服务请求,同时新建一个服务类PlayService用于接收该请求,并在该类中进行Mediaplayer的实例化,进行歌曲的播放
4、对界面各个播放控制按钮进行事件监听,相应按钮的点击事件触发发送相应的服务请求,由PlayService进行接收处理,因为当前mediaplayer就在该类中创建
5、歌曲播放时,对进度条进行实时刷新:在Playservice类中,在oncreate()方法下创建一个Timer,每隔一秒执行一下timetask内部子线程操作,在子线程中获取当前mediaplayer已经播放歌曲的部分时长,以及总时长。并且用广播进行发送出去。主界面activity类中创建一个BroadcastReceiver类进行监听,然后获取广播携带的信息,进行进度条的设置。
6、通过进度条控制歌曲的播放:注册进度条点击事件,实现点击事件的方法,只需要重写onStopTrackingTouch(SeekBar seekBar)方法,该方法在拖动进度条结束的时候出发,拖动结束时,获取该进度条的当前进度,然后用service发送给PlayService,然后调用mediaPlayer.seekTo(position),就可以实现进度条控制歌曲播放进度
至此,大体的设置思想就这些了。本人新手,请多指教
源代码 http://pan.baidu.com/s/1c0dLLZE
      

你可能感兴趣的:(Android极小项目实践)