I've been working on a really simple mp3 player in java for some weeks now and I'm wondering if I can do playlist with a BufferedReader and BufferedWriter method. For example with JFileChooser I'd choose song and store its path to file (for example "playlist.txt") and then in while loop (BufferedReader.available()) I'd read path and play it. Is this possible? Or Is it better to set chooser.setMultiSelectionEnabled() to true and work in this direction?
Seems like the best solution for this would be making a BufferedReader over the FileReader to read the file. Then you can make java.io.File objects and put those into a java.util.ArrayList. Then you can get the files easily from from the ArrayList and dispose the readers. You can also add an refresh method to refresh the list if needed.
What I did was use a for loop like this:
File playlist = new File("U:\\186Project\\Project\\data");
for(File f : playlist.listFiles()){
song = minim.loadFile(f.getName(), 2048);
......
}
I was using Processing to make LEDs light up to the beat of music, if you want to check out the full code out go here.
This won't let you choose certain songs but it does cycle through them like a playlist. Make sure to change "U:\186Project\Project\data" to whatever directory has all your MP3 files in it.
Related
Start by saying that I have not great experience in Java and I've done a lot of research. I would please ask you a specific question.
Thank you
I need to open a file for reading and writing from which I read and write a 512-byte blocks.
The file is fixed length and the information to be written will overlap with other existing.
For example, I read the first 512 bytes of the file and if it contains certain values write a block 512 to position 2048.
I tried using FileInputStream and FileOutputStream but every time I open with FileOutputStream the contents of the file are deleted.
It can be done with Java?
Roberto
Use a FileChannel; it allows random access to any part of a file, in read, write or any combination of both.
Example:
final Path path = Paths.get("path/to/the/file");
final FileChannel channel = FileChannel.open(path, relevantOptions);
Optionally, after that, you can use the .map() method.
I am a beginning Java programmer that is new around this site. I am working on a Java project that involves the saving and reloading of .txt files. I realize that there are many similar questions out there, but none of them are exactly what I am looking for. If I have mistaken, I am very sorry.
I am trying to design a Java program for a classroom when the teacher wants to assign new seats. First, I am going to explain my vision for the program. The program will allow you to type in the students' names, which it will separate into two separate ArrayLists, one for the boys in the class and another for the girls. I want these ArrayLists to be saved somehow into a .txt file so they can be read later. I have done this before with simple strings:
PrintWriter out = new PrintWriter("list.txt");
out.println("Bobby Joe");
out.println("John Doe");
out.close();
Is there a different way to do this with ArrayLists? The PrintWriter worked perfectly when I used simple strings. Another part of the program will read those .txt files and import them into ArrayLists again. There, it can use the ArrayLists to assign seats for the children in the class. I am completely unaware how to load .txt files.
I am pretty sure these are the only two parts of the program I will need help with, I can use the randomizer and other simple methods to program the rest.
I would really appreciate if you could help me create my Java program. Thank you!
I assume you want the names to be written line-by-line.
Edit your writing method to write the names from a list:
PrintWriter out = new PrintWriter("list.txt");
list.forEach(out::println);
out.close();
And then you can read them with something like this:
List<String> list = Files.lines(Paths.get("list.txt")).collect(Collectors.toList());
I propose to use for this serialization:
Example
Just write/read full ArrayList object to/from file.
is there anyway to play a file that is being dynamically added to, using android media player ?
I tried using RandomAccessFile :
public static RandomAccessFile tempMp3;
tempMp3 = new RandomAccessFile(Environment.getExternalStorageDirectory()+"/ggg", "rw");
StaticData.tempMp3.setLength(file_size);
StaticData.tempMp3.seek(0);
//Here I call the auto update function and after X bytes are written I call playMediaPlayer
StaticData.mediaPlayer.setDataSource(StaticData.tempMp3.getFD());
StaticData.mediaPlayer.prepare();
StaticData.mediaPlayer.start();
So basically I am updating and playing at the same time.
The problem is that MediaPlayer and my Update function both are accessing and moving the file pointer, which messes up the MediaPlayer.
Is there any workaround ?
Can I use separate file pointers ?
Solved this.
Java NIO to the rescue.
Using memory-mapped IO, we can write to our file, without disturbing the file pointer, so the android media player is happy. Here is a helpful link.
The trick is to set the size of our MappedByteBuffer = size of music file.
We also need to ensure that the media player pauses if we have not yet written to the buffer. This can be achieved by monitoring the file-pointer.
Suppose I have a sound file that I wish to play in this manner:
Start from the 0:00 mark.
When the sound file ends, replay from the 0:30.00 mark.
Essentially, I wish for the song to play from 0:00 to 0:30, then loop from 0:30 to the end.
How do you do this?
Usually, when playing Audio Files, I use AudioClip.
AudioClip audio = Applet.newAudioClip( this.getClass().getResource( "..." ) );
audio.loop();
It's because of bad experiences with loading the audio file when the project is packed into a .jar file. If the method you recommend uses another class, I would still be more than happy to listen. However, if you've encountered the same problem as I have and know how to fix it, please do tell me.
You might use a Clip for this, or if the clip is too large, feed the bytes directly to a SourceDataLine.
I have a program that will go through and create multiple different class instances. I want to write the details of each instance to a file using DataOutputStream (it's a necessary exercise, I'll look at other ways of doing this later), but the problem is I noticed that DataOutputStream overwrites the file each time a new instance is created and written. My first idea was each time a new instance is written, first using DataInputStream to get what's in the file, save it, and then rewrite it with the new instance. This seems like it could get confusing very fast. What would be best practice for something like this? Thanks in advance.
EDIT: I will try and be a bit more specific about what I'm trying to do here.
When I take the class that I want to write to the file, first I'll use an dataInputStream.readFully to get everything in the file. My understanding is that takes all the bytes in the file and stores them in an array. I would like to compare this with the class instance and if the instance matches something already in the file, don't output this particular instance (because it's already there) to the file. Otherwise, append to the file.
Use the FileOutputStream(File file, boolean append) constructor when you open the file for writing. For example:
File f = new File("C:\data.txt");
FileOutputStream fos = new FileOutputStream(f, true); // open file for appending
DataOutputStream dos = new DataOutputStream(fos);
// anything written to dos after this point will be appended
If you just need to serialize your objects, I'd highly recommend using JAXB or another serialization/marshaling API instead of reinventing the wheel. You'll potentially save a ton of time.