Trying out the audio clip - java

Whenever I try the audio clip, I get this error:
java.net.MalformedURLException: no protocol: /Users/videogames/Documents/workspace/TryApplets/res/adv.wav---------
What's the problem? Here's the code for the program. (I use a mac, if that matters at all)
package game;
import java.applet.*;
import java.net.*;
public class sound {
/**
* #param args
*/
public static void main(String[] args) {
try {
URL url = new URL("/Users/videogames/Documents/workspace/TryApplets/res/adv.wav");
AudioClip clip = Applet.newAudioClip(url);
clip.play();
} catch (MalformedURLException murle) {
System.out.println(murle);
}
}
}

An URL must start with something like http://.. or file://... The URL shown does not, it is not a valid URL.

Valid protocols for the URL class are
http, https, ftp, file, and jar
so try
URL url = new URL("file://Users/videogames/Documents/workspace/TryApplets/res/adv.wav");
If in doubt read the API
http://docs.oracle.com/javase/7/docs/api/java/net/URL.html#URL(java.lang.String)

Related

How to Open a PDF at a Named Destination

I need to write a Java program that opens a PDF file at a named destination. The file test.pdf contains the named destination "DestinationX" on page 2. The program opens the PDF file but does not go to the named destination. How do I get to the named destination?
import java.awt.Desktop;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
public class MyLauncher {
static void openFileAtNamedDest(){
if (Desktop.isDesktopSupported()) {
try {
URI myURI = new URI("file:///C:/test.pdf#nameddest=DestinationX");
Desktop.getDesktop().browse( myURI );
} catch (IOException e) {
e.printStackTrace();
}
catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
openFileAtNamedDest();
}
}
According to the spec, the format of your URL is correct. The only question is what application you are actually launching via browse(). I think it acts the same way as if you had double-clicked the file's icon on your desktop: it will launch whatever application is registered as the default handler for PDFs.
Acrobat should be able to handle a URL with a named destination, but other PDF viewers may not support it.

MalformedURLException no protocol. How to fix?

So I have this Code:
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
public class SoundTest {
public static void main(String[] args) throws Exception {
URL url = new URL("Sprites/Omni's Tones/New tones/bubblespawn_01.wav");
AudioClip clip = Applet.newAudioClip(url);
AudioClip clip2 = Applet.newAudioClip(url);
clip.play();
Thread.sleep(1000);
clip2.loop();
Thread.sleep(20000);
clip2.stop();
System.out.println("end");
}
}
An error Appears:
Exception in thread "main" java.net.MalformedURLException: no protocol: Sprites/Omni's Tones/New tones/bubblespawn_01.wav
at java.net.URL.<init>(URL.java:586)
at java.net.URL.<init>(URL.java:483)
at java.net.URL.<init>(URL.java:432)
at com.edu4java.minitennis7.SoundTest.main(SoundTest.java:10)
How do I fix this? Thank you!
It seems that I need to add more lines to my code,
If you are running it inside the Applet then try with Applet#getCodeBase() and Applet#getDocumentBase
URL url = getDocumentBase();
AudioClip audioClip = getAudioClip(url, "music/abc.wav")
If it's running in a standalone application then choose one based on music file location:
// Read from same package
InputStream inputStream = getClass().getResourceAsStream("abc.wav");
// Read from music folder parallel to src in your project
File file = new File("music/abc.wav");
// Read from src/music folder
URL url = getClass().getResource("/music/abc.wav");
// Read from src/music folder
InputStream inputStream = getClass().getResourceAsStream("/music/abc.wav");

Error getting audio input stream from input URL

I am trying to play a .WAV in Java using the code from the javasound.info:
import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
public class LoopSound {
public static void main(String[] args) throws Exception {
URL url = new URL(
http://www.mediafire.com/listen/vgtpaigmj3un9ii/Untitled34.wav");
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// A GUI element to prevent the Clip's daemon Thread
// from terminating at the end of the main()
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}
}
But I am getting the following error:
Exception in thread "main" javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL
at javax.sound.sampled.AudioSystem.getAudioInputStream(AudioSystem.java:1153)
at LoopSound.main(Wanderando.java:13)
I assume that the problem is with the URL itself and not the code, but I cannot for the life of me come across a service to upload and stream a .WAV file so that the program can capture the audio like in the original code with the :
http://pscode.org/media/leftright.wav
I am not sure if the code would have to be tweaked a bit for the to support mediafire's player or if there is another service for upload that support this.
Any help would be very appreciated.

IOException markreset not supported with Java Sound on export

Ok so i made a game in java and i exported it. In Eclipse everything works perfectly but when i export the jar there are some problems. When you collide with another rectangle it should play a sound (In eclipse it works but not exported).
Here is my class for sounds:
package sound;
import java.io.InputStream;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class GameSounds
{
static String hitPath = "/resources/8bit_bomb_explosion.wav";
public static synchronized void hit()
{
try
{
InputStream audioInStream = GameSounds.class.getResourceAsStream(hitPath);
AudioInputStream inputStream = AudioSystem.getAudioInputStream(audioInStream);
Clip clip = AudioSystem.getClip();
clip.open(inputStream);
clip.start();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
and i used java -jar ProjectZero.jar to open up the console while playing and here is the error i get when it should play a sound:
java.io.IOException markreset not supported
at java.util.zip.InflaterInputStream.reset(Unknown Source)
at java.io.FilterInputStream.reset(Unknown Source)
at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unkno
wn Source)
at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
at sound.GameSounds.hit(GameSounds.java14)
at main.Main.doLogic(Main.java136)
at main.Main.run(Main.java100)
at java.lang.Thread.run(Unknown Source)
I tried exporting the resources into the jar but no success.
I tried putting the resources folder in the same folder with the jar but it doesn't work either.
Java Sound requires a repositionable input stream. Either use getResource(String) for an URL (out of which JS will create such a stream), or wrap the original stream to make it so.
E.G. copied from the Java Sound info. page.
import java.net.URL;
import javax.swing.*;
import javax.sound.sampled.*;
public class LoopSound {
public static void main(String[] args) throws Exception {
URL url = new URL(
"http://pscode.org/media/leftright.wav");
Clip clip = AudioSystem.getClip();
// getAudioInputStream() also accepts a File or InputStream
AudioInputStream ais = AudioSystem.
getAudioInputStream( url );
clip.open(ais);
clip.loop(Clip.LOOP_CONTINUOUSLY);
SwingUtilities.invokeLater(new Runnable() {
public void run() {
// A GUI element to prevent the Clip's daemon Thread
// from terminating at the end of the main()
JOptionPane.showMessageDialog(null, "Close to exit!");
}
});
}
}
See also the embedded-resource info. page.

How to give FTP address in java?

I have written the code which downloads the file from FTP server. Since I have my FTP server locally and I want to access like "ftp://localhost/alfresco". It was alfresco's FTP.
I have the following Code
public class FtpTransfer {
public static final void main(String[] args)
{
FTPClient ftp = new FTPClient();
FileOutputStream br = null;
try
{
ftp.connect("ftp://localhost/alfresco");
ftp.login("admin", "admin");
String file = "KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml";
br = new FileOutputStream("file");
ftp.retrieveFile("/"+file, br);
System.out.println("Downloaded...");
}
catch(IOException exception) {
System.out.println("Error : "+exception);
}
}
}
The following exception occurs.
Error : java.net.UnknownHostException: ftp://localhost/alfresco
Please let me know how should I give the FTP Host Address?
FTPClient f = new FTPClient();
f.connect("localhost");
f.login(username, password);
FTPFile[] files = listFiles(directory);
Also See
Article from JavaWorld
JavaDoc
Here is an example demonstrating connection to a server, changing present working directory, listing files in a directory and downloading a file to some specified directory.
package test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
public class FtpTransfer {
public static final void main(String[] args) throws SocketException, IOException {
FTPClient ftp = new FTPClient();
ftp.connect("ftp.somedomain.com"); // or "localhost" in your case
System.out.println("login: "+ftp.login("username", "pass"));
ftp.changeWorkingDirectory("folder/subfolder/");
// list the files of the current directory
FTPFile[] files = ftp.listFiles();
System.out.println("Listed "+files.length+" files.");
for(FTPFile file : files) {
System.out.println(file.getName());
}
// lets pretend there is a JPEG image in the present folder that we want to copy to the desktop (on a windows machine)
ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // don't forget to change to binary mode! or you will have a scrambled image!
FileOutputStream br = new FileOutputStream("C:\\Documents and Settings\\casonkl\\Desktop\\my_downloaded_image_new_name.jpg");
ftp.retrieveFile("name_of_image_on_server.jpg", br);
ftp.disconnect();
}
}
Try remove protocol ("ftp://") from your url.
And please, look at the example.
The FTPClient.connect() method takes the name of a server, not a URL. Try:
ftp.connect("localhost");
Also, you may need to put alfresco somewhere else. If it's part of the file path,
String file = "alfresco/KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml";

Categories