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.
Related
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 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?
Hi I am trying to make one pane that shows something like windows explorer in my computer.
when user complete it's operations, and after that when he want to save edited image at specific place on disk then he can easily select directory from that pane.
i want to design something like this :
is it possible to do something like that ?
my picture editor looks like :
at right side of editor i want to put something like output directory selection pane.
is anyone know how to do that ?
A complete example using JTree is examined in FileBrowser.
An alternative using Outline is shown here.
Yes its possible. It's basically just JTree.
You will probably want to take a look at File#listRoots, File#isDirectory and File#listFiles.
You'll also want to take a look at How to use trees.
You'll probably also want to take a look at FileSystemView#getSystemIcon which will allow you to look an appropriate icon for the given File
However, it might be simpler to just use a JFileChooser ;)
You could have a look at JFileChooser.
You can use this object to open a SaveDialog and get a save path on the local harddisk.
Then eventually use an ObjectOutputStream to write a file.
Sample code:
JFileChooser c = new JFileChooser();
c.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
// Demonstrate "Save" dialog:
int rVal = c.showSaveDialog(fb);
if (rVal == JFileChooser.APPROVE_OPTION) {
System.out.println(c.getSelectedFile().toString());
}
This can be handled with a JFileChooser, sorry if it's not the solution you're looking for
Note: you say choose a directory but I assume you mean that they can name their file
private File selectSaveFile() {
JFileChooser fc = new JFileChooser();
fc.setFileFilter(new FileNameExtensionFilter("File Type", "txt"));
fc.setCurrentDirectory(new File(System.getProperty("user.home")));
int returnVal = fc.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
return fc.getSelectedFile();
}
//the user didn't click save if we are here
return null;
}
So basically say i have a file that is simply called settings, however it has no extension, but contains the data of a text file renamed.
How can i load this into the file() method in java?
simply using the directory and file seems to make java think its just a directory and not a file.
Thanks
In Java, and on unix, and even on the filesystem level on windows, there is no difference in if a file has an extension or not.
Just the Windows Explorer, and maybe its pendants on Linux, use the extension to show an appropriate icon for the file, and to choose the application to start the file with, if it is selected with a double click or in similar ways.
In the filesystem there are only typed nodes, and there can be file nodes like "peter" and "peter.txt", and there can be folder nodes named "peter" and "peter.txt".
So, to conclude, in Java there is really no difference in file handling regarding the extension.
new File("settings") should work fine. Java does not treat files with or without extension differently.
Java doesn't understand file extensions and doesn't treat a file any differently based on its extension, or lack of extension. If Java thinks a File is a directory, then it is a directory. I suspect this is not what is happening. Can you try?
File file = new File(filename);
System.out.println('\'' + filename + "'.isDirectory() is "+file.isDirectory());
System.out.println('\'' +filename + "'.isFile() is "+file.isFile());
BTW: On Unix, a file file. is different to file which is different to FILE. AFAIK on Windows/MS-DOS they are treated as the same.
The extension should not make a difference. Can you post us the code you are using? And the error message please (stack trace).
Something along these lines should do the trick (taken from http://www.kodejava.org/examples/241.html)
//
// Create an instance of File for data file.
//
File file = new File("data");
try {
//
// Create a new Scanner object which will read the data
// from the file passed in. To check if there are more
// line to read from it we check by calling the
// scanner.hasNextLine() method. We then read line one
// by one till all line is read.
//
Scanner scanner = new Scanner(file);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
I adapted this tutorial (http://www.screaming-penguin.com/node/7749) to an Android app I've built to allow for a button press to export the current database to the user's sdcard. It works flawlessly.
But I'm afraid that users of my app would be unfamiliar with the db file, and I'm looking for a way to convert that to a more user-friendly format. I came across this thread (http://groups.google.com/group/android-beginners/browse_thread/thread/4e53ebca14daecfc), which recommends "querying data from the database and writing the data into a csv file."
I was hoping someone could point me in the right direction to begin figuring out how to do this. I'm finding it hard to track down more information about the specific method.
Or does it make more sense to just explain in a short "about" how to read and access .db files?
Thanks
EDIT: I also have a question about the sqlite export process, which I think I'll just ask here rather than create a new question. Is there a way to modify the code below so that each export would receive either a string representation of the date or just a single numeral appended to it? Right now if you export a second time, it automatically overwrites the old file. Thanks.
protected Boolean doInBackground(final String... args) {
File dbFile = new File(Environment.getDataDirectory() +
"/data/com.example.example/databases/data");
File exportDir = new File(Environment.getExternalStorageDirectory(), "exampledata");
if (exportDir.exists()) {
exportDir.mkdirs();
}
File file = new File(exportDir, dbFile.getName());
try {
file.createNewFile();
this.copyFile(dbFile, file);
return true;
} catch(IOException e) {
Log.e(MyApplication.APP_NAME, e.getMessage(), e);
return false;
}
}
I was hoping someone could point me in the right direction to begin figuring out how to do this.
To read in the data, use rawQuery().
To write the data, use Java I/O. There are also open source CSV libraries for Java that may work on Android.
Is there a way to modify the code below so that each export would receive either a string representation of the date or just a single numeral appended to it?
Use string concatenation to add whatever you want to the filename.
Also, please get rid of:
File dbFile = new File(Environment.getDataDirectory() +
"/data/com.example.example/databases/data");
and replace it with:
File dbFile=getDatabasePath("data");