2 Replies - 21312 Views - Last Post: 24 July 2008 - 01:29 AM Rate Topic: -----

#1 narmer93  Icon User is offline

  • D.I.C Regular

Reputation: 8
  • View blog
  • Posts: 317
  • Joined: 13-March 08

how to play mp3 files

Posted 23 July 2008 - 12:35 PM

i want to play an mp3 or audio file in java desktop application without opening media player or real player as in vb.net
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 :D

Is This A Good Question/Topic? 0
  • +

Replies To: how to play mp3 files

#2 lordms12  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 30
  • View blog
  • Posts: 339
  • Joined: 16-February 08

Re: how to play mp3 files

Posted 23 July 2008 - 01:41 PM

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.

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);
	}
}

Was This Post Helpful? 0
  • +
  • -

#3 1lacca  Icon User is offline

  • code.rascal
  • member icon

Reputation: 44
  • View blog
  • Posts: 3,822
  • Joined: 11-August 05

Re: how to play mp3 files

Posted 24 July 2008 - 01:29 AM

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1