School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Welcome to Dream.In.Code
Become an Expert!

Join 340,158 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 4,063 people online right now. Registration is fast and FREE... Join Now!



how to play mp3 files

how to play mp3 files Rate Topic: -----

#1 narmer93  Icon User is offline

  • D.I.C Regular
  • PipPipPip
  • View blog
  • Group: Members
  • Posts: 315
  • Joined: 13-March 08


Dream Kudos: 0

Posted 23 July 2008 - 11:35 AM

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


#2 lordms12  Icon User is offline

  • D.I.C Regular
  • Icon
  • View blog
  • Group: Contributors
  • Posts: 335
  • Joined: 16-February 08


Dream Kudos: 275

Posted 23 July 2008 - 12: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
  • Icon
  • Group: Alumni
  • Posts: 3,822
  • Joined: 11-August 05


Dream Kudos: 0

Posted 24 July 2008 - 12: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
  • +
  • -



Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users



Live Help!

Be Social

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

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month