Parse M3U music link file and m3u Link

Source: Internet
Author: User

Parse M3U music link file and m3u Link

M3U is essentially not an audio file. It is a list file of audio files and a plain text file. You can download it and open it. The playback software does not play it, but finds the network address based on its records for online playback.

Next we will parse the music network address in the m3u File:

1. The interface is as follows: the interface is very simple. An input box (enter a m3u File Link), and then the resolution, start, and end buttons. After resolution is complete, start button to get focus

Play the music parsed from the m3u File


II. the xml file is as follows:

<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/plugin" android: paddingRight = "@ dimen/plugin" android: paddingTop = "@ dimen/activity_v Ertical_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" Droid: id = "@ + id/parse" android: onClick = "mainCLick" android: text = "resolution"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/start" android: onClick = "mainCLick" android: text = "start"/> <Button android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/stop" android: onClick = "mainCLick" android: text = "Terminate"/> <Button android: layout _ Width = "wrap_content" android: layout_height = "wrap_content" android: id = "@ + id/pausePull" android: onClick = "mainCLick" android: text = "pull Parsing"/> </LinearLayout> <! -- ListView with id @ id/android: list is a custom list layout. If no, the system calls the default layout --> <ListView android: id = "@ id/android: list" android: layout_width = "fill_parent" android: layout_height = "wrap_content" android: background = "#00FF00" android: drawselectid Top = "false" android: layout_marginTop = "100dip"/> <! -- If the ListView contains no data, the view with the id @ id/android: empty is displayed. --> <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>
3. 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 androi D. widget. arrayAdapter; import android. widget. button; import android. widget. editText; import android. widget. listAdapter; import android. widget. toast; public class MainActivity extends ListActivity {/*** Android supports playing Audio * on the network to access Audio on the network, we need to obtain the audio stream through Http * This may involve the ICY protocol. ICY extends the Http protocol * However, sites on the network, we often do not allow direct access to the audio stream * we need an intermediate file to point to the address of the audio stream we need, so that third-party software can play back. * For an ICY stream, It is a PLS file or an M3U File * the MIME type corresponding to PLS is: audio/x-scpls * the MIME type corresponding to M3U is: audio/x-mpegurl ** although Android supports ICy streams, it does not provide a ready-made method to parse M3U or PLS files, in order to play the audio streams on the network, we need to resolve these files by ourselves * The M3U File is actually an index file of the audio stream, which points to the path of the audio stream to be played. * @ Author Administrator ** // private EditText m3uTextPath; private Button btnParse, btnPlay, btnStop; private EditText editUrl; private MediaPlayer player; private List <String> pathList; private int currPosition = 0; // record the index protected void onCreate (Bundle savedInstanceState) of the currently played media file {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); editUrl = (EditText) findViewById (R. I D. 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 MediaPla Yer (); player. setOnCompletionListener (new MediaPlayer. onCompletionListener () {public void onCompletion (MediaPlayer player) {// This method triggers the player after the play () of the MediaPlayer is executed. stop (); player. reset (); if (pathList. size ()> currPosition + 1) {currPosition ++; // go to the next prepareToPlay () ;}}); player. setOnPreparedListener (new MediaPlayer. onPreparedListener () {// buffer completed execution public void onPrepared (MediaPlayer arg0 ){// This method triggers btnStop after the MediaPlayer prepare () is executed. setEnabled (true); player. start (); // after playing a piece of video, execute the onCompletionListener onCompletion method});}/*** play music */private void prepareToPlay () {try {// after obtaining the path of the current audio stream, we need to set it through setDataSource of MediaPlayer, and then call prepareAsync () to load String path = pathList in cache. get (currPosition); player. setDataSource (path); // prepareAsync is used because the method is asynchronous, because access to the audio stream is a network operation, which takes a long time to buffer and prepare for playback, in this way, a card may appear on the user interface. // After the method is executed, the onPreparedListener onPrepared () method is executed. Player. prepareAsync (); // asynchronous thread to avoid getting stuck} 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 () ;}/ *** button event * @ throws Exception */public void mainCLick (View v) throws Exception {switch (v. getI D () {case R. id. parse: // complete resolution // progress = ProgressDialog. show (this, "prompt", "parsing, please wait... "); // 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, "enter the correct M3U File Access address", Toast. LENGTH_LONG ). show ();} break; case R. id. start: // here, the playback starts from the first btnPlay. setEnabled (false); btnParse. setEnabled (false); this. c UrrPosition = 0; if (pathList! = Null & pathList. size ()> 0) {prepareToPlay ();} break; case R. id. stop: player. pause (); btnPlay. setEnabled (true); btnStop. setEnabled (false); break; default: break; }}// Menu public boolean onCreateOptionsMenu (menu) {// Inflate the Menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
Iii. parsing code:

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 {/*** return List <String> type * @ 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 ("#") {// here is the Metadata information} else if (line. length ()> 0 & line. startsWith ("http: //") {// specifies the audio stream path resultList. add (line) ;}} in. close () ;}} catch (Exception e) {e. printStackTrace ();} return resultList;}/*** parse from the specified Url and return a list containing FilePath objects * FilePath encapsulates every Audio path. * @ 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 ("#") {// here is the Metadata information} else if (line. length ()> 0 & line. startsWith ("http: //") {// here is a directed audio stream path FilePath filePath = new FilePath (line); resultList. add (filePath) ;}} in. close () ;}} catch (Exception e) {e. printStackTrace ();} return resultList;} // The resolved object 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 ;}}}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.