`
nightkidzxc
  • 浏览: 10962 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
文章分类
社区版块
存档分类
最新评论

android基础学习笔记--MP3播放器开发

 
阅读更多

功能点1:从服务器上读取配置的resources.xml文件获得MP3具体信息来进行列表的更新

步骤一.从服务器读取到xml文件

步骤二.对读取来的信息进行解析

步骤三.点击菜单项把解析后的数据显示在界面上

步骤四.给Mp3ListActivity添加菜单项



步骤一.从服务器读取到xml文件,在Manifest,xml中加入<uses-permission>中允许访问网络

URl url=new URL("http://192.168.2.33/webapps/zxcmp3player/resources.xml");

HttpURLConnection urlConn=(HttpURLConnection)url.openConnection();

BufferedReader br=new BufferedReader(new InputStreamReader(urlConn.getInputStream()));

while((line=br.readLine())!=null) //String line=null;

sb.append(line); //StringBuffer sb=new StringBuffer();

最后返回sb,这样,xml文件中的信息就存在了StringBuffer sb中了


步骤二.解析读到的xml信息

解析xml文件需要用到SAX,而其中XMLReader进行Parse()解析之前需要设置一个DefaultHandler来申明解析规则

SAXParserFactory sf=new SAXParserFactory().newInstance();

XMLReader xr=sf.newSAXParser().getXMLReader();


class Mp3ListHandler extends DefaultHandler(){

重写startElement(String uri, String localName, String qName,Attributes attributes)

判断如果localNameequals("resource") 创建一个mp3对象,tagName=localName //String tagName

重写characters(char[] ch, int start, int length)

判断如果if(tagName.equals("id")) mp3Info.id=new String(ch,start,length);等依次设置属性

重写endElement(String uri, String localName, String qName)方法

判断如果if(qName.equals("resource")) mp3list.add(mp3Info); //List<mp3Info>=new ArrayList<mp3Info>();

}

List<mp3Info> infos=new ArrayList<mp3Info>();

xr.setContentHandler(new Mp3ListHandler(infos)); //把空的队列infos传入ContentHandler构造函数中,解析时向队列中添加mp3对象

xr.parse(new InputSource(new StringReader(sb))); //这样,解析完成后,infos队列中就是所要的带有信息的MP3对象


步骤三.把解析后的数据显示在界面上
把得到的List<mp3info> infos对象中的信息转化为SimpleAdapter对象所需要的数据源list

List<HashMap<String,String>> list=new ArrayList<HashMap<String,String>>();

for(Mp3Info mp3Info:infos){

HashMap<String,String> map=new HashMap<String,String>();

map.putString("mp3NameKey",mp3Info.getId());

map.putString("mp3SizeKey",mp3Info.getSize());

list.add(map);

}

写一个ListView每一项需要的layout,比如叫mp3_item,其中包含两个TextView mp3Name和mp3Size

SimpleAdapter adapter=new SimpleAdapter(Mp3ListActivity.this,list,R.layout.mp3_item,new String[]{"mp3NameKey","mp3SizeKey"},new int[] {R.id.mp3Name,R.id.mp3Size});

Mp3ListActivity.setListAdapter(adapter);

步骤四.为主Activity添加菜单选项

复写onCreateOptionMenu()方法其中用 menu.add(0,1,1,"更细列表")添加菜单项

复写onOptionsItemSelected()事件方法,通过点击item.getId()来执行上面三步操作更新列表


功能点2:从服务器上下载mp3文件到手机

步骤一.点击相应的item项触发事件开始下载
步骤二.从服务器上读取文件信息
步骤三.把读取的信息存储成文件在手机SD卡上
步骤四.通知客户下载结果

步骤一.点击相应的item项触发事件开始下载
重写onListItemClick事件,通过点击的item的position在之前的infos中获得Mp3Info对象

步骤二.从服务器上读取文件信息

a)创建一个Service去下载文件,在Service中再开一个线程去下载以免应用阻塞

创建一个Service,当要启动的Service和Activity不在同一个包下时,并且在Manifest中注册时需要把Service名填完整,包括前面的包名。
在新建的Service类中添加内部类class blablaThread implements Runnable 实现run()方法,在intent中putExtra的时候选(Sring,Serilizabal)序列化在run()方法中用一个intent启动Service,并且把Mp3Info对象后加上implements Serilizable 这样Mp3Info对象就能序列化传入键值了

b)在新建的Service中的onStartCommand方法中启动线程在SD卡上建立文件

String SDCardRoot=Environment.getExternalStorageDirectory().getAbsolutePath();
File file=new File(SDCardRoot+Path);
file.makedir();
file =newFile(SDCardRoot+File.seperator+Path);
file.createnewFile();
OutputStream output=new OutputStream(file);
byte buffer[]=new byte[4*1024];
int temp;
while((temp=input.read(buffer))!=-1)
output.write(buffer,0,temp);

c)通知用户下载结果

用一个notification来通知用户下载结果
//获得NotificationManager对象
NotificationManager nm=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
//新建notification对象
Notification notification=new Notification(R.drawable.icon,"状态栏上通知的内容",System.currentTimeMillis());
//notification.flags=Notification.FLAG_AUTO_CANCEL; //点击通知后自动消失
//建立点击notification对象所要启动的intent
Intent intent=new Intent(DownloadService.this,mp3ListActivity.class);
PendingIntent pi=PendingIntent.getActivity(DownloadService.this,0,intent,0);
//设置状态栏下拉后的通知的属性
notification.setLatestEventInfo(getApplicationContext(),"通知的标题","通知的内容",pi);
nm.notify(1,notification);

功能点3:讲MP3列表分为远程服务器MP3列表和本地MP3列表

a)用一个TabActivity将两个列表加入其中

写一个MainActivity继承TabActivity,设置其xml文件为固定格式(TabActivity比较固定xml格式),在其onCreate方法中
TabHost tabHost=getTabHost(); //获得TabHost对象,一般对TabActivity的操作都是通过TabHost来进行

TabHost.TabSpec remoteSpec=tabHost.newTabSpec("Remote"); //生成一个TabActivity的一页的对象
remoteSpec.setIndicator("Remote",R.drawable.icon1); //可以说是设置点击图标的文字和样式
//也可以Resources rs=getResources();rs.getDrawable(android.drawable.xx);使用系统图标
Intent remoteIntent=new Intent(this,Mp3ListActivity.class); //设置第一页点击要跳转的Intent
remoteSpec.setContent(remoteIntent); //设置激发点击出现的页面的intent
tabHost.addSpec(remoteSpec);

TabHost.TabSpec localSpec=tabHost.newTabSpec("Local");
localSpec.setIndicator("Local",R.drawable.icon2);
Intent localIntent=new Intent(this,LocalMp3ListActivity.class);
localSpec.setContent(localIntent);
tabHost.addSpec(localSpec);

b)写LocanMp3ListActivity,读取目录中的MP3文件的名字和大小并显示

FIle dir=new File(Path);//指定路径
File[] files=dir.listFiles(); //返回指定路径下所有文件列表
for(File fileItem:files){
Mp3Info mp3=new Mp3Info();
mp3.setMp3Name=fileItem.getName();
mp3.setMp3Size=fileItem.length()+""; //加上""把long类型转换为String
list.add(mp3);
}
得到了List<Mp3Info> list,设置ListAdapter

功能点4:MP3文件的播放,暂停,停止

把localMp3ListActivity的item点击事件指向执行播放Activity
在Mp3PlayActivity中添加三个ImageButton,分别实现播放,暂停和停止功能
MediaPlayer mediaPlayer=null;
设置相应imageButton的onClickListener 进行
mediaPlayer=MediaPlayer.create(Context,Uri.parse("file://"+SDCardRoot+"mp3/"+mp3.getMp3Name()));
mediaPlayer.start() mediaPlayer.pause() mediaPlayer.stop() mediaPlayer.release();
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics