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

2014-11-24 02:47:43 · 作者: · 浏览: 1
ds(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();
}
}
启动选择文件的SelectFileActivity
public void OnSelectFile(View v){
Intent i = new Intent(this, SelectFileActivity.class);
startActivityForResult(i, 0);
}
SelectFileActivity关闭,取得选中的媒体文件的信息并通知的LyricPlayerServiceProxy
接下来是按键处理
public void OnOperationButtonClick(View v){
switch(v.getId()){
case R.id.buttonPrev:
mProxy.seekToPrevLyric();
break;
case R.id.buttonStop:
if(mProxy.isPlaying() || mProxy.isPausing()){
mProxy.stop();
}
break;
case R.id.buttonPlay:
if(!mProxy.isPlaying()){
mProxy.start();
}
break;
case R.id.buttonPause:
if(mProxy.isPlaying()){
mProxy.pause();
}
break;
case R.id.buttonNext:
mProxy.seekToNextLyric();
break;
}
}
根据播放状态更新各个按键的状态。
protected void updateButtonState(){
((ImageButton)this.findViewById(R.id.buttonPrev)).setEnabled(mProxy.isPlaying() || mProxy.isPausing());
((ImageButton)this.findViewById(R.id.buttonStop)).setEnabled(mProxy.isPlaying() || mProxy.isPausing());
((ImageButton)this.findViewById(R.id.buttonPlay)).setEnabled(mProxy.getDataSource()!= null && (!mProxy.isPlaying() || mProxy.isPausing()));
((ImageButton)this.findViewById(R.id.buttonPause)).setEnabled(mProxy.isPlaying());
((ImageButton)this.findViewById(R.id.buttonNext)).setEnabled(mProxy.isPlaying() || mProxy.isPausing());
}
如果是程序启动时已经有歌曲在播放,就更新一下文件标题和按钮状态。
//implement of LyricPlayerServiceProxy.ServiceConnectionListener
public void onServiceConnected(){
String title = mProxy.getTitle();
if(title != null){
TextView tv = (TextView)this.findViewById(R.id.fileTitle);
tv.setText(title);
}
updateButtonState();
}
public void onServiceDisconnected(){

}
实现LyricPlayerListener的代码,负责处理歌词播放服务的各种通知。
//implement of LyricPlayerService.LyricPlayerListener
public void onLyricLoaded(){
mLyricEndList.clear();
String lyric = new String();
for(int i = 0; i < mProxy.getLyricCount(); ++i){
lyric += mProxy.getLyric(i);
lyric += "\r\n";
mLyricEndList.add(new Integer(lyric.length()));
}
mLyricEdit.setText(lyric);
}

public void onStateChanged(){
updateButtonState();
}

public void onPositionChanged(long position)