当前位置:编程学习 > JAVA >>

Android歌词秀设计思路(7)水到渠成

我们用了6篇文章的篇幅做了铺垫,终于到了真正的应用程序了。这部分还是一如既往的简单。
有关应用的类有两个,一个是LiryicMain,一个是SelectFileActivity。都是差不多最低限度的内容,没有任何华丽的内容。
先看看这两个类在整个软件中的位置。从图中可以看出LyricMain是软件全体的控制者。SelectFileActivity也为LyricMain提供服务。

SelectFileActivity太过简单,本文中就不再说明了。我们集中篇幅说明一下LyricMain。
首先是数据成员。一个是LyricPlayerServiceProxy,歌词播放服务的代理,一个是用来保存歌词结束位置的List。
private LyricPlayerServiceProxy mProxy = new LyricPlayerServiceProxy(this); 
private ArrayList<Integer> mLyricEndList = new ArrayList<Integer>();
LyricPlayerServiceProxy是前面已经介绍过的内容,在这里就不在重复了。mLyricEndList需要说明一下。在这个软件中我们将所有歌词都表示在一个TextEditView中,为了能够表示当前播放中的歌词,我们将每一句歌词的位置保存在mLyricEndList中,这样当播放中的歌词发生变化时,只要将这句歌词设为选中状态就可以了。
 接下来是LyricMediaInfoProvider的最简单实现,提供了固定的歌名和歌曲文件的位置信息。如果需要切换歌曲,需要再复杂一些。
private class LyricMediaInfoProvider implements MediaPlayerService.MediaInfoProvider{ 
    String mUrl; 
    String mTitle; 
     
    LyricMediaInfoProvider(String url, String title){ 
        mUrl = url; 
        mTitle = title; 
    } 
     
    @Override
    public boolean moveToPrev() { 
        // TODO Auto-generated method stub 
        return false; 
    } 
     
    @Override
    public boolean moveToNext() { 
        // TODO Auto-generated method stub 
        return false; 
    } 
     
    @Override
    public String getUrl() { 
        return mUrl; 
    } 
     
    @Override
    public String getTitle() { 
        // TODO Auto-generated method stub 
        return mTitle; 
    } 
}
接下来是onCreate方法。主要做了几件事
1.建立和LyricPlayerServiceProxy之间的联系。
2.提供了的实现NotificationProvider(详细信息请参照: Android歌词秀设计思路(4)通用的音乐播放服务(下) )
3.设置ImageButton的尺寸。
/** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main); 
        
       mLyricEdit = (EditText)this.findViewById(R.id.editLyric); 
        
       mProxy.setConnectionListener(this); 
       mProxy.setLyricPlayerListener(this); 
       mProxy.setNotificationProvider(new MediaPlayerService.NotificationProvider(){ 
        @Override
        public Notification createNotification(Context context) { 
            Notification notification = new Notification(R.drawable.button_blue_play, mProxy.getTitle(), System.currentTimeMillis()); 
            // The PendingIntent to launch our activity if the user selects this notification 
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, LyricMain.class), 0);  
            // Set the info for the views that show in the notification panel. 
            notification.setLatestEventInfo(context, getText(R.string.media_player_label), mProxy.getTitle(), contentIntent); 
            return notification; 
        } 
    }); 
       mProxy.startAndBindService(); 
       mLyricEndList.clear(); 
        
       DisplayMetrics metrics = new DisplayMetrics(); 
       getWindowManager().getDefaultDisplay().getMetrics(metrics); 
                
       int btnId[] = {R.id.buttonPrev, R.id.buttonStop, R.id.buttonPlay, R.id.buttonPause, R.id.buttonNext}; 
       int btnSize = Math.min(metrics.widthPixels, metrics.heightPixels) / (btnId.length + 1); 
        
       //调整按键尺寸。 
       for(int i = 0; i < btnId.length; ++i){ 
        ImageButton ib = (ImageButton)this.findViewById(btnId[i]); 
        ib.setAdjustViewBounds(true); 
        ib.setMaxHeight(btnSize); 
        ib.setMaxWidth(btnSize); 
       } 
       ImageButton selectFile = (ImageButton)this.findViewById(R.id.buttonSelectFile); 
       selectFile.setAdjustViewBounds(true); 
       selectFile.setMaxHeight(btnSize*2/3); 
       selectFile.setMaxWidth(btnSize*2/3); 
     
       updateButtonState(); 
   }
再下来是onDestroy方法,如果音乐在播放中,就接触和播放服务之间的关系,退出程序,这是歌曲播放会继续。如果播放出于停止或暂停状态,就连同播放服务一起关闭,完全退出程序。
@Override
    protected void onDestroy() { 
        super.onDestroy(); 
        mProxy.setConnectionListener(null); 
        mProxy.setLyricPlayerListener(null); 
        if(!mProxy.isPlaying()){ 
            mProxy.stopService(); 
    &nbs

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,