I have made a GUI which is pretty simple, looking similar to this:
Where I simply do
Open to browse a music file
Play to play the music
Stop to stop the music
Now I came to a problem where I don't really know how to make it, so my program understands the audio. However I started with this:
if (e.getSource() == btnOpen) {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showOpenDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
file = fc.getSelectedFile();
//This is where a real application would open the file.
System.out.println("File: " + file.getName() + ".");
} else {
System.out.println("Open command cancelled by user.");
}
System.out.println(returnVal);
}
Which opens the browser and lets me choose a file. The only problem now is that I don't really know and haven't found on Google how to setup as an audio file. So basically what I want is like in the picture. When I open an MP3 file, it should show text of what it's called (But it's simple, just to edit the Jlabel which I can make later on) and the Play and stop.
The main problem now is how to make so whenever I choose an audio file (mp3) it should understand it and when pressing Play, it should play the song. but I have not found any solutions on Google so this is what I need help with.
Edit: So I luckliy with adding JAR and so on got mp3 to work but now to Wav. Since I haven't found anywhere how to have both Wav and Mp3 and make it work as a player. So my question is, How could I make it work?
Related
I'm currently making an Android app that will ask some questions and then put the results into a text file for processing in another application. As far as I can tell, everything is working except the text file part. I've done a bunch of looking online and haven't found an answer. Here is the relevant part of my code so far:
try {
String test = "Test";
File myObj = new File("test.txt");
new FileWriter(myObj, Boolean.parseBoolean(test));
} catch (Exception e) {
e.printStackTrace();
}
I have this code set to go off when the button is pressed. I have confirmed the button works because I can replace the code above with System.exit(0) and it will exit the app. After I press the button I will search my tablet for text files but no text files were created. Is there something obvious I'm missing?
Thanks for any assistance provided.
I am writing an app that takes Morse code, and plays it over the speakers.
Currently I am able to record audio over the microphone using this code:
public void startRecord() throws Exception{
if (record != null){
record.release();
}
File fileOut = new File(FILE);
if (fileOut != null){
fileOut.delete(); // delete any existing file at that location.
}
record = new MediaRecorder();
record.setAudioSource(MediaRecorder.AudioSource.MIC);
record.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
record.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
record.setOutputFile(FILE);
record.prepare();
record.start();
}
and i am able to generate morse code in a string formatted like this:
"-.... .---- -.... -.-. -.... ..... --... ---.."
I can iterate over this string using a for loop such as this:
char[] chars = message.toCharArray();
for (char ch : chars) {
//add to audio file
}
But I am not sure how to create a file out of strung together wav files. Ive seen some posts that mention setting the audio source as a file from the device, but Im not sure how to pick and choose which file and where to insert them, or how to compile it all into a single audio file.
Instead of creating a new sound file and playing that, it would probably be easier to just play each sound individually and when that sound finishes, you play the next sound, or you wait for a brief pause if it's a space.
I think you are trying to do this the harder way. What if you were to simply have the program read the first letter, play the appropriate sound, do the same for the next letter and so on throughout the text. I believe it is much simpler but if you are really set on trying to put it into one file you could have the program make an empty file but in the name it sets have the extension `.wav` or `.mp3` and do research into how they are encoded.
I manage to make a program that could understand mp3 in Java (by jaco.mp3 jar) and it works. However this means that the .Wav files doesn't work of course. So I fixed so that I could use the Wav but the problem will then be that I can't use the mp3 then. so I found out that I could use the File Extension and my idea was to make like this:
If the last 3 or 4 letters ends with .mp3 then do the mp3 method, if its .Wav then to Wave method. but I don't really know how to manage it together, I was thinking more like a Switch-statement. But I really never worked with File extension before and could need som help with that!
However this is my mp3 method that is like this right now:
public void Choose() {
String userDir = System.getProperty("user.home");
JFileChooser fileChooser = new JFileChooser(userDir +"/Desktop");
int returnValue = fileChooser.showOpenDialog(null);
if (returnValue == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
System.out.println(selectedFile.getName());
mp3_player = new MP3Player(selectedFile);
lblPlayURL.setText(selectedFile.getName());
}
}
}
as you can see I have a JFileChooser where I pick the song I want of course and I still want to do it, only that the different is now it should work both as .Wav and .mp3. But I don't really know how to go further with it. so any help is needed from you guys! :)
Before people is trying to give any suggestions from a thread that says Playing .Wav and .Mp3. I would just say first that I have read it and there is only answers about each of them, Only mp3 or the .Wav. not both. so thats why I created this thread because I need help!
You can perhaps check if the file path ends with MP3 or WAV and have an if statement to run different programs depending on the condition.
This can be done like so:
String ext = selectedFile.getPath();
if(ext.endsWith(".wav"))
{
// A WAV file was chosen
}
else if(ext.endsWith(".mp3"))
{
// An MP3 file was chosen
}
else
{
// Something else was chosen
}
EDIT: This method together with Bogdan's answer is probably your best bet.
Add FileFilter rules to your JFileChooser to match .mp3 and .wav files:
fileChooser.addChoosableFileFilter(new FileTypeFilter(".mp3", "MP3 Files"));
fileChooser.addChoosableFileFilter(new FileTypeFilter(".wav", "WAV files"));
How to add file filter for JFileChooser dialog.
I am trying to program a classroom assistant (I work as a teacher) using Java who will give spoken instructions to students/ask them questions etc. I have managed to connect successfully to the cerevoice cloud to create an ogg file
e.g. "https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg"
however when I try to play this ogg file from java I get an error that it could not be found or that it is an unsupported audio resource depending on the ogg player I use (i have tried EasyOGG and TinySound so far) - both the ogg players work successfully locally but I cannot get them to play a file directly from the website.
Examples how I have tried to reference it:
URL url = new URL("https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg");
Music song = TinySound.loadMusic(url);
song.play(true);
OggClip ogg = new OggClip("https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg");
ogg.loop();
Apologies for my ignorance I'd really appreciate any help anyone can give with playing this file! =)
Many thanks,
Darren
Edited to show using a literal String value to help clarity
Edit very ugly hack 1 solution: //opens a browser window plays file then closes window
String url = "https://cerevoice.s3.amazonaws.com/Heather220501c8c2e94d4650f64f7d951bf76b08b0eb.ogg"; //hard coded here for simplicity but URL dynamically retrieved from webservice
java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
Thread.sleep(3000);
Runtime.getRuntime().exec("taskkill /F /IM chrome.exe");
Edit slightly less ugly hack 2 solution:
//creation of a temporary file to allow playing from java without creating a browser window
try{
Files.deleteIfExists(Paths.get("C:/cere/temp.ogg"));
try (InputStream in = URI.create(url.value).toURL().openStream()) {
Files.copy(in, Paths.get("C:/cere/temp.ogg"));
}catch(IOException e) {
}
So this is what I want to do. I want to load an .wav music file from my project folder. I have done this with a image, as displayed below. I want to do this with an audio file. Is this possible? Is there an easy way to convert my old method to a new one. The reason I want to do this is because I want to have all the files for my program in a .jar file so I can just send that and it plays.
static audiotest music = new audiotest( "C:\\WINDOWS\\Media\\POL-purple-hills-short.wav" );
music.start();
URL mountainImage2 = Main.class.getResource("Mountains.png");
mountainImage = ImageIO.read(mountainImage2);