Android自带音乐播放器代码分析(2)
要查看android自带的Music播放器需要下载android源码,具体方法在android源码网站有介绍,自带的Music播放器的代码位于android4.0\packages\apps\Music中为了查看代码的方便可以将项目导入Eclipse查看,忽略提示的错误。
首先查看整个项目中的java类如下介绍各类的作用:
1.AlbumBrowserActivity此类继承自ListActivity实现接口View.OnCreateContextMenuListener,MusicUtils.Defs,ServiceConnection该类的作用是显示专辑的界面。
2.ArtistAlbumBrowserActivity此类继承自ExpandableListActivity实现接口View.OnCreateContextMenuListener,MusicUtils.Defs,ServiceConnection该类的作用是显示艺术家列表,内部是ExpandableList。
3.AudioPreview此类继承自Activity实现接口OnPreparedListener,OnErrorListener,OnCompletionListener此类提供界面当其他程序如彩信要加入音乐时提供音乐预览的界面。
4.CheckableRelativeLayout此类继承自RelativeLayout实现接口Checkable,这是一个自定义的布局,布局文件。
5.CreatePlaylist此类继承自Activity该类提供创建播放列表的界面。
6.DeleteItems此类继承自Activity该类提供删除音乐时的提示界面主题是Dialog。
7.MediaAppWidgetProvider此类继承自AppWidgetProvider,其作用是实现桌面的音乐插件,窗口小部件。
8.MediaButtonIntentReceiver此类继承自BroadcastReceiver,此类提供对相应按钮按下发送的广播进行接收的机制,蓝牙耳机。
9.MediaPickerActivity此类继承自ListActivity实现接口MusicUtils.Defs。
10.MediaPlaybackActivity此类继承自Activity实现接口MusicUtils.Defs,View.OnTouchListener,View.OnLongClickListener该类提供音乐播放的界面,用户在该界面上可以点击控制音乐的播放,暂停,前进和后退。
11.MediaPlaybackService此类继承自Service此类提供音乐的播放服务,音乐的播放控制都在内部定义的。
12.MusicAlphabetIndexer此类继承自AlphabetIndexer。
13.MusicBrowserActivity此类继承自Activity实现接口MusicUtils.Defs。
界面启动时4个tab的选择:
MusicBrowserActivity中,程序启动入口函数。
activeTab=MusicUtils.getIntPref(this,"activetab",R.id.artisttab);
每次程序退出时,讲记录当前界面为哪一个tab选项,存储到数据库SharedPreferences中,当程序再次启动时,从数据库中获取对应存储的tab值。如果没有,默认为activeTab=R.id.artisttab;
MusicUtils.activateTab(this,activeTab);
启动对应tab的activity.intent.putExtra("withtabs",true);标记所启动的activity有没有tab选择条。
在对应的四个应用程序界面的oncreate中,都有调用MusicUtils.updateButtonBar(this,R.id.xxxtab);
来更新界面是否需要显示tab选择条。
14.MusicPicker此类继承自ListActivity,添加附件。
15.MusicUtils该类提供获取音乐播放状态的辅助方法,工具类。
16.PlaylistBrowserActivity此类继承自View.OnCreateContextMenuListener,MusicUtils.Defs此类用于显示播放列表。
17.QueryBrowserActivity此类继承自ListActivity实现接口MusicUtils.Defs,ServiceConnection,长按搜索。
18.RenamePlaylist此类继承自Activity该类用于对音乐列表进行重命名。
19.RepeatingImageButton此类继承自ImageButton该类实现长按某个图片时歌曲的快进、快退。
20.ScanningProgress此类继承自Activity显示当前扫描的进度,歌曲扫描。
21.SharedPreferencesCompat存储上次使用时的某些播放器首选项的信息保存,开机时保留播放列表信息。
22.SortCursor此类继承自AbstractCursor。
23.TouchInterceptor此类继承自ListView,新建播放列表中,歌曲位置移动。
24.TrackBrowserActivity此类继承自ListActivity实现接口View.OnCreateContextMenuListener,MusicUtils.Defs,ServiceConnection,歌曲界面。
25.VerticalTextSpinner此类继承自View,
26.VideoBrowserActivity此类继承ListActivity,实现接口MusicUtils.Defs。
27.WeekSelector此类继承自Activity。
28.IMediaPlaybackService.aidl此文件中定义了IPC的接口,mService。
待解决:
29.AudioEffectControlPanel.java此类继承ListActivity,选择音效设置,低音增强,虚拟化,预设混响,均衡器showEQPresetListDialog(){正常、古典、舞曲、平直、民歌、重金属、嘻哈、爵士、流行、摇滚}
30.AudioEffectReceiver.java此类继承BroadcastReceiver,
31.AudioPreviewStarter.java
32.LrcFileException.java此类继承Exception,处理歌词文件异常
33.LyricsBody.java处理歌词文件,对对歌词进行处理,显示歌词
34.MusicLogUtils.java该类提供获取音乐播放状态的Log辅助方法,Log工具类。
35.ScrollLrcView.java此类继承ScrollView,歌词滚动显示。
(系统自带播放界面)(修改后的播放界面)
Audio_player_zwq.xml文件
整体布局:
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/album_info"//定义id:album_info
android:layout_width="match_parent"android:layout_height="match_parent"
android:orientation="vertical"android:background="#f0f0f0">
//顶部布局:随机播放,艺术家名、歌曲名,repeat播放
//水平线性布局,艺术家名,歌曲名用垂直线性布局
<LinearLayout
android:layout_width="match_parent"android:layout_height="70dip"
android:orientation="horizontal"android:background="@drawable/top">
<ImageButton
android:id="@+id/shuffle"//随机播放按钮
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_gravity="center"android:layout_weight="1"
android:background="#00000000"/>
<LinearLayout
android:layout_width="150dip"android:layout_height="wrap_content"
android:orientation="vertical"android:layout_gravity="center"
android:layout_weight="2"android:paddingTop="5dip">
<TextView
android:id="@+id/artistname"//艺术家名字
android:textSize="16sp"android:singleLine="true"
android:ellipsize="end"android:textStyle="bold"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/albumname"//专辑名,在程序中设置visibility为gone
android:textSize="14sp"android:singleLine="true"
android:ellipsize="end"android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"android:layout_height="wrap_content"/>
<TextView
android:id="@+id/trackname"//歌曲名
android:textSize="20sp"android:singleLine="true"
android:ellipsize="end"android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"android:layout_height="wrap_content"/>
</LinearLayout>
<ImageButtonandroid:id="@+id/repeat"//repeat按钮
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:layout_gravity="center"android:layout_weight="1"
android:background="#00000000"/>
</LinearLayout>
//定义布局,加载id,控制进度条可见/不可见
<LinearLayout
android:id="@+id/barvisible"android:layout_width="match_parent"
android:layout_height="wrap_content"android:background="@drawable/background_c"
android:orientation="vertical">
//半透明的TextView歌曲次序/总歌曲数
<TextView
android:id="@+id/ordertxt"android:layout_width="match_parent"
android:layout_height="wrap_content"android:orientation="horizontal"
android:textSize="18sp"android:paddingTop="2dip"
android:paddingBottom="2dip"android:textColor="#ffffffff"
android:gravity="center_horizontal"android:background="#2f0000ff"/>
<ImageView
android:id="@+id/album"android:background="@drawable/album_border_large"
android:layout_width="160dip"android:layout_height="match_parent"
android:layout_gravity="center_horizontal"android:visibility="invisible"
android:layout_marginTop="6dip"android:layout_marginBottom="8dip"/>
<LinearLayout//定义的Seekbar布局
android:id="@+id/seekbarlayout"android:orientation="vertical"
android:layout_width="match_parent"android:layout_height="wrap_content"
android:background="#2f0000ff">
<SeekBar//自定义SeekBar
android:thumb="@layout/thumb"android:id="@android:id/progress"
android:maxHeight="2dip"android:minHeight="2dip"
android:layout_width="250dip"android:layout_height="wrap_content"
android:layout_marginTop="10dip"android:layout_gravity="center_horizontal"/>
<LinearLayout
android:layout_width="match_parent"android:layout_height="wrap_content"
android:orientation="horizontal"android:paddingLeft="11dip"
android:paddingRight="11dip"android:paddingTop="0dip"
android:paddingBottom="8dip">
<TextView
android:id="@+id/currenttime"//歌曲播放时间
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp"android:textStyle="bold"
android:shadowColor="#ff000000"android:shadowDx="0"
android:shadowDy="0"android:shadowRadius="3"
android:layout_gravity="bottom"android:layout_weight="1"
android:layout_width="0dip"android:paddingLeft="5dip"
android:layout_height="wrap_content"/>
<TextViewandroid:id="@+id/totaltime"//歌曲总时间
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="14sp"android:textStyle="bold"
android:shadowColor="#ff000000"android:shadowDx="0"
android:shadowDy="0"android:shadowRadius="3"
android:gravity="right"android:paddingRight="5dip"
android:layout_gravity="bottom"android:layout_weight="1"
android:layout_width="0dip"android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<includelayout="@layout/audio_player_common_zwq"/>
</LinearLayout>
audio_player_common_zwq.xml文件
<mergexmlns:android="http://schemas.android.com/apk/res/android">
<View
android:layout_width="match_parent"android:layout_height="1px"
android:background="#ffffffff"/>
<LinearLayout
android:id="@+id/btnlayout"
android:orientation="vertical"android:layout_width="match_parent"
android:layout_height="105dip"android:background="@drawable/bottom">
<LinearLayout
android:id="@+id/media_button"android:layout_width="match_parent"
android:layout_height="wrap_content"android:paddingTop="1dip"
android:paddingBottom="1dip"android:orientation="horizontal">
<TextView
android:layout_gravity="bottom"android:layout_weight="1"
android:layout_width="0dip"android:paddingLeft="5dip"
android:layout_height="wrap_content"/>
<LinearLayout
android:layout_width="wrap_content"android:layout_height="wrap_content"
android:orientation="horizontal"android:layout_gravity="bottom"
android:layout_marginTop="3dip"android:layout_marginBottom="5dip"
android:gravity="center">
<com.android.music.RepeatingImageButtonandroid:id="@+id/prev"//上一曲按钮
android:background="#00000000"android:layout_height="30dip"
style="@android:style/MediaButton.Previous"/>
//播放、暂停按钮
<ImageButtonandroid:id="@+id/pause"android:background="#00000000"android:layout_height="30dip"style="@android:style/MediaButton.Play"/>
<com.android.music.RepeatingImageButtonandroid:id="@+id/next"
android:background="#00000000"android:layout_height="30dip"
style="@android:style/MediaButton.Next"/>
</LinearLayout>
<TextViewandroid:layout_gravity="bottom"
android:layout_weight="1"android:layout_width="0dip"
android:paddingLeft="5dip"android:layout_height="wrap_content"/>
</LinearLayout>
<Button
android:id="@+id/curplaylist"//播放列表按钮
android:layout_width="200dip"
android:layout_height="40dip"
android:gravity="right|center_vertical"
android:layout_gravity="center_horizontal"
android:layout_marginTop="2dip"
android:background="@drawable/list"
android:text="@string/listname_zwq"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_marginBottom="15dip"
adroid:paddingRight="60dip"/>
</LinearLayout>
</merge>
更多相关文章
- Android中Intent传递类对象的两种方式
- 布局优化:使用include、merge、ViewStub
- Android双屏异显另辟蹊径---minui的移植
- Android适配器Adapter的学习
- 你需要了解下Android(安卓)View的更新 requestLayout 与重绘 inv
- 详解Android解析Xml的三种方式——DOM、SAX以及XMLpull
- Android(安卓)-- 序列化Parcelable与Serializable区别及用法
- Android之View的诞生之谜
- android 中fragment和activity的区别?