如何在播放时获取URL的mp3

How to get URL of mp3 when it is playing

我们可以从浏览器、社交网络、应用程序播放 mp3 文件...我想在播放时获得 URL 的 mp3。这里的一种方法是在浏览器中创建一个扩展。但在其他应用程序中我们不能。这是一个例子: 我去一些网站并打开一些 mp3:

如果您可以看到音乐正在播放并且有一个 URL。我想在音乐开始播放时得到这个 URL。我怎样才能做到这一点?我怎样才能获得 URL 正在某些应用程序中播放的音乐?可能吗?

首先从maven导入JSOUP库

compile 'org.jsoup:jsoup:1.12.1'

之后使用此代码

            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);
            }

现在让我解释一下 JSOUP 使用您的 HTML URL

获取网页

之后,它转换成文档

然后,它使用 select 方法 select 元素,在这里我们得到了我们想要搜索的第一个音频标签。

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?

如果您将网站加载到 WebView,最简单的方法是通过 WebViewClient 拦截请求并检查 URL 是否可以获取 URL ] 包含“.mp3”。看下面的例子,

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?

可以,但不太容易,特别是如果您要从 SSL 流量中获取它。要嗅探其他应用程序的网络流量,您需要执行 MITM. There are a couple of apps in the play store which are doing exactly the same thing. You can look into HttpCanary 应用程序以供参考。基本上你需要执行以下步骤 -

  1. 设置代理服务器
  2. 配置您的设备以将其网络流量传递到该代理服务器
  3. 请求代理服务器给你网络数据
  4. 分析网络数据是否包含'mp3'URL

如果需要拦截https流量,需要生成并安装SSL证书。