Trying to play sound from a URL Java -


i having troubles playing sound url.

here current code,

    public static void aplay(string url) throws     malformedurlexception, unsupportedaudiofileexception,      ioexception, lineunavailableexception {        clip c = audiosystem.getclip();       audioinputstream = audiosystem.getaudioinputstream(new url(url));       clip c = audiosystem.getclip();       c.open(a);       c.start(); } 

also, have method in 'main' method. put https://translate.google.com/translate_tts?ie=utf-8&tl=en&q=hi%20there 'url' , not work, , respond 403 (server returned http response code: 403). wondering if fix this.

thanks, dan

the url sound file google translate. in order prevent misuse of google services, server performs few heuristic checks tries tell whether it's human or automated service accessing url. that's why replies 403, means "forbidden".

disclaimer: consider why forbid , check terms of usage.

opening url directly in browser chrome works. retrieving url tool wget or class url doesn't - @ least not out-of-the-box.

you need change user-agent property of http request in order fake different user agent. can connecting "manually" , changing "user-agent" request property.

the following wget.java listing demonstrates how so:

import java.io.*; import java.net.*;  public class wget {     public static void main(final string... args) throws ioexception {         (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];         (int bytesread; (bytesread = in.read(buf)) != -1; )             out.write(buf, 0, bytesread);         out.flush();     } } 

audiosystem can used inputstream. so, following aplay() method should work:

public static void aplay(string url) throws unsupportedaudiofileexception, ioexception, lineunavailableexception {     clip c = audiosystem.getclip();     audioinputstream = 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(); } 

disclaimer: showing technical solution. if add code program , use sound files google, may violating terms of use of google translate. legal advice regarding before use code in product sound files google translate.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -