---
title: " MP3播放器项目---编写代码---5"
author: "xiaoyangh"
date: "2011-10-22T20:21:03Z"
tags: []
categories: ["移动开发"]
slug: "mp3-5"
draft: false
lastmod: "2011-10-22T20:21:03Z"
id: 1207844
url: "https://xiaoyangh.iteye.com/blog/1207844"
---

**MP3播放器项目---编写代码---5**

1.Mp3ListContentHandler

```
package hui.xml;

import hui.model.Mp3Info;

import java.util.List;


import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class Mp3ListContentHandler extends DefaultHandler {
	private List<Mp3Info> infos = null;
	private Mp3Info mp3Info = null;
	private String tagName = null;

	public Mp3ListContentHandler(List<Mp3Info> infos) {
		super();
		this.infos = infos;
	}

	public List<Mp3Info> getInfos() {
		return infos;
	}

	public void setInfos(List<Mp3Info> infos) {
		this.infos = infos;
	}

	@Override
	public void characters(char[] ch, int start, int length)
			throws SAXException {
		String temp = new String(ch, start, length);
		if (tagName.equals("id")) {
			mp3Info.setId(temp);
		} else if (tagName.equals("mp3.name")) {
			mp3Info.setMp3Name(temp);
		} else if (tagName.equals("mp3.size")) {
			mp3Info.setMp3Size(temp);
		} else if (tagName.equals("lrc.name")) {
			mp3Info.setLrcName(temp);
		} else if (tagName.equals("lrc.size")) {
			mp3Info.setLrcSize(temp);
		}
	}

	@Override
	public void endDocument() throws SAXException {
	}

	@Override
	public void endElement(String uri, String localName, String qName)
			throws SAXException {
		if (qName.equals("resource")) {
			infos.add(mp3Info);
		}
		tagName = "";

	}

	@Override
	public void startDocument() throws SAXException {
		// TODO Auto-generated method stub
		super.startDocument();
	}

	@Override
	public void startElement(String uri, String localName, String qName,
			Attributes attributes) throws SAXException {
		this.tagName = localName;
		if (tagName.equals("resource")) {
			mp3Info = new Mp3Info();
		}
	}

}
```

 

2.AppConstant

```
package hui.mp3player;

public interface AppConstant {
	public class PlayerMsg{
		public static final int PLAY_MSG = 1;
		public static final int PAUSE_MSG = 2;
		public static final int STOP_MSG = 3;
	}
	public class URL{
		public static final String BASE_URL = "http://192.168.1.2:8080/myapp/";
	}
}
```

 

3.DownloadService

package hui.mp3player;

import hui.download.HttpDownloader;  
import hui.model.Mp3Info;  
import hui.mp3player.AppConstant;  
import android.app.Service;  
import android.content.Intent;  
import android.os.IBinder;

public class DownloadService extends Service{

 @Override  
 public IBinder onBind(Intent intent) {  
  // TODO Auto-generated method stub  
  return null;  
 }

 //每次用户点击ListActivity当中的一个条目时，就会调用该方法  
 @Override  
 public int onStartCommand(Intent intent, int flags, int startId) {  
  // TODO Auto-generated method stub  
  //从Intent对象当中将Mp3Info对象取出  
  Mp3Info mp3Info = (Mp3Info)intent.getSerializableExtra("mp3Info");  
  //生成一个下载线程，并将Mp3Info对象作为参数传递到线程对象当中  
  System.out.print("service----------"+mp3Info);  
  DownloadThread downloadThread = new DownloadThread(mp3Info);  
  //启动新线程  
  Thread thread = new Thread(downloadThread);  
  thread.start();  
  return super.onStartCommand(intent, flags, startId);  
 }  
   
 class DownloadThread implements Runnable{  
  private Mp3Info mp3Info = null;  
  public DownloadThread(Mp3Info mp3Info){  
   this.mp3Info = mp3Info;  
  }  
  @Override  
  public void run() {  
   //下载地址[http://192.168.1.107:8081/mp3/a1.mp3](http://192.168.1.107:8081/mp3/a1.mp3)  
   //根据MP3文件的名字，生成下载地址  
   String mp3Url = AppConstant.URL.BASE\_URL + mp3Info.getMp3Name();  
   String lrcUrl = AppConstant.URL.BASE\_URL + mp3Info.getLrcName();  
   //生成下载文件所用的对象  
   HttpDownloader httpDownloader = new HttpDownloader();  
   //将文件下载下来，并存储到SDCard当中  
   int mp3Result = httpDownloader.downFile(mp3Url, "mp3/", mp3Info.getMp3Name());  
   int lrcResult = httpDownloader.downFile(lrcUrl, "mp3/", mp3Info.getLrcName());  
   /\*String resultMessage = null;  
   if(result == -1){  
    resultMessage = "下载失败";  
   }  
   else if(result == 0){  
    resultMessage = "文件已经存在，不需要重复下载";  
   }  
   else if(result == 1){  
    resultMessage = "文件下载成功";  
   }\*/  
   //使用Notification提示客户下载结果  
  }  
    
 }

}  
4.LocalMp3ListActivity

```
package hui.mp3player;

import hui.model.Mp3Info;
import hui.utils.FileUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import mars.mp3player.R;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class LocalMp3ListActivity extends ListActivity {
	private List<Mp3Info> mp3Infos = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.local_mp3_list);

	}

	@Override
	protected void onResume() {
		FileUtils fileUtils = new FileUtils();
		mp3Infos = fileUtils.getMp3Files("mp3/");
		List<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
		for (Iterator iterator = mp3Infos.iterator(); iterator.hasNext();) {
			Mp3Info mp3Info = (Mp3Info) iterator.next();
			HashMap<String, String> map = new HashMap<String, String>();
			map.put("mp3_name", mp3Info.getMp3Name());
			map.put("mp3_size", mp3Info.getMp3Size());
			list.add(map);
		}

		SimpleAdapter simpleAdapter = new SimpleAdapter(this, list,
				R.layout.mp3info_item, new String[] { "mp3_name", "mp3_size" },
				new int[] { R.id.mp3_name, R.id.mp3_size });
		setListAdapter(simpleAdapter);
		super.onResume();
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		if(mp3Infos != null){
			Mp3Info mp3Info = mp3Infos.get(position);
			Intent intent = new Intent();
			intent.putExtra("mp3Info", mp3Info);
			intent.setClass(this, PlayerActivity.class);
			startActivity(intent);
		}
	}

}
```

 

5.MainActivity

```
package hui.mp3player;

import mars.mp3player.R;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.TabHost;

public class MainActivity extends TabActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		
		//得到TabHost对象，正对TabActivity的操作通常都有这个对象完成
		TabHost tabHost = getTabHost();
		//生成一个Intent对象，该对象指向一个Activity
		Intent remoteIntent = new Intent();
		remoteIntent.setClass(this, Mp3ListActivity.class);
		//生成一个TabSpec对象，这个对象代表了一个页
		TabHost.TabSpec remoteSpec = tabHost.newTabSpec("Remote");
		Resources res = getResources();
		//设置该页的indicator
		remoteSpec.setIndicator("Remote", res.getDrawable(android.R.drawable.stat_sys_download));
		//设置该页的内容
		remoteSpec.setContent(remoteIntent);
		//将设置好的TabSpec对象添加到TabHost当中
		tabHost.addTab(remoteSpec);
		
		Intent localIntent = new Intent();
		localIntent.setClass(this, LocalMp3ListActivity.class);
		TabHost.TabSpec localSpec = tabHost.newTabSpec("Local");
		localSpec.setIndicator("Local", res.getDrawable(android.R.drawable.stat_sys_upload));
		localSpec.setContent(localIntent);
		tabHost.addTab(localSpec);
	}
}
```