Jump to content
  • Advertisement
Sign in to follow this  
Robo-Link

[java] Java Sound Help Needed.

This topic is 3777 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, so when I run this code I get an UnsupportedAudioFileException. I have the MP3 plug in installed, and even if I run it with bgm.mid it throws the same thing. Can someone please help me?
import java.io.File;
import java.io.InputStream;
import java.io.FileNotFoundException;
import java.io.*;

import javax.sound.sampled.*;

public class sound
{
	//AudioInputStream as;
//	AudioSystem ay;
	Clip c;
	
	sound()
	{
		try
		{
			
			//System.out.print("\n\n\n\n\nc = " + ay.getAudioInputStream(new File("bgm.mp3") ).toString() + "\n\n\n\n\n");
			
		//	c = new Clip();
			//as = 
			c.open(AudioSystem.getAudioInputStream(new File("bgm.mp3")));
		}
		
		
		
		catch(IOException e)
		{
			System.out.print("\n\n\n\n\nc = " + " IO not"  + "\n\n\n\n\n");
		}
		
		catch(UnsupportedAudioFileException e)
		{
			System.out.print("\n\n\n\n\nc = " + " mp3 not"  + "\n\n\n\n\n");
		}
		
		catch(LineUnavailableException e)
		{
			System.out.print("\n\n\n\n\nc = " + " line not"  + "\n\n\n\n\n");
		}
		
	//	catch(Exception e)
	//	{
		//	System.out.print("\n\n\n\n\nc = " + ay.getAudioInputStream(new File("bgm.mp3") ) + "\n\n\n\n\n");
	//		System.out.print("\n\n\n\n\nc = " + as  + "\n\n\n\n\n");
	//	}
	}
	
	public void play()
	{
		System.out.print("\n\n\n\n\nc = " + c + "\n\n\n\n\n");
		c.start();
	}
}

Share this post


Link to post
Share on other sites
Advertisement
Anyone? I noticed that this subforum is dead, so if I don't get a reply by tomorrow I'll post it in general game programming.

Share this post


Link to post
Share on other sites
This forum is not dead but you're description of the error is very vague, and I do not think that this is the real code, as "sound()" shouldn't work.

First of all, which "plugin" did you install? JMF? Or do you use the "lib" jLayer?
What is the real source code? What is the whole stacktrace?

With that information you are more likely to get help in such a hurry.

Share this post


Link to post
Share on other sites
Wow, really? Okay, first off this is the real code. "sound()" is a constructor for the class, not a method. That's why it doesn't need things infront of it. The error is not vauge. vauge would be "Exception" not "UnsupportedAudioFileException" which is the exact error it throws.

Did you try compileing the code? Try it (I use J creator) and you'll see it is proper. I just need to know why I am getting UnsupportedAudioFileException thrown even when I use audio formats that are supported.

Share this post


Link to post
Share on other sites
The "studio()" constructor, ok, my fault, haven't thought of constructor as I personally set the visibility always.

But you still didn't say which "plugin" you installed? Does it need explicit libs given to the build path? I tried your code, of course, but how can I reproduce if I do not know which "plugin" or "library" you're meaning.

Share this post


Link to post
Share on other sites
Well, I've tried a few a few diffeant ways.

JMF2.1.1e, and mp3spi.

First I tried adding the jars to the /ext/ folder on my jre and jdk, than adding those jars to the jdk profile in j creator. Than I tried setting them as needed libs from the projects menu. But both did the same thing. Also, I think I'm using the java sound API. Anyway, the error happens when I try to play .mid files too, which I know are supported. I have the audio files in the same directery as the class files in my project.

Share this post


Link to post
Share on other sites
Hi,

first of all, you are using Windows, right? Because JMF MP3 support is Win only (if the FAQ is correct).

Second, JMF adds "LIBS" and further functions not "support" to old/other methods like Clip. If I remember correctly you have to create a Player object of JMF. There are several tutorials for that e.g. https://www6.software.ibm.com/developerworks/education/j-jmf/index.html.

Another way would be to use jLayer (http://www.javazoom.net/javalayer/javalayer.html) and this source for example http://www.cs.princeton.edu/introcs/faq/mp3/MP3.java.html.

Both approaches worked for me.

Share this post


Link to post
Share on other sites
Yes, I'm on windows. The latter worked, but I am having trouble getting my music to loop. Do you have any ideas?



/*************************************************************************
* Compilation: javac -classpath .:jl1.0.jar MP3.java (OS X)
* javac -classpath .;jl1.0.jar MP3.java (Windows)
* Execution: java -classpath .:jl1.0.jar MP3 filename.mp3 (OS X / Linux)
* java -classpath .;jl1.0.jar MP3 filename.mp3 (Windows)
*
* Plays an MP3 file using the JLayer MP3 library.
*
* Reference: http://www.javazoom.net/javalayer/sources.html
*
*
* To execute, get the file jl1.0.jar from the website above or from
*
* http://www.cs.princeton.edu/introcs/24inout/jl1.0.jar
*
* and put it in your working directory with this file MP3.java.
*
*************************************************************************/

import java.io.BufferedInputStream;
import java.io.FileInputStream;

import javazoom.jl.player.Player;


public class sound {
private String filename;
private Player player;
Thread bgm;
FileInputStream fis;
BufferedInputStream bis;
// String filename;

// constructor that takes the name of an MP3 file
public sound()
{
this.filename = "bgm.mp3";

try {
fis = new FileInputStream(filename);
bis = new BufferedInputStream(fis);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}

bgm = new Thread()
{
public void run()
{
while(true)
{
try
{
if(player.isComplete())
{
player = new Player(bis);
}

if(player.getPosition() <= 0)
{
player.play();
}
}

catch (Exception e)
{
System.out.println(e);
}
}
}
};
}

public void close() { if (player != null) player.close(); }

// play the MP3 file to the sound card
public void play()
{
// run in new thread to play in background
if(!bgm.isAlive() || player.isComplete())
bgm.start();
}


// test client
/*public static void main(String[] args) {
String filename = args[0];


// do whatever computation you like, while music plays
int N = 4000;
double sum = 0.0;
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
sum += Math.sin(i + j);
}
}
System.out.println(sum);

// when the computation is done, stop playing it
mp3.close();

// play from the beginning
mp3 = new MP3(filename);
mp3.play();

}*/

}



Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!