How to play .mp3 and .wav in Java?

0 votes
Please guide me.
Jul 9, 2018 in Java by Akrati
• 3,150 points
202 views

2 answers to this question.

0 votes

Java FX has Media and MediaPlayer classes which will play mp3 files.

Example:

String bip = "bip.mp3";
Media hit = new Media(new File(bip).toURI().toString());
MediaPlayer mediaPlayer = new MediaPlayer(hit);
mediaPlayer.play();

You will need the following import statements:

import javafx.scene.media.Media;
import javafx.scene.media.MediaPlayer;
answered Jul 9, 2018 by Daisy
• 8,050 points
I think these classes only works with JavaFX Toolkit it doesnt work with JFrame
0 votes

We can use Java API to play .wav :

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
AudioInputStream audioInput = AudioSystem.getAudioInputStream(MyClazz.class.getResource("musicDemo.wav"));
Clip clip = AudioSystem.getClip();
clip.open(audioInput);
clip.start();
answered Dec 7, 2018 by Sushmita
• 6,880 points

Related Questions In Java

0 votes
2 answers

When and how to use Super() keyword in Java?

super() is used to call immediate parent. super() can be ...READ MORE

answered Jul 9, 2018 in Java by Sushmita
• 6,880 points
122 views
0 votes
1 answer

how to play sound in java?

 The following code will work fine. But I ...READ MORE

answered Jun 20, 2018 in Java by scarlett
• 1,280 points
130 views
0 votes
1 answer

How to convert or cast hashmap to JSON object in Java, and again convert JSON object to JSON string?

You can use: new JSONObject(map); READ MORE

answered Jun 27, 2018 in Java by Akrati
• 3,150 points
2,056 views
+1 vote
3 answers

What is the syntax to declare and initialize an array in java?

You can use this method: String[] strs = ...READ MORE

answered Jul 25, 2018 in Java by samarth295
• 2,190 points
563 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 10,053 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
2,261 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
5,889 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
685 views
0 votes
1 answer

How to getters and setters work in java

In Java, getter and setter are two ...READ MORE

answered Jun 1, 2018 in Java by Daisy
• 8,050 points
261 views
+2 votes
1 answer

How to import an existing x509 certificate and private key in Java keystore to use in SSL?

keytool does not provide such basic functionality ...READ MORE

answered Dec 19, 2018 in Java by Daisy
• 8,050 points
339 views