我正在尝试使用MediaPlayer在我的Android设备上流式传输SoundCloud轨道,但是我很难让它正常工作。我正在使用SoundCloud java API wrapper从SoundCloud获取数据,这很好用。 MediaPlayer实例设置如下:
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
并在单击按钮时调用流:
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();
}
}
变量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"}
我尝试过同时使用stream_url
和permalink_url
,但都失败了。我注意到使用mMediaPlayer.setDataSource(mStreamURL);
和mMediaPlayer.setDataSource(mPlayButton.getContext(), Uri.parse(mStreamURL));
设置MediaPlayer的数据源存在差异。使用前者时,我没有从MediaPlayer实例中获取任何错误,但是当我使用后者时,我收到以下错误:
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
对于设置数据源的两种方法,我在到达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)
我无法在任何地方找到关于error (1, -1004)
的任何信息,我在这里有点不知所措。没有谷歌搜索也似乎非常有帮助。此外,我尝试了以下内容:
mMediaPlayer.prepareAsync()
。<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
https
更改为http
,反之亦然。非常感谢任何评论或提示!
答案 0 :(得分:3)
您需要在Stream Url之后添加?client_id=YOUR_API_KEY
。
在您的示例中:stream_url = https://api.soundcloud.com/tracks/143042205/stream?client_id=YOUR_API_KEY