2
\$\begingroup\$

Demo for a SoundCloud player in WebView for specific song or artist stream as a workaround because SoundCloud is not accepting API key request anymore.

SoundCloudWebView:

public class SoundCloudWebView extends AppCompatActivity {
    private WebView mWebView;
    private Button songButton, artistButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.soundcloud_web_view);

        mWebView = (WebView) findViewById(R.id.webView);
        songButton = (Button) findViewById(R.id.SongButton);
        artistButton = (Button) findViewById(R.id.ArtistButton);

        songButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PlayerFactory playerFactory = new PlayerFactory();
                SongPlayer player = new SongPlayer();

                player.setmWebView(mWebView);
                player.setURL("https://soundcloud.com/adhsofficial/adhs-dampfer-sisyphos-berlin");

                playerFactory.setPlayer(player);
                playerFactory.showPlayer();
            }
        });

        artistButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                PlayerFactory playerFactory = new PlayerFactory();
                ArtistStreamPlayer player = new ArtistStreamPlayer();

                player.setmWebView(mWebView);
                player.setURL("https://soundcloud.com/polytoxikomanie/");

                playerFactory.setPlayer(player);
                playerFactory.showPlayer();
            }
        });
    }

    public class myJavaScriptInterface {
        @JavascriptInterface
        public void JavaScriptListener() {
            runOnUiThread(new Runnable() {

                @Override
                public void run() {
                }
            });
        }

    }
}

PlayerFactory:

public class PlayerFactory {
    private WebViewPlayer player;

    public void setPlayer(WebViewPlayer player) {
        this.player = player;
    }

    public void showPlayer(){
        this.player.show();
    }
}

WebViewPlayer:

public class WebViewPlayer {
    private WebView mWebView;
    private final String APP_HEADER = "'app__header g-z-index-header'";
    private final String APP_UPSELL = "'app__upsell g-z-index-upsell'";
    private String URL;
    private WebViewClient mWebViewClient;


    public void hideHeader(final WebView webView) {
        webView.setWebViewClient(getmWebViewClient());
    }

    public void show(){};

    public WebViewClient setOnPageFinishedBehavior(){return null;};

    public String generateHeaderJavaScriptCode() {
        List<String> elementIDs = Arrays.asList(APP_HEADER, APP_UPSELL);
        String js = "javascript:(function() { ";
        for (String id : elementIDs) {
            js += "document.getElementsByClassName(" + id + ")[0].style.visibility = 'hidden'; ";
        }
        js += "document.getElementById(\"content\").style.padding = \"0px 0px 0px 0px\";";
        js += "})()";
        return js;
    }

    public WebView getmWebView() {
        return mWebView;
    }

    public void setmWebView(WebView mWebView) {
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebContentsDebuggingEnabled(true);
        mWebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
        mWebView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        mWebView.setVisibility(View.INVISIBLE);

        this.mWebView = mWebView;
    }

    public String getURL() {
        return URL;
    }

    public void setURL(String URL) {
        this.URL = URL;
    }

    public WebViewClient getmWebViewClient() {
        return mWebViewClient;
    }

    public void setmWebViewClient(WebViewClient mWebViewClient) {
        this.mWebViewClient = mWebViewClient;
    }
}

ArtistStreamPlayer:

public class ArtistStreamPlayer extends WebViewPlayer {
    private final String PLAY_BUTTON = "'userProfile__playButton g-play-button'";
    private final String FOOTER = "'footer show'";

    public ArtistStreamPlayer() {
    }

    @Override
    public void show(){
        if(getmWebView() != null) {
            setmWebViewClient(setOnPageFinishedBehavior());
            setOnPageFinishedBehavior();
            hideHeader(getmWebView());
            getmWebView().loadUrl(getURL());
            getmWebView().setVisibility(View.VISIBLE);
        }
    }

    @Override
    public WebViewClient setOnPageFinishedBehavior(){
        return new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                view.loadUrl(generateHeaderJavaScriptCode());
                startArtistStream(getmWebView());
                view.loadUrl("javascript: window.CallToAnAndroidFunction.JavaScriptListener()");
            }
        };
    }

    public void startArtistStream(WebView view){
        try {
            Thread.sleep(3000);
            view.loadUrl("javascript:(function() {var c = document.getElementsByClassName(" + PLAY_BUTTON + ")[0]; c.click();" +
                    " document.getElementsByClassName(" + FOOTER + ")[0].remove(); })()");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

SongPlayer:

public class SongPlayer extends WebViewPlayer {
    public SongPlayer() {
    }

    @Override
    public void show(){
        if(getmWebView() != null) {
            setmWebViewClient(setOnPageFinishedBehavior());
            hideHeader(getmWebView());
            getmWebView().loadUrl(getURL());
            getmWebView().setVisibility(View.VISIBLE);
        }
    }

    @Override
    public WebViewClient setOnPageFinishedBehavior(){
        return new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                view.loadUrl(generateHeaderJavaScriptCode());
                view.loadUrl("javascript: window.CallToAnAndroidFunction.JavaScriptListener()");
            }
        };
    }
}

soundcloud_web_view.xml:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="vhalad.soundcloudsoundscraper.SoundCloudWebView">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@color/colorPrimaryDark"
            tools:layout_editor_absoluteX="8dp"
            tools:layout_editor_absoluteY="8dp" />

        <Button
            android:id="@+id/SongButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/webView"
            android:layout_marginStart="67dp"
        android:layout_marginTop="123dp"
        android:text="Song"
        android:textAllCaps="false" />

    <Button
        android:id="@+id/ArtistButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/SongButton"
        android:layout_marginStart="65dp"
        android:layout_toEndOf="@+id/SongButton"
        android:text="Artist Stream"
        android:textAllCaps="false" />

</RelativeLayout>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.