试图从URL的Java播放声音 [英] Trying to play sound from a URL Java

查看:162
本文介绍了试图从URL的Java播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有从特定URL播放声音的烦恼。

I am having troubles with playing a sound from a certain URL.

下面是我目前的code,

Here is my current code,

    public static void aplay(String url) throws
    MalformedURLException, UnsupportedAudioFileException, 
    IOException, LineUnavailableException {

      Clip c = AudioSystem.getClip();
      AudioInputStream a = AudioSystem.getAudioInputStream(new URL(url));
      Clip c = AudioSystem.getClip();
      c.open(a);
      c.start();
}

另外,我有我的'主'方​​法这个方法。我把 https://translate.google.com / translate_tts即= UTF-8和; TL = EN&安培; q =嗨%20There 为网址,这是行不通的,它会回应一个403(服务器返回的HTTP响应code? 403)。我只是想知道如果我能解决这个问题。

Also, I have this method in my 'main' method. I put https://translate.google.com/translate_tts?ie=utf-8&tl=en&q=Hi%20There as the 'url' and it would not work, and it would respond with a 403 (Server returned HTTP response code: 403). I am just wondering if I could fix this.

谢谢,
  丹

Thanks, Dan

推荐答案

URL是从谷歌声音文件翻译。为了将谷歌服务的prevent滥用,服务器执行与它试图告诉它是否是一个人或一个自动化的服务访问URL的几个启发式检查。这就是为什么它有403回复,这实际上意味着禁止。

The URL is a sound file from Google Translate. In order to prevent misuse of the Google services, the server performs a few heuristic checks with which it tries to tell whether it's a human or an automated service accessing the URL. That's why it replies with 403, which actually means "Forbidden".

免责声明:考虑他们为什么禁止它,并检查他们使用方面

Disclaimer: Consider why they forbid it and check their terms of usage.

直接打开网址在浏览器Chrome等工作。像wget的或类URL工具检索URL不会 - 至少不会超出开箱

Opening the URL directly in a browser like Chrome works. Retrieving the URL with a tool like wget or class URL doesn't - at least not out-of-the-box.

您需要更改以伪造一个不同的用户代理HTTP请求的用户代理属性。您可以通过手动连接和改变的User-Agent请求属性做到这一点。

You need to change the User-Agent property of the HTTP request in order to fake a different user agent. You can do that by connecting "manually" and changing the "User-Agent" request property.

下面的 WGet.java 房源展示了如何做到这一点:

The following WGet.java listing demonstrates how to do so:

import java.io.*;
import java.net.*;

public class WGet {
    public static void main(final String... args) throws IOException {
        for (final String arg : args) {
            wget(arg);
        }
    }
    public static void wget(final String arg) throws IOException {
        wget(new URL(arg));
    }
    public static void wget(final URL url) throws IOException {
        final URLConnection con = url.openConnection();
        con.setRequestProperty("User-Agent", "My Client");
        try (final InputStream in = con.getInputStream()) {
            copy(in, System.out);
        }
    }
    public static void copy(final InputStream in, final OutputStream out) throws IOException {
        final byte[] buf = new byte[4096];
        for (int bytesRead; (bytesRead = in.read(buf)) != -1; )
            out.write(buf, 0, bytesRead);
        out.flush();
    }
}

AudioSystem ,也可以用的InputStream 使用。因此,下面 aplay()方法应该工作:

AudioSystem can also be used with an InputStream. So, the following aplay() method should work:

public static void aplay(String url) throws UnsupportedAudioFileException, IOException, LineUnavailableException {
    Clip c = AudioSystem.getClip();
    AudioInputStream a = AudioSystem.getAudioInputStream(openStream(url));
    Clip c = AudioSystem.getClip();
    c.open(a);
    c.start();
}
public static InputStream openStream(String url) throws IOException {
    final URL url = new URL(url);
    final URLConnection con = url.openConnection();
    con.setRequestProperty("User-Agent", "My Client");
    return con.getInputStream();
}

免责声明:我向您展示的技术方案。如果添加此code到您的程序,并用它来获得谷歌的声音文件,你实际上可能违反谷歌使用的翻译的条款。获得有关你使用这个code之前的产品,从谷歌获得的声音文件翻译的法律咨询。

Disclaimer: I am showing you the technical solution. If you add this code to your program and use it to get sound files from Google, you may actually be violating the terms of use of Google Translate. Get legal advice regarding that before you use this code in product to get sound files from Google Translate.

这篇关于试图从URL的Java播放声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆