Skip to content

Commit

Permalink
Merge pull request #156 from ZeusWPI/android/use-m3u-in-streaming
Browse files Browse the repository at this point in the history
Fix the stream to a m3u file (closes #126)
  • Loading branch information
TomNaessens committed Dec 24, 2013
2 parents 1541cb6 + da3ed73 commit 891a235
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;


Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Void, String> {

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);
}
}

0 comments on commit 891a235

Please sign in to comment.