Skip to content Skip to sidebar Skip to footer

Open Soundcloud Url In Native Soundcloud App From Webview

Scenario I have a WebView in my Android app which contains a Soundcloud embed (from Embedly). This embed has two buttons: 'Play on Soundcloud' and 'Listen in browser'. The 'Play o

Solution 1:

I'm just extending the Temporary (crap) solution here, so this is far from a perfect answer, but might still help someone who absolutely needs to get this to work, also with private tracks.

The replace method works if the track is public, but with private tracks this does not work, probably because of the missing secret token in the intent URL.

Unfortunately the embed player does not contain all the necessary pieces of the URL I need to generate, except inside the iframe, which I cannot access due to cross-origin policy. So in addition to the iframe code I also need the share link.

What I ended up doing is making sure that the containing HTML page has the share link as a JS variable. I then read that variable using Java and create a new Intent with that URL. This works, because the official app also registers all soundcloud.com URLs.

So for private tracks this goes to the HTML page:

<script>var soundCloudURL = "https://soundcloud.com/my-profile/my-track/my-secret-token";</script>

Then inside your Android app you would have something like this:

@Overridepublicboolean shouldOverrideUrlLoading (WebView view, String url) {
    if (uri.getScheme().contains("intent")) {
        openSoundCloudPlayer();
        returntrue;
    }
}

privatevoidopenSoundCloudPlayer() {
    appWebView.evaluateJavascript("(function() { return soundCloudUrl })();", newValueCallback<String>() {
        @OverridepublicvoidonReceiveValue(String soundCloudUrl) {
            // JS null is converted into a string "null", not Java null.if (soundCloudUrl != "null") {
                // Take out the quotes from the string
                soundCloudUrl = soundCloudUrl.replace("\"", "");

                Uri newUri = Uri.parse(soundCloudUrl);
                Intent intent = newIntent(Intent.ACTION_VIEW, newUri);
                startActivity(intent);
            }
        }
    });
}

Post a Comment for "Open Soundcloud Url In Native Soundcloud App From Webview"