解析M3U音乐链接文件

M3U本质上说不是音频文件,它是音频文件的列表文件,是纯文本文件。你下载下来打开它,播放软件并不是播放它,而是根据它的记录找到网络地址进行在线播放。

下面我们来解析m3u文件中的音乐网络地址:

一、界面如下:界面很简单,一个输入框(输入一个m3u文件链接),然后解析、开始和终止按钮。解析完成后,开始按钮获得焦点

播放从m3u文件解析的音乐


二、xml文件如下:

<RelativeLayout 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" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/m3uTextPath"
        />
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_marginTop="60dip"
        >
        
        <Button 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:id="@+id/parse"
        	android:onClick="mainCLick"
        	android:text="解析"
            />
        <Button 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:id="@+id/start"
        	android:onClick="mainCLick"
        	android:text="开始"
            />
        <Button 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:id="@+id/stop"
        	android:onClick="mainCLick"
        	android:text="终止"
            />
        <Button 
            android:layout_width="wrap_content"
        	android:layout_height="wrap_content"
        	android:id="@+id/pausePull"
        	android:onClick="mainCLick"
        	android:text="pull解析"
            />
    </LinearLayout>
    
    <!-- id为@id/android:list的ListView为客户化的list布局,如果没有,则系统会调用默认的布局 -->
	<ListView 
		android:id="@id/android:list" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:background="#00FF00"
		android:drawSelectorOnTop="false" 
		android:layout_marginTop="100dip"
		/>
	 
	<!-- 当ListView中没有数据时,id为@id/android:empty的view就会显示出来 -->
	<TextView 
		android:id="@id/android:empty" 
		android:layout_width="fill_parent"
		android:layout_height="wrap_content" 
		android:textColor="#FF0000"
		android:text="No data" 
		android:gravity="center_vertical|center_horizontal" 
		android:layout_marginTop="110dip"
		/>

</RelativeLayout>
三、activity

package com.example.m3upulltest;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import com.example.parseUtil.M3UParser;
import com.example.parseUtil.Person;
import com.example.parseUtil.netXmlPause;

import android.app.ListActivity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {
	/** 
	 * 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 
	 * 
	 */ 
//	private EditText m3uTextPath;
	private Button btnParse, btnPlay, btnStop;  
    
    private EditText editUrl;  
      
    private MediaPlayer player;  
      
    private List<String> pathList;  
      
    private int currPosition = 0; //记录当前播放的媒体文件的index  
	
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		editUrl = (EditText)findViewById(R.id.m3uTextPath);
		btnParse = (Button)this.findViewById(R.id.parse);  
        btnPlay = (Button)this.findViewById(R.id.start);  
        btnStop = (Button)this.findViewById(R.id.stop);  
          
        editUrl.setText("http://pubint.ic.llnwd.net/stream/pubint_kmfa.m3u");  
//      InputMethodManager imm = (InputMethodManager)this.getSystemService(INPUT_METHOD_SERVICE);   
        btnPlay.setEnabled(false);  
        btnStop.setEnabled(false);  
          
        player = new MediaPlayer();  
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {  
              
            public void onCompletion(MediaPlayer player) {  
                // 这个方法当MediaPlayer的play()执行完后触发   
                player.stop();  
                player.reset();  
                if(pathList.size() > currPosition+1){  
                    currPosition++; //转到下一首   
                    prepareToPlay();  
                }  
            }  
        });  
          
        player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {  
              
        	//缓冲完成执行
            public 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 block   
            e.printStackTrace();  
        } catch (IllegalStateException e) {  
            // TODO Auto-generated catch block   
            e.printStackTrace();  
        } catch (IOException e) {  
            // TODO Auto-generated catch block   
            e.printStackTrace();  
        }         
    }  
	/**
	 * 按钮事件
	 * @throws Exception 
	 */
	public void mainCLick(View v) throws Exception
	{
		switch (v.getId()) {
		case R.id.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.start:  
            //这里播放是从第一个开始   
            btnPlay.setEnabled(false);  
            btnParse.setEnabled(false);  
            this.currPosition = 0;  
            if(pathList != null && pathList.size() > 0){  
                  
                prepareToPlay();  
            }  
            break;  
            
        case R.id.stop:  
            player.pause();  
            btnPlay.setEnabled(true);  
            btnStop.setEnabled(false);  
            break;
            
      

		default:
			break;
		}
	}
	//菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
三、解析代码:

package com.example.parseUtil;

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;

import com.example.HTTPServer.HttpConnect;

public class M3UParser {
	/** 
     * 返回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; 
	}
	/** 
     * 从指定的Url进行解析,返回一个包含FilePath对象的列表 
     * FilePath封装每一个Audio路径。 
     * @param url 
     * @return 
     */  
    public static List<FilePath> parseFromUrl(String url)
    {  
        List<FilePath> resultList = null;  
        HttpResponse response = HttpConnect.getResponseFromUrl(url);  
        try {  
            if(response != null){  
                resultList = new ArrayList<M3UParser.FilePath>();  
                InputStream in = response.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;  
    } 
  //解析后的实体对象   
    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;  
        }  
    } 
    
}






  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值