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:
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?
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 -
If you need to intercept https traffic, you need to generate and install an SSL certificate.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With