Streaming with Android MediaPlayer - catching errors and buffering
I'm having trouble getting MediaPlayer
to be resilient when streaming from a HTTP URL.
If I start playing the file, but then drop the connection (e.g. airplane mode), MediaPlayer#OnErrorListener
generates what=1, extra=-17
and then shortly afterwards what=-38, extra=0
.
There's no documentation I can see in the APIs of what this denotes, except extra is "Typically implementation dependant". I'm using a HTC Hero (well, it's T-Mobile UK's G2 Touch).
Do other people get the same values and is it safe to catch these values as meaning the connection's gone?
How can I best resume when the connection reappears? (save the current seek to preferences, and retry every 5 seconds?)
How do I know when the device has decided to start playing what it's been buffering - is there a callback (other than polling isPlaying()
)?
Additionally, I'm not entirely sure what onBufferingUpdate
provides. I'm using a 40 minute podcast MP3 (64kbps bitrate)
- buffering goes 1%, 2%, 3%. When I seek to about 30 mins in, it shows 75%, then when I seek back to the start back to 5% - what is the point of this callback other than showing approximately what is cached?
Finally - is there any way to pipe what's streamed to an MP3
?
Asked by: Blake509 | Posted: 24-01-2022
Answer 1
Taken from This similar stack Overflow Question
I too am disappointed about the MEDIA_INFO_BUFFERING_START and MEDIA_INFO_BUFFERING_END hooks... bummer.
Answered by: Patrick576 | Posted: 25-02-2022I checked out the Pandora app and it doesn't display a buffering indicator. When music is interrupted for buffering, it just sits there as if nothing happened and the UI looks like it's still playing. So I've come to the conclusion that if you're using MediaPlayer, it's just not possible to determine if the track is temporarily paused for buffering.
However, I did notice that there are a couple MediaPlayer constants that could be of use: MEDIA_INFO_BUFFERING_START and MEDIA_INFO_BUFFERING_END. But they're only available in API level 9+, and the docs don't say anything about them. I'm assuming they can be used with an OnInfoListener.
I'm disappointed, but at least I can stop spinning my wheels now and move on to something else.
Answer 2
It is possible to do this
MediaPlayer has methods to register a OnPreparedListener and a OnBufferingUpdateListener
onPrepared will be called once the player has buffered enough to start playing.
http://developer.android.com/reference/android/media/MediaPlayer.OnPreparedListener.html
onBufferingUpdate will be called to update you on buffering information.
http://developer.android.com/reference/android/media/MediaPlayer.OnBufferingUpdateListener.html
You should also use the Connectivity service to listen for network connectivity
public boolean hasConnectivity()
{
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = connectivityManager.getActiveNetworkInfo();
int netType = info.getType();
int netSubtype = info.getSubtype();
if (netType == ConnectivityManager.TYPE_WIFI)
{
return info.isConnected();
}
else if (netType == ConnectivityManager.TYPE_MOBILE && netSubtype == TelephonyManager.NETWORK_TYPE_UMTS)
{
return info.isConnected();
}
return false;
}
Answered by: Daryl758 | Posted: 25-02-2022
Answer 3
I think you should listne for loss/gain of network. Have a look at: Detect 3G or Wifi Network restoration
Answered by: Sienna689 | Posted: 25-02-2022Answer 4
When you seek or skip or the connection is lost and MediaPlayer keeps reconnecting to the proxy server, you must send this response with Status 206 after you get the request and range(int) from the client.
String headers += "HTTP/1.1 206 Partial Content\r\n";
headers += "Content-Type: audio/mpeg\r\n";
headers += "Accept-Ranges: bytes\r\n";
headers += "Content-Length: " + (fileSize-range) + "\r\n";
headers += "Content-Range: bytes "+range + "-" + fileSize + "/*\r\n";
headers += "\r\n";
And when you receive a request from MediaPlayer that does not contain Range in the HTTP header , then it is requesting a new stream file, in this case your response header should look like this:
String headers = "HTTP/1.1 200 OK\r\n";
headers += "Content-Type: audio/mpeg\r\n";
headers += "Accept-Ranges: bytes\r\n";
headers += "Content-Length: " + fileSize + "\r\n";
headers += "\r\n";
Enjoy!
Answered by: Miranda333 | Posted: 25-02-2022Similar questions
video streaming - How to speed up MediaPlayer buffering on Android
Hi I'm developing an application that uses Android MediaPlayer to play a background video usually of 30 seconds length.
With some videos that are less than 5-7MB the Player starts immediately, but when the video size goes over 10-15MB the video takes over 30 seconds before playing.
My question is: is there a way to speed up Android native MediaPlayer Buffering in order to pla...
Getting streaming audio to play with MediaPlayer class on Android?
Hello I am currently new to android development I been working on the examples in the Dev Guide in the android website. I want to get a stream from a server I have to play in the emulator when I insert the url it doesn't seam to want to play. My question is there a way to get the emulator to play audio or is it all enabled also does MediaPlayer require a special kind of format like mp3 or ogg ?
This is the co...
Android MediaPlayer Streaming from a PHP Redirect does not work-out!
The company I work for is developing an Android App that plays a video file from a URL on web. The
video URL is a parameter for a PHP script that encode it properly and redirects to the encoded video as shown below:
header('Content-Type: video/'.$format);
header('Location:'.$output_video);
Where $output_video is the URL to the encoded video (it works if we use this URL in the ...
http - Android MediaPlayer streaming MP3 over POST
I've got a question related to the Android MediaPlayer. Can it stream content through HTTP POST method , or do I have to write my own implementation? If so, what SDK do I have to use?
Thanks in advance.
media player - Problem with mediaplayer to play live streaming audio in android
I have created one songs album app. In this app, i have to play the songs by using URLs. These URLs gave me 2 types of songs.The first one is on-demand songs, which are our regular songs limited to 5 or 6 minutes. I dont have any problems to play/stop these songs with MediaPlayer object. The second one is live streaming, which is nothing but shout casting.With this shout-casting URLs, the MediaPlayer object is not working ...
proxy - Streaming with Android MediaPlayer in SDK 8
SDK level 8 (Froyo) has introduced the native capability for the MediaPlayer to connect to a streaming source, like Shoutcast. Previous SDK versions were able to do workarounds, such as run a local proxy on the device (see NPR).
I took the same approach as NPR and am using a StreamProxy. However, NPR first checks if the currently running SDK i...
android - MediaPlayer audio streaming fails on Galaxy Tab
I have some issue playing streaming* audio on a Galaxy Tab (works on HTC Desire HD and Nexus One). When I create the MediaPlayer object, it produces an error like this:
02-09 02:21:39.088: VERBOSE/MediaPlayer-JNI(9325): native_setup
02-09 02:21:39.088: VERBOSE/MediaPlayer(9325): constructor
02-09 02:21:39.088: VERBOSE/MediaPlayer(9325): setListener
02-09 02:21:39.092: INFO/MediaPlayer(9325): uri is:http://1...
java - Android Mediaplayer Streaming - Worked now it doesn't
I have a strange problem. I have an mp3 stream I am trying to utilize within an application (2.1). Before you say streaming isn't supported here, it seems to be.
I was able to get it working last night by using the following code:
import java.io.IOException;
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Stream extends Activity {
/** Called ...
Audio streaming with Android MediaPlayer
I'm trying to create an audio streamer with Android's MediaPlayer. It's just fine if it doesn't work with Android 2.1 or bellow. I need to be able to play the audio from a SHOUTcast stream. Here's my code:
player = new MediaPlayer();
try {
player.setDataSource("http://87.230.103.107:8000");
player.prepareAsync();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace...
RTSP Streaming Video With Android MediaPlayer
I've been trying for quite some time now to use Android's MediaPlayer to stream mp4/h-264 encoded video to my HTC Incredible (Android 2.2). However, I've been getting various error messages such as "prepare failed", "setDataSource failed", "unable to create media player", and "media server died."
I was able to stream an mp4 video and a USB webcam feed from a VLC server on my desktop to my android last week (with a...
android - Issues while streaming on MediaPlayer SDK8
I'm streaming a live radio url directly through Mediaplayer in a service (SDK 8 and above). Some clients are saying that they are hearing some blips and bloops in between the stream. I'm not doing anything different here and just allowing the MediaPlayer to play the audio. Can someone tell me why is this happening?
-Hari
android - Play playlist with MediaPlayer
I'm trying to play a playlist I get using the MediaStore provider. However, when I try playing a playlist nothing happens. Can a MediaPlayer play a playlist (m3u file) and do I need to set the first track to play ?
This is my test code in the onCreate() method:
Uri uri = MediaStore.Audio.Playlists.EXTERNAL_CONTENT_URI;
if(uri == null) {
Log.e("Uri = null");
}
String[] pro...
How to get gapless playback?in android mediaplayer
I am trying to play continuous stream using android mediaplayer.but am not able to avoid gap in playback while updating the datasource file of mediaplayer.Hw can i overcome this problem?
Thanks in advance
media player - Android - Buffering in MediaPlayer
I am using MediaPlayer to play a video in my app. The video takes a while to buffer and the videoview is blank for that time.
Is there a way to start the buffering when the user is in the previous screen, so that when he comes to the video playing screen, the video is ready to play?
Thanks
Chris
android - Problems with MediaPlayer, raw resources, stop and start
I'm new to Android development and I have a question/problem.
I'm playing around with the MediaPlayer class to reproduce some sounds/music. I am playing raw resources (res/raw) and it looks kind of easy.
To play a raw resource, the MediaPlayer has to be initialized like this:
MediaPlayer mp = MediaPlayer.create(appContext, R.raw.song);
mp.start();
Until he...
media player - Android MediaPlayer ignores it's internal volume when the system volume changes
here is my situation:
I have a media player playing music in an Android application. I've found that with certain headphones, the volume is much too loud even when the volume is set to it's lowest setting. As a result, I want to change the volume of the music for all volume levels to be 10% of what it normally is (actually, this value is user-defined of course).
The following works perfectly:
android - Strange behaviour with mediaplayer and seekTo
I'm implementing a custom video player because I need custom video controls. I have an app with only one activity, which on startup shall start playing a video right away.
Now, the problem I have is:
I don't want the video to start from the beginning, but from a later position. Therefore I do a seekTo(16867). Since seekTo is asynchronous, I place the start call of the mediaplayer (player.start()) in the on...
android - Error creating MediaPlayer with Uri or file in assets
I copied song.mp3 to my project's assets directory and wrote this code:
private MediaPlayer mp;
Uri uri = Uri.parse("file:///android_asset/song.mp3");
mp=MediaPlayer.create(this, uri);
After running the create statement, the variable mp is null. What is wrong?
Thanks.
audio - Android MediaPlayer - only one instance at any given time?
I have a audio player app, where there is a Main activity that shows 3 audio sample urls. On click on one, it goes to a Details Activity, which has a play and pause button, to start and pause the audio.
My problem is that, when I start the Main activity, and say click on audio 1, I hit play on Details activity. This starts the MediaPlayer and the audio starts to play. When I go back to the Main activity, the audio...
Getting streaming audio to play with MediaPlayer class on Android?
Hello I am currently new to android development I been working on the examples in the Dev Guide in the android website. I want to get a stream from a server I have to play in the emulator when I insert the url it doesn't seam to want to play. My question is there a way to get the emulator to play audio or is it all enabled also does MediaPlayer require a special kind of format like mp3 or ogg ?
This is the co...
android - Pause any mediaplayer on some event
I have created an application that read messages with voice. I would like to pause any running media player when a message arrives in order to make the voice more clear. Is there any intent that could achieve this aim? I have noticed that google navigator, for example, is able to pause media player (and other music app like pandora).
Thanks in advance
Tobia Loschiavo
Still can't find your answer? Check out these communities...
Android Google Support | Android Community | Android Community (Facebook) | Dev.io Android