Dilshod K
Dilshod K

Reputation: 3032

How to get URL of mp3 when it is playing

We can play mp3 files from browsers, Social Networks, Apps... I want to get URL of mp3 when it's playing. One way here is to create an extension in the browser. But in other apps we can't. Here is an example: I go to some site and open some mp3:

mp3 from sote

If you can see the music is playing and it has a URL. I want to get this URL when music starts playing. How can I do that? And how can I get URL of music which is playing in some app? Is it possible?

Upvotes: 5

Views: 12134

Answers (3)

dama-dev
dama-dev

Reputation: 1

As the tip above of intercepting the requests, I went to the Network tab on the browser console and found what I want it.

Using Google Chrome, just inspect the page and go to the network tab. The audio link is there. Just click on the line that you think it is (the ones that has *.mp3, *.wav etc) and the browser will open the audio on a new tab, with the full link

Upvotes: 0

Roaim
Roaim

Reputation: 2358

If you can see the music is playing and it has a URL. I want to get this URL when music starts playing. How can I do that?

If you are loading the website into a WebView, the easiest possible way you can get the URL by intercept the requests through WebViewClient and check if the URL contains '.mp3'. See the following example,

public class YourWebViewClient extends WebViewClient {
        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, WebResourceRequest request) {
            String url = request.getUrl().toString();
            if (url.contains(".mp3")) {
                Log.d(TAG, "MP3 url = [" + url + "]");
            }
            return super.shouldInterceptRequest(view, request);
        }
}

And how can I get URL of music which is playing in some app? Is it possible?

Yes possible but not too easy especially if you are going to get it from SSL traffic. To sniff other app's network traffic you need to perform MITM. There are a couple of apps in the play store which are doing exactly the same thing. You can look into HttpCanary app for reference. Basically you need to perform the following steps -

  1. Set up a proxy server
  2. Configure your device to pass its network traffic to that proxy server
  3. Ask the proxy server to give you the network data
  4. Analyze the network data to see if it contains 'mp3' URL

If you need to intercept https traffic, you need to generate and install an SSL certificate.

Upvotes: 2

raj kavadia
raj kavadia

Reputation: 1053

First import the JSOUP library from maven

compile 'org.jsoup:jsoup:1.12.1'

after that use this code

            Document doc = Jsoup.connect(url).get();
            System.out.println(doc.title());
            Elements h1s = doc.select(".jp-type-single"); 
            System.out.println("Number of results: " + h1s.size());
            for (Element element : h1s) { 
            String mp3Url = element.attr("data-xc-filepath"); 
            System.out.println("mp3 url: " + mp3Url);
            file_num++;
            URLConnection conn = new URL(mp3Url).openConnection();
            InputStream is = conn.getInputStream();
            OutputStream outstream = new FileOutputStream(new 
            File("/users/pelican/downloads/"+file_num+"file.mp3"));
            byte[] buffer = new byte[4096];
            int len;
            while ((len = is.read(buffer)) > 0) {
                outstream.write(buffer, 0, len);
            }

now let me explain this JSOUP fetched the webpage using your HTML URL

after that, it converts into doc

and after that, it selects the element by using a select method and here we are getting the first audio tag that we want to search.

Upvotes: 1

Related Questions