2

I have made a GUI which is pretty simple, looking similar to this:

enter image description here

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?

WeInThis
  • 537
  • 2
  • 8
  • 22
  • 1
    The mp3plugin.jar of the JMF core API can add MP3 functionality to [tag:javasound], but the [`MediaPlayer`](https://docs.oracle.com/javafx/2/api/javafx/scene/media/MediaPlayer.html) of Java-FX can also play MP3s. – Andrew Thompson Nov 11 '15 at 15:23
  • Possible duplicate of [Playing .mp3 and .wav in Java?](http://stackoverflow.com/questions/6045384/playing-mp3-and-wav-in-java) – Petter Friberg Nov 11 '15 at 15:23
  • Hmm. so I kinda need a API to get it to mp3? also how is it possible to combine both JfileChooser and Audio together? – WeInThis Nov 11 '15 at 15:24
  • *"..how is it possible to combine both JfileChooser and Audio together?"* That question sounds like 'how do I combine pizza and politics?' In other words, it makes no sense at all. You do not 'combine audio and file chooser', though you might try to play the audio in a file chosen using the file chooser. – Andrew Thompson Nov 11 '15 at 15:28
  • Exactly, It was bad of me. My english isn't my strongest side but as you meant., I meant like choose a audio file and then make it play :) – WeInThis Nov 11 '15 at 15:29
  • *"..choose a audio file and then make it play"* So you are OK with choosing a file, it is just a matter of playing that file? Narrow it down, do you want to play it with `MediaPlayer` or Java Sound? The first is described in the accepted answer to the question linked by @PetterFriberg, I also mention the 2nd approach (using Java Sound) in an answer to that same question. Please don't expect us to spoon feed information to you. Follow links, if you don't understand something in them, ask a specific question. – Andrew Thompson Nov 11 '15 at 15:33
  • If I ask like this then, Is there possible to make it work without addning Jar's and so on? and by that I mean only have a button that alloweds you to select a file and by clicking Play it should play the song right off? In that case, is that a Java sound or Mediaplayer? – WeInThis Nov 11 '15 at 15:41
  • I got mp3 to work but now I don't know how to make so both Wav and mp3 can be choosen. No one in the other thread said about both – WeInThis Nov 11 '15 at 17:19
  • You can branch based on the file extension, i.e., whether it is wav or mp3. You should be able to determine that with some basic String functions on the file name. – Phil Freihofner Nov 11 '15 at 21:08
  • Something like [this](http://stackoverflow.com/questions/29836255/playing-multiple-sound-clips-using-clip-objects/29836764#29836764) or [this](http://stackoverflow.com/questions/24274997/java-wav-player-adding-pause-and-continue/24275168#24275168) – MadProgrammer Nov 11 '15 at 22:16

0 Answers0