Android:一步一步实现音乐播放器
Android:一步一步实现音乐播放器
已经做过一个Android版音乐播放器,模仿音乐播放器项目(见),这个播放器基本功能已经实现,但是最大的问题是播放代码放在了activity中处理的,当推出音乐播放界面的时候,音乐是需要继续播放,当带过来电话时音乐需要暂停,打完电话继续播放,所以以前的版本还是有很大问题的,今天决定一步一步实现一个功能齐全的播放器,把播放控制代码放在service中。首先来实现这样一个简单的界面:
新建一个android项目,如图所示:
把项目中用到的图片拷贝到drawable目录下,编写main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@android:id/tabhost"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" >
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- android:padding="5dp" >
- <TabWidget
- android:id="@android:id/tabs"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <FrameLayout
- android:id="@android:id/tabcontent"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:padding="5dp" />
- </LinearLayout>
- </TabHost>
- public class MainActivity extends TabActivity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
- WindowManager.LayoutParams.FLAG_FULLSCREEN);
- setContentView(R.layout.main);
- Resources res = getResources();
- TabHost tabHost = getTabHost();
- TabHost.TabSpec spec;
- Intent intent;
- intent = new Intent().setClass(this, ListActivity.class);
- spec = tabHost.newTabSpec("音乐").setIndicator("音乐",
- res.getDrawable(R.drawable.item))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, ArtistsActivity.class);
- spec = tabHost.newTabSpec("艺术家").setIndicator("艺术家",
- res.getDrawable(R.drawable.artist))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, AlbumsActivity.class);
- spec = tabHost.newTabSpec("专辑").setIndicator("专辑",
- res.getDrawable(R.drawable.album))
- .setContent(intent);
- tabHost.addTab(spec);
- intent = new Intent().setClass(this, SongsActivity.class);
- spec = tabHost.newTabSpec("最近播放").setIndicator("最近播放",
- res.getDrawable(R.drawable.album))
- .setContent(intent);
- tabHost.addTab(spec);
- tabHost.setCurrentTab(0);
- }
- }
这样上面的主界面就完成了。
|
评论暂时关闭