阅读背景:

Android MediaPlayer未能流进SoundCloud轨道。

来源:互联网 

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:

我正在尝试用MediaPlayer在我的Android设备上播放SoundCloud的音轨,但是我很难使它正常工作。我使用SoundCloud java API包装器来从SoundCloud获取数据,这样做很好。MediaPlayer实例是这样设置的:

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):

变量mStreamURL包含从SoundCloud获得的流URL,例如https://api.soundcloud.com/tracks/143042205/stream。我不太确定在本例中应该使用哪个URL,因为我从SoundCloud获得了几个URL(我已经从下面删除了明显不必要的信息):

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

我尝试使用stream_url和permalink_url,但都失败了。我注意到使用mMediaPlayer.setDataSource(mStreamURL)是有区别的;和mMediaPlayer.setDataSource(mPlayButton.getContext(),Uri.parse(mStreamURL));为MediaPlayer设置数据源。使用前者时,我不会从MediaPlayer实例中得到错误,但当我使用后者时,我得到了以下错误:

D/SoundCloudProfileWidget(388): Trying to play track https://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: https://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:

对于这两种设置数据源的方法,当到达MediaPlayer的prepare()方法时,我得到以下信息:

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,-1004),我在这里有点损失。没有谷歌搜索似乎也很有帮助。此外,我尝试了以下几点:

  1. Change to using mMediaPlayer.prepareAsync().
  2. 改变使用mMediaPlayer.prepareAsync()。
  3. Change permissions to this: <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
  4. 更改权限: < uses-permission android:name = " android.permission。ACCESS_NETWORK_STATE " / >
  5. Change https to http and vice versa in the stream URL.
  6. 在流URL中更改https到http,反之亦然。
  7. Various other stuff.
  8. 其他的东西。

Any comments or hints are very appreciated!

任何意见或提示都非常感谢!

1 个解决方案

#1


3  

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

您需要在流Url后面添加?client_id=YOUR_API_KEY。

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

在你的例子:stream_url = https://api.soundcloud.com/tracks/143042205/stream?client_id=YOUR_API_KEY


分享到: