LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-08-2006, 05:39 PM   #1
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Rep: Reputation: 65
Java and mp3 playback


Hi all!

I'm playing to start a simple mp3 player written in Java, with a very simply GUI (still deciding between Swing or SWT). But anyway, I'm not going very far. My first thought was to use JLayer. The problem is actually to find a simple example of how to play a file. Something "dummy-safe", but nope. I tried to hack a bit myself and I came up with this:

Code:
import java.io.File;
import java.io.IOException;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;

import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.*;

public class MainClass {
	
	private Player player;

	public MainClass() throws UnsupportedAudioFileException, IOException, JavaLayerException {

		/** 44100Hz, 16-bit, stereo, signed, little-endian */
		AudioFormat playbackFormat = new AudioFormat(44100, 16, 2, true, false);

		AudioInputStream source = AudioSystem.getAudioInputStream(new File(
				"test.mp3"));
		source = AudioSystem.getAudioInputStream(playbackFormat, source);

		player = new Player(source);
		System.out.println("playing anything?");
		player.play();
		System.out.println("don't know...");

	}		

	public static void main(String[] args) throws UnsupportedAudioFileException, IOException, JavaLayerException {
		new MainClass();
	}

}
I know the above code is horrible. This is just a simple test to know if I can or not get mp3 to play. Trying to run the above code (and yes, I do have a file called "test.mp3" in a valid place inside my project map), does well... nothing. I've two System.out.println() in the above code. When the application runs, The fist System.out runs and after a few seconds, the second System.out runs. I'm totally missing what is going on here. Should this piece of code be put in a separated thread?

I've two jar files in this project: jl1.0.jar(This is JLayer from www.javazoom.net) and mp3plugin.jar(from Sun's homepage).

Since this was not working, I went ahead and decided to give SDL a shot, but it is not going well. I've worked before with midi playback in the J2SE and I used javaSDL as well for graphics only. Getting sound to work seems harder than graphics so far ^_^;;

So, if anyone has any tips about mp3 playback in Java, please give me some ideas. Please, don't post links such as jlGUI or JavaTunes because those are far too advanced mp3 players and will do little to help me. I simply need some simple examples of how to use JLayer (or any other simple to use API).

Thanks in advance for any help!

P.S: I won't put any SDL code here because I would ratter prefer to use JLayer in this case. SDL only if I can't fix this

Last edited by Mega Man X; 09-08-2006 at 05:45 PM.
 
Old 09-09-2006, 04:58 AM   #2
robbbert
Member
 
Registered: Oct 2005
Location: Hannover, Germany
Distribution: Let there be Ubuntu... :o)
Posts: 573

Rep: Reputation: 32
Possibly, your main method throws an exception, and, depending on how you call it, it gets swallowed. Try to use a try...catch block, and to write out a possible exception explicitely.
 
Old 09-09-2006, 10:50 AM   #3
Mega Man X
LQ Guru
 
Registered: Apr 2003
Location: ~
Distribution: Ubuntu, FreeBSD, Solaris, DSL
Posts: 5,339

Original Poster
Rep: Reputation: 65
Thank you robbbert for the reply. I did what you told me to, but it would not play anything out the speakers, nor throw any exceptions.

I've been working with this a lot today and found the problem: It's necessary to create a device

"FactoryRegistry.systemRegistry().createAudioDevice()"

and then use that device on the "Player" constructor as argument. Everything is working great now. I'm going with SWT instead of Swing as well... just for fun, since I never worked with SWT before ^_^. Now I am going to add ogg support to it .

I think I might use SDL to create some nice visualizations for the player in the future. I think I can come up with something useful... at last ^_^;;
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
mp3 playback problem feetyouwell Linux - Software 2 09-19-2005 11:07 AM
zero mp3 playback on 9.3 pro stealthgate SUSE / openSUSE 4 08-31-2005 11:09 PM
Problem with MP3 Playback DiscoBiscuit Linux - Newbie 3 05-31-2004 01:30 PM
mp3 playback andyman06 Linux - Software 4 03-05-2004 11:06 PM
MP3 Playback with Noatun? JapanFred Fedora 0 02-14-2004 11:39 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:04 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration