I know Android's MediaPlayer
is a great thing. It allows us to play local files as well as media streams. And it is quite easy to use (just for example):
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource("http://streaming.shoutcast.com/80sPlanet"); // this steam broadcasts audio/mpeg
mediaPlayer.prepareAsync();
mediaPlayer.start();
It is possible to set different types of DataSource by calling the overloaded setDataSource()
with different set of parameters.
And there is an interesting prototype of this function:
void setDataSource(MediaDataSource dataSource)
Looks like it is possible to completely override the DataSource
with your own implementation. And it works indeed:
import android.media.MediaDataSource;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.net.URL;
import java.net.HttpURLConnection;
public class UrlMediaDataSource extends MediaDataSource {
URL url;
HttpURLConnection connection;
BufferedInputStream stream;
public UrlMediaDataSource(URL url) throws IOException {
this.url = url;
connection = (HttpURLConnection) url.openConnection();
}
@Override
public long getSize() {
return 0;
}
@Override
public int readAt(long position, byte[] buffer, int offset, int size) throws IOException {
if (stream == null)
stream = new BufferedInputStream(connection.getInputStream());
return stream.read(buffer, offset, size);
}
@Override
public void close() throws IOException {
stream.close();
stream = null;
connection.disconnect();
connection = null;
}
}
and in the main code:
UrlMediaDataSource dataSource = new UrlMediaDataSource(new URL("http://streaming.shoutcast.com/80sPlanet"));
mediaPlayer.setDataSource(dataSource);
Yes, this works fine. But if I try audio/aacp broadcasting stream (for example: "http://111.223.51.8:8005" - it is "COOLfahrenheit 93" radio), the player does not play. Logcat trace:
06-07 23:26:01.680 1352-1147/? E/GenericSource: Failed to init from data source!
06-07 23:26:01.681 1352-1093/? D/NuPlayerDriver: notifyListener_l(0xf3e051e0), (100, 1, -2147483648)
06-07 23:26:01.735 1352-2013/? D/NuPlayerDriver: reset(0xf3e051e0)
06-07 23:26:01.735 1352-2013/? D/NuPlayerDriver: notifyListener_l(0xf3e051e0), (8, 0, 0)
06-07 23:26:01.736 1352-1093/? D/NuPlayerDriver: notifyResetComplete(0xf3e051e0)
Though, the URL works fine (the music plays) when no custom MediaDataSource
is used:
mediaPlayer.setDataSource("http://111.223.51.8:8005");
Does anybody know the right way to manage this?
Just don't propose me to use the URL
directly - I need a custom MediaDataSource
to get access to the raw data of the stream.
See Question&Answers more detail:
os