Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,322 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,004 people online right now. Registration is fast and FREE... Join Now!




how to play mp3 files

 

how to play mp3 files

narmer93

23 Jul, 2008 - 11:35 AM
Post #1

D.I.C Regular
***

Joined: 13 Mar, 2008
Posts: 315



Thanked: 8 times
My Contributions
i want to play an mp3 or audio file in java desktop application without opening media player or real player as in vb.net
vb
my.computer.audio.play("audio location")

i need to make a similer thing in java and the form is not visible
i guess i cac make the visible property to false
and if it isn't possible to make the audio replay ,how can i close the form after the audio is finished?
i have no idea about that and this is NO HOMEWORK biggrin.gif

User is offlineProfile CardPM
+Quote Post


lordms12

RE: How To Play Mp3 Files

23 Jul, 2008 - 12:41 PM
Post #2

D.I.C Regular
Group Icon

Joined: 16 Feb, 2008
Posts: 335



Thanked: 26 times
Dream Kudos: 250
My Contributions
You can use the following code to run wav, au or aiff files if you want to run mp3 you would need to use library like JMF or any other library.

java
import java.io.File;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
import javax.swing.JFrame;

public class WavPlayer {

Clip clip;

public WavPlayer(File audioFile){
init(audioFile);
}

private void init(File audioFile){
try {
AudioInputStream soundStream = AudioSystem.getAudioInputStream( audioFile );
AudioFormat audioFormat = soundStream.getFormat();
// define line information based on line type,
// encoding and frame sizes of audio file
DataLine.Info dataLineInfo = new DataLine.Info(
Clip.class, AudioSystem.getTargetFormats(
AudioFormat.Encoding.PCM_SIGNED, audioFormat ),
audioFormat.getFrameSize(),
audioFormat.getFrameSize() * 2 );

// make sure sound system supports data line
if ( !AudioSystem.isLineSupported( dataLineInfo ) ) {
System.err.println( "Unsupported Clip File!" );
return;
}
// get clip line resource
clip = ( Clip ) AudioSystem.getLine( dataLineInfo );
// open audio clip and get required system resources
clip.open( soundStream );
}
catch ( Exception e) {
e.printStackTrace();
}
}

public void play(){
clip.start();
}

public static void main(String[] args) {
JFrame frm=new JFrame("sound");

WavPlayer clipPlayer = new WavPlayer(new File("C://1.wav"));
clipPlayer.play();

frm.setSize(200, 300);
frm.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frm.setVisible(true);
}
}

User is offlineProfile CardPM
+Quote Post

1lacca

RE: How To Play Mp3 Files

24 Jul, 2008 - 12:29 AM
Post #3

code.rascal
Group Icon

Joined: 11 Aug, 2005
Posts: 3,822



Thanked: 31 times
My Contributions
Or simply search this forum, we had at least 3 threads on mp3 playback in Java a couple of weeks ago some of them with complete source code.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/7/09 02:40PM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month