Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 451
Dilshod K Avatar asked Oct 15 '22 10:10

Dilshod K


2 Answers

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.

like image 119
Roaim Avatar answered Oct 21 '22 02:10

Roaim


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.

like image 34
raj kavadia Avatar answered Oct 21 '22 01:10

raj kavadia