title: Android MediaPlayer fails to stream SoundCloud tracks
body: <p>I am trying to stream a SoundCloud track on my Android device using the MediaPlayer, but I am having difficulties making it work properly. I am using the <a href="https://github.com/soundcloud/java-api-wrapper" rel="nofollow">SoundCloud java API wrapper</a> to fetch data from SoundCloud, and this works fine. The MediaPlayer instance is set up like this:</p>
  
  <pre><code>mMediaPlayer = new MediaPlayer();
  mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
  </code></pre>
  
  <p>and the stream is invoked when clicking a button:</p>
  
  <pre><code>public void onClick(View v) {
      try {
  //        mMediaPlayer.setDataSource(mStreamURL);
          mMediaPlayer.setDataSource(mPlayButton.getContext(), Uri.parse(mStreamURL));
          mMediaPlayer.prepare();
          mMediaPlayer.start();
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>The variable <code>mStreamURL</code> contains the obtained stream URL from SoundCloud, e.g. <code>https://api.soundcloud.com/tracks/143042205/stream</code>. I am not quite sure which URL that should be used in this case, since I obtain several of them from SoundCloud (I have removed obviously unnecessary information from the below):</p>
  
  <pre><code>{"id":143042205,
  "title":"Piano Sonata No.26 In E Flat, Op.81a - Les Adieux  - 3. Das Wiedersehn (Vivacissimamente)",
  "sharing":"public",
  "download_url":"https:\/\/api.soundcloud.com\/tracks\/143042205\/download",
  "streamable":true,
  "permalink_url":"http:\/\/soundcloud.com\/ystein-myrmo\/piano-sonata-no-26-in-e-flat-2",
  "original_format":"mp3",
  "original_content_size":12515204,
  "attachments_uri":"https:\/\/api.soundcloud.com\/tracks\/143042205\/attachments",
  "stream_url":"https:\/\/api.soundcloud.com\/tracks\/143042205\/stream",
  "uri":"https:\/\/api.soundcloud.com\/tracks\/143042205",
  "permalink":"piano-sonata-no-26-in-e-flat-2",
  "secret_uri":"https:\/\/api.soundcloud.com\/tracks\/143042205?secret_token=s-GvCYp",
  "waveform_url":"https:\/\/w1.sndcdn.com\/IASpgyYFSiPM_m.png"}
  </code></pre>
  
  <p>I have tried using both the <code>stream_url</code> and the <code>permalink_url</code>, but both fails. I note that there is a difference in using <code>mMediaPlayer.setDataSource(mStreamURL);</code> and <code>mMediaPlayer.setDataSource(mPlayButton.getContext(), Uri.parse(mStreamURL));</code> to set the data source for the MediaPlayer. When using the former, I get no errors from the MediaPlayer instance, but when I use the latter I get the following error:</p>
  
  <pre><code>D/SoundCloudProfileWidget(388): Trying to play track http://soundcloud.com/ystein-myrmo/piano-sonata-no-26-in-e-flat-2
  I/MediaPlayer(388): path is null
  D/MediaPlayer(388): setDataSource IOException happend : 
  D/MediaPlayer(388): java.io.FileNotFoundException: No content provider: http://soundcloud.com/ystein-myrmo/piano-sonata-no-26-in-e-flat-2
  D/MediaPlayer(388):     at android.content.ContentResolver.openTypedAssetFileDescriptor(ContentResolver.java:761)
  D/MediaPlayer(388):     at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:665)
  D/MediaPlayer(388):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:960)
  D/MediaPlayer(388):     at android.media.MediaPlayer.setDataSource(MediaPlayer.java:914)
  D/MediaPlayer(388):     at com.test.app.soundcloud.SoundCloudProfileWidget$PlayButtonListener.onClick(SoundCloudProfileWidget.java:149)
  D/MediaPlayer(388):     at android.view.View.performClick(View.java:4475)
  D/MediaPlayer(388):     at android.view.View$PerformClick.run(View.java:18786)
  D/MediaPlayer(388):     at android.os.Handler.handleCallback(Handler.java:730)
  D/MediaPlayer(388):     at android.os.Handler.dispatchMessage(Handler.java:92)
  D/MediaPlayer(388):     at android.os.Looper.loop(Looper.java:176)
  D/MediaPlayer(388):     at android.app.ActivityThread.main(ActivityThread.java:5419)
  D/MediaPlayer(388):     at java.lang.reflect.Method.invokeNative(Native Method)
  D/MediaPlayer(388):     at java.lang.reflect.Method.invoke(Method.java:525)
  D/MediaPlayer(388):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
  D/MediaPlayer(388):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
  D/MediaPlayer(388):     at dalvik.system.NativeStart.main(Native Method)
  D/MediaPlayer(388): Couldn't open file on client side, trying server side
  </code></pre>
  
  <p>For both ways to set the data source I get the following when arriving at the <code>prepare()</code> method for the MediaPlayer:</p>
  
  <pre><code>V/MediaPlayer(4964): prepare
  V/MediaPlayer(4964): message received msg=100, ext1=1, ext2=-1004
  E/MediaPlayer(4964): error (1, -1004)
  V/MediaPlayer(4964): signal application thread
  V/MediaPlayer(4964): prepare complete - status=1
  W/System.err(4964): java.io.IOException: Prepare failed.: status=0x1
  W/System.err(4964):     at android.media.MediaPlayer.prepare(Native Method)
  W/System.err(4964):     at com.test.app.soundcloud.SoundCloudProfileWidget$PlayButtonListener.onClick(SoundCloudProfileWidget.java:143)
  W/System.err(4964):     at android.view.View.performClick(View.java:4475)
  W/System.err(4964):     at android.view.View$PerformClick.run(View.java:18786)
  W/System.err(4964):     at android.os.Handler.handleCallback(Handler.java:730)
  W/System.err(4964):     at android.os.Handler.dispatchMessage(Handler.java:92)
  W/System.err(4964):     at android.os.Looper.loop(Looper.java:176)
  W/System.err(4964):     at android.app.ActivityThread.main(ActivityThread.java:5419)
  W/System.err(4964):     at java.lang.reflect.Method.invokeNative(Native Method)
  W/System.err(4964):     at java.lang.reflect.Method.invoke(Method.java:525)
  W/System.err(4964):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
  W/System.err(4964):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
  W/System.err(4964):     at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p>I cannot find any information on <code>error (1, -1004)</code> anywhere, and I am at a bit of a loss here. No googling seems to be very helpful either. Furthermore, I have tried the following:</p>
  
  <ol>
  <li>Change to using <code>mMediaPlayer.prepareAsync()</code>.</li>
  <li>Change permissions to this:
  <code>&lt;uses-permission android:name="android.permission.INTERNET" /&gt;
  &lt;uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /&gt;</code></li>
  <li>Change <code>https</code> to <code>http</code> and vice versa in the stream URL.</li>
  <li>Various other stuff.</li>
  </ol>
  
  <p>Any comments or hints are very appreciated!</p>