Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I use the same StreamProxy that guys used for NPR project - <a href="https://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java" rel="noreferrer">https://code.google.com/p/npr-android-app/source/browse/Npr/src/org/npr/android/news/StreamProxy.java</a></p> <p>So it gets original audio stream: </p> <pre><code> String url = request.getRequestLine().getUri(); HttpResponse realResponse = download(url); ... InputStream data = realResponse.getEntity().getContent(); </code></pre> <p>And writes from this stream to client socket:</p> <pre><code> byte[] buff = new byte[1024 * 50]; while (isRunning &amp;&amp; (readBytes = data.read(buff, 0, buff.length)) != -1) { client.getOutputStream().write(buff, 0, readBytes); } </code></pre> <p>(You can get whole code from the link above.)</p> <p>And finally how they initialize the player (PlaybackService):</p> <pre><code> if (stream &amp;&amp; sdkVersion &lt; 8) { if (proxy == null) { proxy = new StreamProxy(); proxy.init(); proxy.start(); } String proxyUrl = String.format("http://127.0.0.1:%d/%s", proxy.getPort(), url); playUrl = proxyUrl; } ... mediaPlayer.setDataSource(playUrl); mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC); mediaPlayer.prepareAsync(); </code></pre> <p>So they check SDK version. But if I omit this check and use proxy for SDK 8 as well I get an exception. It is strange that MediaPlayer even doesn't try to read the stream:</p> <pre><code>12-30 15:09:41.576: DEBUG/StreamProxy(266): downloading... 12-30 15:09:41.597: DEBUG/StreamProxy(266): reading headers 12-30 15:09:41.597: DEBUG/StreamProxy(266): headers done 12-30 15:09:41.647: DEBUG/StreamProxy(266): writing to client 12-30 15:09:41.857: INFO/AwesomePlayer(34): mConnectingDataSource-&gt;connect() returned - 1007 12-30 15:09:41.857: ERROR/MediaPlayer(266): error (1, -1007) 12-30 15:09:41.867: ERROR/MediaPlayer(266): Error (1,-1007) 12-30 15:09:41.867: WARN/AudioService(266): onError(1, -1007) 12-30 15:09:41.867: WARN/AudioService(266): MediaPlayer refused to play current item. Bailing on prepare. 12-30 15:09:41.867: WARN/AudioService(266): onComplete() 12-30 15:09:42.097: ERROR/StreamProxy(266): Connection reset by peer java.net.SocketException: Connection reset by peer at org.apache.harmony.luni.platform.OSNetworkSystem.writeSocketImpl(Native Method) at org.apache.harmony.luni.platform.OSNetworkSystem.write(OSNetworkSystem.java:723) at org.apache.harmony.luni.net.PlainSocketImpl.write(PlainSocketImpl.java:578) at org.apache.harmony.luni.net.SocketOutputStream.write(SocketOutputStream.java:59) at com.skyblue.service.media.StreamProxy.processRequest(StreamProxy.java:204) at com.skyblue.service.media.StreamProxy.run(StreamProxy.java:103) at java.lang.Thread.run(Thread.java:1096) </code></pre> <p>It seems that MediaPlayer became more clever. And If i pass such url <code>"http://127.0.0.1:%d/%s"</code> it wants to get not only bytes but "full" http responce.</p> <p>Also I wonder if there any other ways implement buffering? As I know the MediaPlayer consumes only files and urls. Solution with files doesn't work. So I have to use socket for stream transmitting.</p> <p>Thanks</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload