Question

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 SoundCloud java API wrapper to fetch data from SoundCloud, and this works fine. The MediaPlayer instance is set up like this:

mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

and the stream is invoked when clicking a button:

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();
    }
}

The variable mStreamURL contains the obtained stream URL from SoundCloud, e.g. https://api.soundcloud.com/tracks/143042205/stream. 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):

{"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"}

I have tried using both the stream_url and the permalink_url, but both fails. I note that there is a difference in using mMediaPlayer.setDataSource(mStreamURL); and mMediaPlayer.setDataSource(mPlayButton.getContext(), Uri.parse(mStreamURL)); 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:

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

For both ways to set the data source I get the following when arriving at the prepare() method for the MediaPlayer:

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)

I cannot find any information on error (1, -1004) 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:

  1. Change to using mMediaPlayer.prepareAsync().
  2. Change permissions to this: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  3. Change https to http and vice versa in the stream URL.
  4. Various other stuff.

Any comments or hints are very appreciated!

Was it helpful?

Solution

You need to add ?client_id=YOUR_API_KEY after the Stream Url.

In your example: stream_url=https://api.soundcloud.com/tracks/143042205/stream?client_id=YOUR_API_KEY

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top