[原]Android 音乐播放器源码分析1
manifest看到入口为MusicBrowserActivity.java
public class MusicBrowserActivity extends Activity
implements MusicUtils.Defs
实现了接口MusicUtils.Defs ,实际上它只不过是一系列的常量
public class MusicUtils { private static final String TAG = "MusicUtils"; public interface Defs { public final static int OPEN_URL = 0; public final static int ADD_TO_PLAYLIST = 1; public final static int USE_AS_RINGTONE = 2; public final static int PLAYLIST_SELECTED = 3;。。。这个是java中定义常量常用的方法,学习了
进入到onCreate函数
是一个Tab布局
int activeTab = MusicUtils.getIntPref(this, "activetab", R.id.artisttab);(这个函数其实是对取得SharedPreferences的封装)
在MusicUtils.java中通过getIntPref函数得到了当前Tab的资源ID. 默认是“艺术家”。
接着 MusicUtils.activateTab(this, activeTab);激活Tab中的内容(一个简单的switch..case判断),并结束当前Activity。
String shuf = getIntent().getStringExtra("autoshuffle"); if ("true".equals(shuf)) { mToken = MusicUtils.bindToService(this, autoshuffle); }然后得到Intent通过Key值“autoshuffle”传过来的的字符串,若为true则把此activity与服务帮定。
autoshuffle 是用来回调的ServiceConnection
serv.setShuffleMode(MediaPlaybackService.SHUFFLE_AUTO);
并设置播放服务的模式为自动
(Activity继承自ContextThemeWrapper,ContextThemeWrapper继承自ContextWrapper,ContextWrapper继承自Context)
默认进入artisttab
接下来进入ArtistAlbumBrowserActivity.java中
onCreste()中
if (icicle != null) { mCurrentAlbumId = icicle.getString("selectedalbum"); mCurrentAlbumName = icicle.getString("selectedalbumname"); mCurrentArtistId = icicle.getString("selectedartist"); mCurrentArtistName = icicle.getString("selectedartistname"); }Bundle得到一些保存过的当前的专辑ID,name,ArtistId,ArtistName.
然后,Intent过滤器,加入了一些动作,并注册了广播;
IntentFilter f = new IntentFilter(); f.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED); f.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED); f.addAction(Intent.ACTION_MEDIA_UNMOUNTED); f.addDataScheme("file"); registerReceiver(mScanListener, f);setContentView(R.layout.media_picker_activity_expanding);看看布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:gravity="center_vertical" ><!-- 当没有音乐的时候显示--> <include layout="@layout/sd_error" /><!-- 这里用一系列textView定义了artisttab albumtab songtab playlisttab 及 下方的nowplayingtab上内容的显示及点击状态等--> <include layout="@layout/buttonbar" /> <ExpandableListView android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:textSize="18sp" android:drawSelectorOnTop="false" android:fastScrollEnabled="true" android:indicatorLeft="8dip" android:indicatorRight="52dip" /><!-- 下方当前播放音乐的布局--> <include layout="@layout/nowplaying" /></LinearLayout>