我该如何在安卓应用中使用SoundCloud?

4

我想在我的安卓应用中使用SoundCloud,就像这样: 我希望能够通过URL地址在SoundCloud播放器中播放一首歌曲。 我尝试在WebView中使用以下代码,但它并没有正确运行。 请问我该如何做? 谢谢。

<iframe width=\"100%\" height=\"166\" scrolling=\"no\" frameborder=\"no\" src=\"http://w.soundcloud.com/player/?url=http%3A%2F%2Fapi.soundcloud.com%2Ftracks%2F31416027&amp;auto_play=false&amp;show_artwork=false&amp;color=ff7700\"></iframe>

请详细说明哪些功能没有起作用,哪些功能能够正常工作。 - Cheesebaron
只能运行一次,但我没有继续完善这首歌。也许有更好的解决方案。 - realuser
4个回答

5

2
我也尝试过使用webview的嵌入式播放器解决方案,但是这不起作用。
现在我正在使用Soundcloud Java API Wrapper,这个方法很好用。 按照GitHub存储库上的说明实现API即可: https://github.com/soundcloud/java-api-wrapper 代码非常简单。您只需要一个客户端ID和客户端秘密,两者都必须在soundcloud开发人员网站上获取。
代码非常简单:
        String id = getResources().getString(R.string.sc_client_id);
        String secret = getResources().getString(R.string.sc_client_secret);
        ApiWrapper wrapper = new ApiWrapper(id,secret, null, null);

        try {
            //Only needed for user-specific actions;
            //wrapper.login("<user>", "<pass>");
            //HttpResponse resp = wrapper.get(Request.to("/me"));
            //Get a track
            HttpResponse trackResp = wrapper.get(Request.to("/tracks/60913196"));
            //Track JSON response OK?
            if(trackResp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                JSONObject trackJSON = new JSONObject(EntityUtils.toString(trackResp.getEntity()));
                //If track is streamable, fetch the stream URL (mp3-https) and start the MediaPlayer
                if(trackJSON.getBoolean("streamable"))
                {
                    HttpResponse streamResp = wrapper.get(Request.to("/tracks/60913196/stream"));
                    JSONObject streamJSON = new JSONObject(EntityUtils.toString(streamResp.getEntity()));
                    String streamurl = streamJSON.getString("location");
                    Log.i("SoundCloud", trackJSON.getString("streamable"));
                    Log.i("SoundCloud", streamurl);
                    m_soundcloudPlayer.stop();
                    m_soundcloudPlayer = new MediaPlayer();
                    m_soundcloudPlayer.setDataSource(streamurl);
                    m_soundcloudPlayer.prepare();
                    m_soundcloudPlayer.start();
                }

            }
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }catch (ParseException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (JSONException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

m_soundcloudPlayer对象是一个android.media.MediaPlayer


1
我遇到了同样的问题。我发现标准嵌入代码无法工作的原因是Android浏览器不支持HTML5音频编解码器。最好的选择可能是官方包装器,但我还不确定如何做到这一点(只是个业余爱好者)。

如何将SoundCloud Java Wrapper API https://github.com/soundcloud/java-api-wrapper 库导入到我的Android应用程序中? - Faisal Ashraf

1
//In Activity_layout.xml

 <LinearLayout
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical">
        <WebView android:id="@+id/webview"
                     android:layout_width="match_parent"
                     android:layout_height="wrap_content"
                     />

    </LinearLayout>



// In ActivityClass.java

    mSoundCloudPlayer =(WebView) findViewById(R.id.webview);

    String VIDEO_URL = "Set Your Embedded URL";

    String html = "<!DOCTYPE html><html> <head> <meta charset=\"UTF-8\"><meta name=\"viewport\" content=\"target-densitydpi=high-dpi\" /> <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"> <link rel=\"stylesheet\" media=\"screen and (-webkit-device-pixel-ratio:1.5)\" href=\"hdpi.css\" /></head> <body style=\"background:black;margin:0 0 0 0; padding:0 0 0 0;\"> <iframe id=\"sc-widget " +
                                    "\" width=\"100%\" height=\"50%\"" + // Set Appropriate Width and Height that you want for SoundCloud Player
                                    " src=\"" + VIDEO_URL   // Set Embedded url
                                    + "\" frameborder=\"no\" scrolling=\"no\"></iframe>" +
                                    "<script src=\"https://w.soundcloud.com/player/api.js\" type=\"text/javascript\"></script> </body> </html> ";

            mSoundCloudPlayer.setVisibility(View.VISIBLE);
            mSoundCloudPlayer.getSettings().setJavaScriptEnabled(true);
            mSoundCloudPlayer.getSettings().setLoadWithOverviewMode(true);
            mSoundCloudPlayer.getSettings().setUseWideViewPort(true);
            mSoundCloudPlayer.loadDataWithBaseURL("",html,"text/html", "UTF-8", "");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接