Android:一步一步实现音乐播放器


已经做过一个Android版音乐播放器,模仿音乐播放器项目(见),这个播放器基本功能已经实现,但是最大的问题是播放代码放在了activity中处理的,当推出音乐播放界面的时候,音乐是需要继续播放,当带过来电话时音乐需要暂停,打完电话继续播放,所以以前的版本还是有很大问题的,今天决定一步一步实现一个功能齐全的播放器,把播放控制代码放在service中。

首先来实现这样一个简单的界面:

            

      新建一个android项目,如图所示:    

把项目中用到的图片拷贝到drawable目录下,编写main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@android:id/tabhost"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent" >  
  6.   
  7.     <LinearLayout  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="fill_parent"  
  10.         android:orientation="vertical"  
  11.         android:padding="5dp" >  
  12.   
  13.         <TabWidget  
  14.             android:id="@android:id/tabs"  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content" />  
  17.   
  18.         <FrameLayout  
  19.             android:id="@android:id/tabcontent"  
  20.             android:layout_width="fill_parent"  
  21.             android:layout_height="fill_parent"  
  22.             android:padding="5dp" />  
  23.     </LinearLayout>  
  24.   
  25. </TabHost>  
编写MainActivity类    
  1. public class MainActivity extends TabActivity {  
  2.     /** Called when the activity is first created. */  
  3.     @Override  
  4.     public void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         requestWindowFeature(Window.FEATURE_NO_TITLE);  
  7.         this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
  8.                 WindowManager.LayoutParams.FLAG_FULLSCREEN);  
  9.         setContentView(R.layout.main);  
  10.           
  11.         Resources res = getResources();   
  12.         TabHost tabHost = getTabHost();   
  13.         TabHost.TabSpec spec;   
  14.         Intent intent;    
  15.         intent = new Intent().setClass(this, ListActivity.class);  
  16.         spec = tabHost.newTabSpec("音乐").setIndicator("音乐",  
  17.                           res.getDrawable(R.drawable.item))  
  18.                       .setContent(intent);  
  19.         tabHost.addTab(spec);  
  20.           
  21.         intent = new Intent().setClass(this, ArtistsActivity.class);  
  22.         spec = tabHost.newTabSpec("艺术家").setIndicator("艺术家",  
  23.                           res.getDrawable(R.drawable.artist))  
  24.                       .setContent(intent);  
  25.         tabHost.addTab(spec);  
  26.   
  27.         intent = new Intent().setClass(this, AlbumsActivity.class);  
  28.         spec = tabHost.newTabSpec("专辑").setIndicator("专辑",  
  29.                           res.getDrawable(R.drawable.album))  
  30.                       .setContent(intent);  
  31.         tabHost.addTab(spec);  
  32.         intent = new Intent().setClass(this, SongsActivity.class);  
  33.         spec = tabHost.newTabSpec("最近播放").setIndicator("最近播放",  
  34.                           res.getDrawable(R.drawable.album))  
  35.                       .setContent(intent);  
  36.         tabHost.addTab(spec);  
  37.           
  38.   
  39.         tabHost.setCurrentTab(0);  
  40.   
  41.     }  
  42. }  
注意这里要继承的是TabActivity,关于TabHost的用法不做过多介绍,官网有。最后分别建立其他用到的activity和使用的xml布局文件,不要忘记在manifest中注册,

这样上面的主界面就完成了。

  • 1
  • 2
  • 3
  • 下一页

相关内容