0

My PC has 3 sound cards. I can select and then play a .wav file using this code

import javax.sound.sampled.*

AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);

Mixer.Info[] arrMixerInfo = AudioSystem.getMixerInfo();

// Get a sound clip resource.
Clip clip = AudioSystem.getClip(arrMixerInfo[1]);

// Open audio clip and load samples from the audio input stream.
clip.open(audioIn);
clip.start();
clip.drain();
clip.close();

How can I play .mp3 file?

Crystal Meth
  • 589
  • 1
  • 4
  • 16
Julius Prayogo
  • 133
  • 3
  • 12

4 Answers4

0

On Linux you can use mpg123 command to play mp3 on different soundcards.

public class SoundTest {
    public static void main(String[] args) {
    //play mp3
        try {
         //my first soundcard is CA0106 with id: CARD=CA0106
            java.lang.Process p1 = Runtime.getRuntime().exec(new String[]{"mpg123", "-asysdefault:CARD=CA0106", "audio/1.mp3"});
         //my second soundcard is CARD=Intel (internal motherboard souncard)
            java.lang.Process p2 = Runtime.getRuntime().exec(new String[]{"mpg123", "-asysdefault:CARD=Intel", "audio/2.mp3"});
        } catch (Exception e) {
            e.printStackTrace();
        }
    //play wav
        try {
        java.lang.Process p3 = Runtime.getRuntime().exec(new String[]{"aplay", "audio/audio1.wav", "-Dsysdefault:CARD=Intel"});
        java.lang.Process p4 = Runtime.getRuntime().exec(new String[]{"aplay", "audio/audio2.wav", "-Dsysdefault:CARD=CA0106"});
     } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Soundcard ids you can get via command aplay -L It's not a Java way but was useful for my task.

-1

Do a Google search for "JavaZoom mp3". They provide an alternative and in my opinion better way of playing mp3's which does not require the JMF.

Basically, what this library does is to plug an mp3 decoder into your VM.

JMF and JavaZoom are pretty much the only usable alternatives, and I'd say JavaZoom is the easiest one.

sbrattla
  • 5,274
  • 3
  • 39
  • 63
  • I'm try to follow this site : http://www.javazoom.net/mp3spi/documents.html but it's only make a 'ssshhh...' sound, like television with no signal – Julius Prayogo Mar 18 '14 at 14:39
-2

THIS ANSWER WAS TAKEN FROM: How to play an mp3 file in java. I GIVE FULL CREDIT FOR THIS ANSWER TO THE ORIGINAL POSTER: Juned Ahsan


For this you'll need to install Java Media Framework (JMF) in your PC. One you have it installed,then try this piece of code:

import javax.media.*;
import java.net.*;
import java.io.*;
import java.util.*;
class AudioPlay
{
 public static void main(String args[]) throws Exception
 {


 // Take the path of the audio file from command line
 File f=new File("song.mp3");


 // Create a Player object that realizes the audio
 final Player p=Manager.createRealizedPlayer(f.toURI().toURL());


  // Start the music
  p.start();


  // Create a Scanner object for taking input from cmd
  Scanner s=new Scanner(System.in);


  // Read a line and store it in st
  String st=s.nextLine();


   // If user types 's', stop the audio
   if(st.equals("s"))
   {
   p.stop();
   }
 }
}

You may run into unable to handle formaterror, that is because Java took out the MP3 support by default (pirate copyright issue), you are required to install a “JMF MP3 plugin” in order to play MP3 file.

Go Java’s JMF website to download it http://java.sun.com/javase/technologies/desktop/media/jmf/mp3/download.html

To be sure that you are using a supported format file, check here:

http://www.oracle.com/technetwork/java/javase/formats-138492.html

If you are using windows7, you may have to read this as well:

https://forums.oracle.com/forums/thread.jspa?threadID=2132405&tstart=45

Community
  • 1
  • 1
Crystal Meth
  • 589
  • 1
  • 4
  • 16
-2

JavaFx have Media and MediaPlayer classes.

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;    

Media media = new Media(new File("sound.mp3").toURI().toURL().toExternalForm());
MediaPlayer mediaPlayer = new MediaPlayer(media);

mediaPlayer.play();

The difficult part is when you need to provide the url, you cannot just use sound.mp3 you need to provide the full URL (example: file:/C:/a/b/c/d/sound.mp3) that's why i used File class to get the URL (toExternalForm).

Another problem: you need to init JavaFX application or you cannot use anything.

It's an example which just plays the mp3

public class Main extends Application
{
    public static void main(String[] args)
    {
        Application.launch();
    }

    @Override
    public void start(Stage stage) throws Exception
    {
        Media media = new Media(new File("sound.mp3").toURI().toURL().toExternalForm());
        MediaPlayer mediaPlayer = new MediaPlayer(media);

        mediaPlayer.play();
    }
}
Marco Acierno
  • 14,682
  • 8
  • 43
  • 53