|
程序的结构:Activity+Service,前者负责界面交,后者负责对音乐文件的各种操作,它们之间通过broadcast和broadcastReceive来进行通讯。
1.读取歌词文件,放入到一个ArrayList里,生成adapter,给listView注入数据。
2.点击按钮或移动seekbar滑块后,通过broadcast给service发送信息,service收到信息,对音乐文件进行操作。
3.启动一个线程,向service发送broadcast来询问歌曲的播放状态和播放得进度,receiver接收返回的broadcast来刷新listview ,seekbar,现在播放到的时间等控件的显示值。
相应的代码:
package com.andy.media;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.util.EncodingUtils;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageButton;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.SeekBar.OnSeekBarChangeListener;
public class AndyMediaPlayer extends Activity implements OnClickListener {
private ImageButton back;
private ImageButton forward;
private ImageButton previous;
private ImageButton next;
private ImageButton play;
private ImageButton finish;
private SeekBar seekBar;
private ListView listview;
private TextView status;
private TextView currentTime;
private TextView totalTime;
private boolean isPlaying = false;
private ArrayList fileList = new ArrayList();
private int currentIndex;
private boolean loop;
private int fileId;
private String singer;
private String album;
private ArrayList timeList = new ArrayList();
private ArrayList<HashMap> lrcContent = new ArrayList<HashMap>();
private int currentRow;
private int totalTimeData;
BroadcastReceiver recevier;
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initUI();
recevier = new BroadcastReceiver(){
@Override
public void onReceive(Context context, Intent intent) {
Bundle bd = intent.getBundleExtra("refresh");
if(bd != null){
int p = bd.getInt("progress");
seekBar.setProgress(p);
int mms = bd.getInt("mms");
int total = bd.getInt("total");
int m = (int)mms/(1000*60);
int s =( mms-m*1000*60)/1000;
String mm = m+":"+s;
Log.i("recvF",mm);
currentTime.setText(mm);
int status1 = bd.getInt("status");
if(status1==1){
status.setText(AndyMediaPlayer.this.getResources().getString(R.string.playing));
}else{
status.setText(AndyMediaPlayer.this.getResources().getString(R.string.pausePlaying));
}
currentRow=0;
String ts = (String) lrcContent.get(currentRow).get("time");
int tsint = Integer.parseInt(ts);
while(tsint<mms){
if(currentRow>=lrcContent.size()-1) break;
ts = (String) lrcContent.get(currentRow+1).get("time");
tsint = Integer.parseInt(ts);
if(tsint<mms){
currentRow++;
}
}
Log.i("cccur",currentRow+"");
System.out.println(listview.getChildCount());
int row = currentRow % listview.getChildCount();
listview.setSelection(currentRow);
// if(row<listview.getChildCount()){
// LinearLayout v=null;
// if(row-1>=0){
// v = (LinearLayout)listview.getChildAt(row-1);
// System.out.println(v.getChildAt(0).getClass());
// ((TextView)(v.getChildAt(0))).setTextColor(Color.WHITE);
// }
//
// v = (LinearLayout)listview.getChildAt(row);
// System.out.println(v.getChildAt(0).getClass());
// ((TextView)(v.getChildAt(0))).setTextColor(Color.RED);
// //v.setBackgroundColor(R.color.blue);
// v.setFocusable(true);
// v.setSelected(true);
// listview.setSelection(currentRow);
// }
//listview.getAdapter().getView(currentIndex, null, listview).setBackgroundColor(R.color.blue);
totalTimeData = total;
m = (int)total/(1000*60);
s =( total-m*1000*60)/1000;
mm = m+":"+s;
totalTime.setText(mm);
}
}
};
IntentFilter filter = new IntentFilter();
filter.addAction("com.andy.fromService");
this.registerReceiver(recevier, filter);
startService();
new Thread(){
public void run(){
while(true){
Intent intent = new Intent("com.andy.toService");
Bundle bd = new Bundle();
bd.putInt("command", 10);
intent.putExtra("info", bd);
sendBroadcast(intent);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {}
}
}
}.start();
initFile();
SimpleAdapter adapter = new SimpleAdapter(this, (List<? extends Map<String, ?>>) lrcContent, R.layout.lrclist,
new String[]{"content"}, new int[]{R.id.lrccontent});
listview.setAdapter(adapter);
}
public void startService(){
Intent intent = new Intent(this,AudioPlayerService.class);
Bundle bundel = new Bundle();
//if(!fileList.isEmpty()& |
|