From da3ed7336565d7eba953ea33c2e21be5ab8a31d4 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Tue, 24 Dec 2013 23:24:49 +0100 Subject: [PATCH] Fix the stream to a m3u file (closes #126) --- .../hydra/util/audiostream/MusicService.java | 13 ++-- .../audiostream/ParseStreamAsyncTask.java | 65 +++++++++++++++++++ 2 files changed, 73 insertions(+), 5 deletions(-) create mode 100644 android/src/be/ugent/zeus/hydra/util/audiostream/ParseStreamAsyncTask.java diff --git a/android/src/be/ugent/zeus/hydra/util/audiostream/MusicService.java b/android/src/be/ugent/zeus/hydra/util/audiostream/MusicService.java index 995eb416..70af0a7b 100644 --- a/android/src/be/ugent/zeus/hydra/util/audiostream/MusicService.java +++ b/android/src/be/ugent/zeus/hydra/util/audiostream/MusicService.java @@ -53,7 +53,7 @@ public class MusicService extends Service implements OnCompletionListener, OnPre OnErrorListener, MusicFocusable { // Stream URL - final static String URL = "http://195.10.10.209/urgent/high.mp3"; + final static String URL = "http://urgent.stream.flumotion.com/urgent/high.mp3.m3u"; // The tag we put on debug messages final static String TAG = "Urgent.fm"; // These are the Intent actions that we are prepared to handle. Notice that the fact these @@ -339,22 +339,25 @@ void tryToGetAudioFocus() { } } + void playStream() { + new ParseStreamAsyncTask(this).execute(URL); + } + /** * Starts playing the next song. If manualUrl is null, the next song will be randomly selected * from our Media Retriever (that is, it will be a random song in the user's device). If * manualUrl is non-null, then it specifies the URL or path to the song that will be played * next. */ - void playStream() { + void playmp3(String mp3url) { mState = State.Stopped; relaxResources(false); // release everything except MediaPlayer - try { - + try { // set the source of the media player to a manual URL or path createMediaPlayerIfNeeded(); mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); - mPlayer.setDataSource(URL); + mPlayer.setDataSource(mp3url); mIsStreaming = true; diff --git a/android/src/be/ugent/zeus/hydra/util/audiostream/ParseStreamAsyncTask.java b/android/src/be/ugent/zeus/hydra/util/audiostream/ParseStreamAsyncTask.java new file mode 100644 index 00000000..ea8eb493 --- /dev/null +++ b/android/src/be/ugent/zeus/hydra/util/audiostream/ParseStreamAsyncTask.java @@ -0,0 +1,65 @@ +/* + * To change this template, choose Tools | Templates + * and open the template in the editor. + */ +package be.ugent.zeus.hydra.util.audiostream; + +import android.os.AsyncTask; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import java.net.MalformedURLException; +import java.net.URL; + +/** + * + * @author silox + */ +public class ParseStreamAsyncTask extends AsyncTask { + + MusicService musicService; + + public ParseStreamAsyncTask(MusicService musicService) { + this.musicService = musicService; + } + + @Override + protected String doInBackground(String... params) { + String line; + + try { + URL urlPage = new URL(params[0]); + HttpURLConnection connection = (HttpURLConnection) urlPage.openConnection(); + InputStream inputStream = connection.getInputStream(); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); + + StringBuilder stringBuffer = new StringBuilder(); + + while ((line = bufferedReader.readLine()) != null) { + if (line.contains("http")) { + connection.disconnect(); + bufferedReader.close(); + inputStream.close(); + return line; + } + stringBuffer.append(line); + } + + connection.disconnect(); + bufferedReader.close(); + inputStream.close(); + } catch (MalformedURLException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(String result) { + musicService.playmp3(result); + } +}