Question:
Turning a file path into a URL, read details and how do I do it?
Hunter
2010-03-15 23:06:49 UTC
C:\Users\owner\Documents\JCreator LE\cs_ap\GridWorld\GridWorldCode\projects\MichaelCritter\Thriller.wav

The above file path needs to be turned into a URL usable in Java (using JCreator).
The intended purpose for this is to use the getAudioClip method, but it requires as a parameter a URL of the file path from which I am trying to get a wav file.
so it goes something like this:
ac = new getAudioClip(URL);

So how do I turn the above file path into a URL for this purpose?
Three answers:
deonejuan
2010-03-16 00:53:02 UTC
If you do a

System.out.println( url.toString() );

that will help you debug your path



So often, when using an IDE, you have source files in one folder, and the IDE makes another folder for the .class files. Therefore, the JRE is pointing to the main() with the .class code and the sound file is not there because the human has the sound file with the source code. So, first put the sound file with your .class -- wherever else it may be.



Second, getAudioClip() is an interface for Applet audio functionality.



import sun.audio.*; //import the sun.audio package

import java.io.*;

//** add this into your application code as appropriate

URL url = this.class.getResource( "gimmeShelter.wav" );

String filename = url.getPath(); // this will be the C:/blah_blah/blah .. correct everything slashes

// Open an input stream to the audio file.

InputStream in = new FileInputStream( filename );

// Create an AudioStream object from the input stream.

AudioStream as = new AudioStream(in);

// Use the static class member "player" from class AudioPlayer to play

// clip.

AudioPlayer.player.start(as);

// Similarly, to stop the audio.

AudioPlayer.player.stop(as);
korgrue
2010-03-15 23:08:56 UTC
Put the file into the same folder as your java file and just use the file name of the audioclip as the path.



Or you can put it into a sub folder, named, music and use the relative path.



/music/song.mp3
brustrom
2016-12-07 14:49:21 UTC
nicely every time i try turning a information superhighway website right into a link..i reproduction the URL like verify it truly is highlighted perfect click and press "reproduction" then i "paste" it the position i favor it to be or bypass...i'm no longer certain if it will artwork on myspace, yet try it and see if it truly works!! sturdy luck!! =D


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...