MP3播放器1.0版本

news/2024/11/23 5:45:00/

实现了从服务器当中下载XML文件并且解析XML文件。

package guoximing.download;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;//看的出来 IOException 是MalformedURLException 的父类public class HttpDownloader {private URL url = null;public String download(String urlStr) {StringBuffer sb = new StringBuffer();String line = null;BufferedReader buffer = null;try {// 创建一个URL对象URL url = new URL(urlStr);// 创建一个Http连接HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();// 使用IO流读取数据buffer = new BufferedReader(new InputStreamReader(urlConn.getInputStream()));while ((line = buffer.readLine()) != null) {sb.append(line);}} catch (Exception e) {e.printStackTrace();} finally {try {buffer.close();} catch (Exception e) {e.printStackTrace();}}return sb.toString();}
}


package guoximing.mp3player;import guoximing.download.HttpDownloader;
import guoximing.model.Mp3Info;
import guoximing.xml.Mp3ListContentHandler;import java.io.StringReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleAdapter;import com.example.mp3player.R;@SuppressLint("NewApi")
public class MP3ListActivity extends ListActivity{private static final int UPDATE = 1;private static final int ABOUT  = 2;@Overrideprotected void onCreate(Bundle savedInstanceState) {//详见StrictMode文档StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites().detectNetwork()   // or .detectAll() for all detectable problems.penaltyLog().build());StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects().detectLeakedClosableObjects().penaltyLog().penaltyDeath().build());super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {menu.add(0,UPDATE,1,R.string.mp3List_update);menu.add(0,ABOUT,2,R.string.mp3List_about);return super.onCreateOptionsMenu(menu);}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {if(UPDATE == item.getItemId()){System.out.println(UPDATE);String str = "http://121.49.87.4:8080/mp3/resources.xml";String xml = downloaderXML(str);System.out.println("xml....."+xml);parse(xml);}else{System.out.println(ABOUT);}return super.onOptionsItemSelected(item);}private String downloaderXML(String urlStr){HttpDownloader httpDownloader = new HttpDownloader();String result = null;result = httpDownloader.download(urlStr);return result;}private List<Mp3Info> parse(String xmlStr){SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();List<Mp3Info> infos = new ArrayList<Mp3Info>();try{XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();Mp3ListContentHandler mp3ListContentHandler = new Mp3ListContentHandler(infos);xmlReader.setContentHandler(mp3ListContentHandler);xmlReader.parse(new InputSource(new StringReader(xmlStr)));for (Iterator iterator = infos.iterator(); iterator.hasNext();) {Mp3Info mp3Info = (Mp3Info) iterator.next();System.out.println(mp3Info);}}catch(Exception e){			}return infos;}
}


package guoximing.model;public class Mp3Info {private String id = null;private String mp3Name = null;private String mp3Size = null;private String lrcName = null;private String lrcSize = null;public Mp3Info(String id, String mp3Name, String mp3Size, String lrcName,String lrcSize) {super();this.id = id;this.mp3Name = mp3Name;this.mp3Size = mp3Size;this.lrcName = lrcName;this.lrcSize = lrcSize;}public Mp3Info() {super();}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getMp3Name() {return mp3Name;}public void setMp3Name(String mp3Name) {this.mp3Name = mp3Name;}public String getMp3Size() {return mp3Size;}public void setMp3Size(String mp3Size) {this.mp3Size = mp3Size;}public String getLrcName() {return lrcName;}public void setLrcName(String lrcName) {this.lrcName = lrcName;}public String getLrcSize() {return lrcSize;}public void setLrcSize(String lrcSize) {this.lrcSize = lrcSize;}@Overridepublic String toString() {return "Mp3Info [id=" + id + ", mp3Name=" + mp3Name + ", mp3Size="+ mp3Size + ", lrcName=" + lrcName + ", lrcSize=" + lrcSize+ "]";}}


package guoximing.xml;import guoximing.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{//每一个<resource>都是一个MP3Info对象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;}@Overridepublic 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);}super.characters(ch, start, length);}@Overridepublic void endDocument() throws SAXException {super.endDocument();}@Overridepublic void startDocument() throws SAXException {super.startDocument();}@Overridepublic void startElement(String uri, String localName, String qName,Attributes attributes) throws SAXException {this.tagName = localName;if(tagName.equals("resource")){mp3Info = new Mp3Info();}super.startElement(uri, localName, qName, attributes);}@Overridepublic void endElement(String uri, String localName, String qName)throws SAXException {if(qName.equals("resource")){infos.add(mp3Info);}tagName = "";super.endElement(uri, localName, qName);}}


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity" ><ListView android:id="@id/android:list" android:layout_width="fill_parent"android:layout_height="wrap_content" android:drawSelectorOnTop="false"android:scrollbars="vertical" /></LinearLayout>


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.mp3player"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="17" /><uses-permission android:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="guoximing.mp3player.MP3ListActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application></manifest>




http://www.ppmy.cn/news/357030.html

相关文章

mp3 播放器

转自&#xff1a; http://woshizn.javaeye.com/blog/605197  特此收藏&#xff01;&#xff01;&#xff01;&#xff01; "在信息时代&#xff0c;客观障碍已不复存在&#xff0c;所谓障碍都是主观上的。如果你想研发什么新的技术&#xff0c;你不需要几百万美元的资金…

Dewplayer MP3网页播放器

顺便提一下jMP3 &#xff08;javaScript MP3 player&#xff09;&#xff1a;http://www.sean-o.com/jquery/jmp3/ 官网&#xff1a;http://www.alsacreations.fr/dewplayer.html 开源 Dewplayer MP3网页播放器 音乐 Flash Dewplayer是一款不错的flash式MP3播放器[目前只支持M…

MP3播放器完全代码

主窗口代码:(JMFMedia.class) import java.applet.*;import java.awt.datatransfer.*;import java.awt.dnd.*;import java.net.*;import java.awt.*;import java.awt.event.*;import javax.swing.*;import javax.swing.Timer;import javax.media.*;import javax.swing.filechoo…

音乐播放器(4)--Mp3歌曲播放与控制

这一篇介绍一下使用java播放Mp3的&#xff0c;这里使用的是一个第三方包(jl.jar)&#xff0c;项目主页http://www.javazoom.net/index.shtml&#xff0c;有了这个包&#xff0c;我们便可以轻松的实现mp3播放了。 1 Mp3播放 首先定义一个播放器接口 package honest.playmusic.i…

HTML5音频(自定义mp3播放器源码)

audio对象 src兼容.ogg .wav .mp3 <audio controls srcdata/imooc.wav></audio> width autoplay loop muted静音 <audio controls srcdata/imooc.wav autoplay loop width500 height500 muted></audio> 播放play() var myAudio new Audio();myAudio.…

点击mp3链接时播放而不下载的问题.

用户点击mp3下载链接经过一次跳转,然后有的机子就提示让下载,有的就提示让播放. 后来找了N多方案均无法解决. 其中 Code System.IO.FileInfo DownloadFile new System.IO.FileInfo(Server.MapPath("test.mp3"));//别的服务器不行. System.Web.HttpContext.Cu…

mpg123播放器的安装与使用

本文非原创&#xff0c;是根据实际操作过程及其遇到的问题&#xff0c;百度后集合几个博主的文章而得&#xff0c;算是一个搬运或集成者&#xff01; 具体步骤如下&#xff1a; 1、找到安装包&#xff1a;http://www.mpg123.de/download/?V1&OD 2、解压&#xff1a; 2.1…

Android开发本地及网络Mp3音乐播放器(十七)已存在歌曲歌词下载

转载请注明出处&#xff1a; http://blog.csdn.net/iwanghang/article/details/51388896 觉得博文有用&#xff0c;请点赞&#xff0c;请留言&#xff0c;请关注&#xff0c;谢谢&#xff01;~ 实现功能&#xff1a; 已存在歌曲歌词下载 后续将博文&#xff0c;将实现已下载音…