首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

(转)Android多媒体学习七:访问网络下的Audio对应的M3U文件,实现网络音频流的播放

2012-09-28 
(转)Android多媒体学习七:访问网络上的Audio对应的M3U文件,实现网络音频流的播放package demo.cameraimpo

(转)Android多媒体学习七:访问网络上的Audio对应的M3U文件,实现网络音频流的播放
package demo.camera;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import org.apache.http.HttpResponse;import org.apache.http.HttpStatus;import org.apache.http.client.HttpClient;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import android.util.Log;/** * 给类提供访问网络的方法 * @author Administrator * */public final class HttpConnect {/** * 利用HttpClient获取指定的Url对应的HttpResponse对象 * @param url * @return */public static HttpResponse getResponseFromUrl(String url){try {HttpClient client = new DefaultHttpClient();HttpGet get = new HttpGet(url);Log.v("URI : ", get.getURI().toString());HttpResponse response = client.execute(get);if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){return response;}} catch (Exception e) {// TODO: handle exceptione.printStackTrace();}return null;}/** * 利用HttpClient获取指定Url对应的字符串对象 * @param url * @return */public static String getStringFromUrl(String url){try {StringBuilder result = new StringBuilder();HttpResponse res = HttpConnect.getResponseFromUrl(url);if(res != null){InputStream is = res.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(is));String line = "";while((line = reader.readLine()) != null){result.append(line);}is.close();return result.toString();}} catch (Exception e) {// TODO: handle exception}return null;}}?

2、M3UParser类:解析M3U文件

?

?

package demo.camera;import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.util.ArrayList;import java.util.List;import org.apache.http.HttpResponse;/** * 该类提供对M3U文件的解析 * @author Administrator * */public final class M3UParser {/** * 从指定的Url进行解析,返回一个包含FilePath对象的列表 * FilePath封装每一个Audio路径。 * @param url * @return */public static List<FilePath> parseFromUrl(String url){List<FilePath> resultList = null;HttpResponse res = HttpConnect.getResponseFromUrl(url);try {if(res != null){resultList = new ArrayList<M3UParser.FilePath>();InputStream in = res.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(in));String line = "";while((line = reader.readLine()) != null){if(line.startsWith("#")){//这里是Metadata信息}else if(line.length() > 0 && line.startsWith("http://")){//这里是一个指向的音频流路径FilePath filePath = new FilePath(line);resultList.add(filePath);}}in.close();}} catch (Exception e) {e.printStackTrace();}return resultList;}/** * 返回List<String>类型 * @param url * @return */public static List<String> parseStringFromUrl(String url){List<String> resultList = null;HttpResponse res = HttpConnect.getResponseFromUrl(url);try {if(res != null){resultList = new ArrayList<String>();InputStream in = res.getEntity().getContent();BufferedReader reader = new BufferedReader(new InputStreamReader(in));String line = "";while((line = reader.readLine()) != null){if(line.startsWith("#")){//这里是Metadata信息}else if(line.length() > 0 && line.startsWith("http://")){//这里是一个指向的音频流路径resultList.add(line);}}in.close();}} catch (Exception e) {e.printStackTrace();}return resultList;}//解析后的实体对象static class FilePath{private String filePath;public FilePath(String filePath){this.filePath = filePath;}public String getFilePath() {return filePath;}public void setFilePath(String filePath) {this.filePath = filePath;}}}

?

?

?

3、InternetAudioDemo类:显示解析列表吗,并实现播放

?

package demo.camera;import java.io.IOException;import java.util.List;import demo.camera.M3UParser.FilePath;import android.app.Activity;import android.app.ListActivity;import android.app.ProgressDialog;import android.media.MediaPlayer;import android.os.Bundle;import android.view.View;import android.view.inputmethod.InputMethodManager;import android.widget.ArrayAdapter;import android.widget.Button;import android.widget.EditText;import android.widget.ListAdapter;import android.widget.Toast;/** * Android支持播放网络上的Audio * 访问网络上的Audio,我们通过Http需要获取音频流 * 这可能要涉及到ICY协议。ICY对Http协议进行了扩展 * 然而,网络上的站点,往往并不允许我们直接访问其音频流 * 我们需要一种中间文件来指向我们需要的音频流的地址,以使第三方的软件可以播放。 * 对于ICY流来说,其就是一个PLS文件或者一个M3U文件 * PLS对应的MIME类型为:audio/x-scpls * M3U对应的MIME类型为:audio/x-mpegurl *  * 虽然Android提供了对ICy流的支持,但是其并没有提供现成的方法来解析M3U或PLS文件 * 所以,为了播放网络上的音频流,我们需要自己实现这些文件的解析 * M3U文件其实就是一个音频流的索引文件,他指向要播放的音频流的路径。 * @author Administrator * */public class InternetAudioDemo extends ListActivity {private Button btnParse, btnPlay, btnStop;private EditText editUrl;private MediaPlayer player;private List<String> pathList;private int currPosition = 0; //记录当前播放的媒体文件的index//private ProgressDialog progress;public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.internet_audio);btnParse = (Button)this.findViewById(R.id.btn_parse);btnPlay = (Button)this.findViewById(R.id.btn_start);btnStop = (Button)this.findViewById(R.id.btn_end);editUrl = (EditText)this.findViewById(R.id.edit_url);editUrl.setText("http://pubint.ic.llnwd.net/stream/pubint_kmfa.m3u");//InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);//imm.showSoftInput(editUrl, InputMethodManager.SHOW_IMPLICIT);btnPlay.setEnabled(false);btnStop.setEnabled(false);player = new MediaPlayer();player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {@Overridepublic void onCompletion(MediaPlayer player) {// 这个方法当MediaPlayer的play()执行完后触发player.stop();player.reset();if(pathList.size() > currPosition+1){currPosition++; //转到下一首prepareToPlay();}}});player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {@Overridepublic void onPrepared(MediaPlayer arg0) {// 这个方法当MediaPlayer的prepare()执行完后触发btnStop.setEnabled(true);player.start();//当一曲播放完后,执行onCompletionListener的onCompletion方法}});}private void prepareToPlay(){try {//获取当前音频流的路径后我们需要通过MediaPlayer的setDataSource来设置,然后调用prepareAsync()来完成缓存加载String path = pathList.get(currPosition);player.setDataSource(path);//之所以使用prepareAsync是因为该方法是异步的,因为访问音频流是网络操作,在缓冲和准备播放时需要花费//较长的时间,这样用户界面就可能出现卡死的现象//该方法执行完成后,会执行onPreparedListener的onPrepared()方法。player.prepareAsync();} catch (IllegalArgumentException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IllegalStateException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}public void onClick(View v){int id = v.getId();switch(id){case R.id.btn_parse://完成解析//progress = ProgressDialog.show(this, "提示", "正在解析,请稍后...");//progress.show();String url = null;if(editUrl.getText() != null){url = editUrl.getText().toString();}if(url != null && !url.trim().equals("")){pathList = M3UParser.parseStringFromUrl(url);ListAdapter adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, pathList);this.setListAdapter(adapter);btnPlay.setEnabled(true);}else{Toast.makeText(this, "请输入正确的M3U文件访问地址", Toast.LENGTH_LONG).show();}break;case R.id.btn_start://这里播放是从第一个开始btnPlay.setEnabled(false);btnParse.setEnabled(false);this.currPosition = 0;if(pathList != null && pathList.size() > 0){prepareToPlay();}break;case R.id.btn_end:player.pause();btnPlay.setEnabled(true);btnStop.setEnabled(false);break;default:break;}}}

?

?

4、需要在清单文件中加入INTERNET权限。

?

?

转自chenjie19891104的的博客(http://blog.csdn.net/chenjie19891104/article/category/756236),以

便以后学习和查询!


?

?

热点排行