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 =new  File(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();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM