0

I have a problem with http streams over the android mediaplayer. I have written my own http server task which sends wav audio files to the mediaplayer. Everything works perfect with my galaxy s3 galaxy note2 and a chinese tablet but now I have got a ASUS K109 tablet and with this device my server doesn't work.

My start code looks like this:

mPlayer.reset();
mPlayer.setDataSource(uri.toString());
mPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
     @Override
     public void onPrepared(MediaPlayer mediaPlayer) {
          mPlayer.start();
     }
});
mPlayer.prepareAsync();

The first call work great but if I want to start a new file while the first is running the "onPrepared() was never called. I figured out the with my other devices the reset() method closes the socket connection therefore my server throws an error while sending the data and if that occures the server will stop sending data and waits for a new connection. With the Asus tablet the connection is not closed and therefore the server stays inside the send loop and does not wait for a new connection. I have also tried to stop() and to release() the mediaplayer but this also doesn't help.

Is this a known bug with the Asus tableb or the Android 5.0.1 version on it?

Have I missed something?

Thanks

stallianz
  • 176
  • 1
  • 17
truthz03
  • 41
  • 3
  • This might be a shot in the dark, but I tend to `stop(), reset()` and `release()` my `MediaPlayer` objects and re-create a new object every time I open (even the same) file. The MP got internal cleanup and might just take a little longer to clean up and is prone to interference. – Sebastian Roth Dec 04 '15 at 07:23
  • I already tried to completely stop, reset, release and recreate a new Mediaplayer but the new MediaPlayer also doesn't prepare because my server keeps sending to the first MediaPlayer (the first MediaPlayer has not closed the connection) – truthz03 Dec 04 '15 at 09:48
  • Please post the full HTTP headers as they are sent from your server. Will the problem occur if you, for example, host a music file via NGINX or Apache and then stream it? Does your server support multiple connections? Ensure the headers are sent like this: http://stackoverflow.com/a/24893603/375209 – Sebastian Roth Dec 04 '15 at 10:48
  • These are the Headers I send back after the MediaPlayer makes a request: `headers += "HTTP/1.1 200 OK\r\n"; headers += "Content-Type: audio/x-wav\r\n"; headers += "Accept-Ranges: bytes\r\n"; headers += "Content-Length: 300000" + "\r\n"; headers += "Connection: Keep-Alive\r\n"; headers += "\r\n";` – truthz03 Dec 04 '15 at 11:10
  • Sorry for the bad format – truthz03 Dec 04 '15 at 11:14
  • Do I have to send a close header? As descriped in the link "stackoverflow.com/a/24893603/375209" with "Connection: close". No my server does not allow multiple connections – truthz03 Dec 04 '15 at 11:17
  • 1. `Connection: Close`, I don't think so. 2. Multiple connections is something you definitely want because MediaPlayer might jump around in the stream multiple times (to download various parts). You should also support Range Requests properly. – Sebastian Roth Dec 04 '15 at 11:42
  • I will trie to accept multiple connections when I'm at home. I have another problem. The mediaserver should also transmit files where I don't know the exact content-length. I only have the numbers of ms with which I'm not able to get the exact number of bytes. How can I handle this problem? – truthz03 Dec 04 '15 at 12:01
  • Ok. I have now tried to accept multiple connections and it works better but I still have some problems. Are there some samples maybe also with unknown content length – truthz03 Dec 04 '15 at 16:47

1 Answers1

0

Can you try:

mPlayer.reset();
mPlayer.setDataSource(uri.toString());
mPlayer.prepare();
mPlayer.start();

Hope this help!

Lan Nguyen
  • 785
  • 5
  • 13
  • I also have tested this. If I try this while the player is playing another stream it blocks at prepare() – truthz03 Dec 04 '15 at 09:45