How can I use SoundCloud in my Android app?

I want to use soundcloud in my android application like this: I want to play the song on the sound player with the URL. I used the following code in a webview, but it did not work correctly. How can i do this? Thanks.

<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> 
+4
source share
5 answers

Have you considered the official SoundCloud Java shell instead?

+5
source

I also tried the built-in solutions for the webview player, but this does not work.

Now I am using the Soundcloud Java API Wrapper and it works fine. Follow the instructions in the Github repository to implement the API: https://github.com/soundcloud/java-api-wrapper

Then the code is really simple. You only need the client ID and client secret, both of which must be obtained on the soundcloud developers website.

Then the code is really simple:

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

The m_soundcloudPlayer object is android.media.MediaPlayer .

+2
source

I have the same problem. I found out that the reason the standard embed code does not work is because the Android browser does not support HTML5 audio codecs. I think the best shot is the official wrapper, but I'm not sure how to do it (just an amateur).

+1
source
 //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", ""); 
+1
source

I tried using SoundCloud Java Api Wrapper. But this thing gives me an error when I try to get a track.

That is, in the line

  HttpResponse trackResp = wrapper.get(Request.to("/tracks/60913196")); 

Error - 13781-13781 / com.example.DDS.soundcloud E / Trace: file with error opening: there is no such file or directory (2)

If anyone has a working draft of the Soundcloud player in an Android app. Please, share with us the project.

0
source

Source: https://habr.com/ru/post/1389451/


All Articles