Im an amature java programmer and I realy need help. i need to play mp3 and wma files. I was just wondering if anyone could help me. I've been searching the internet and java help files and only found this:
import java.awt.*;
import java.applet.*;
public class Sound extends Applet {
AudioClip soundFile;
public void init() {
soundFile = getAudioClip(getDocumentBase(),"Reflectia.mp3");
soundFile.play();
}
}
but this only gives an aplet that dosn't make a sound
Audio filesplaying mp3 and wma files
19 Replies - 4237 Views - Last Post: 01 June 2008 - 01:38 AM
Replies To: Audio files
#3
Re: Audio files
Posted 29 May 2008 - 09:27 AM
Applet do not support .MP3 you will have to use a .AU file
You can download from the net a free .mp3 to Wave converter
http://www.dreaminco...wtopic50873.htm
You can download from the net a free .mp3 to Wave converter
http://www.dreaminco...wtopic50873.htm
#4
#6
Re: Audio files
Posted 29 May 2008 - 10:31 AM
Shure, but I actually didnt change any thing. I couldnt even get the first code to work:
/*************************************************************************
* 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....er/sources.html
*
*
* To execute, get the file jl1.0.jar from the website above or from
*
* http://www.cs.prince...inout/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 MP3
{
private String filename = "20 In Die Hemel Is Die Heer.mp3";
private Player player;
// constructor that takes the name of an MP3 file
public MP3 (String filename)
{
this.filename = filename;
}
public void close ()
{
if (player != null)
player.close ();
}
// play the MP3 file to the sound card
public void play ()
{
try
{
FileInputStream fis = new FileInputStream (filename);
BufferedInputStream bis = new BufferedInputStream (fis);
player = new Player (bis);
}
catch (Exception e)
{
System.out.println ("Problem playing file " + filename);
System.out.println (e);
}
// run in new thread to play in background
new Thread ()
{
public void run ()
{
try
{
player.play ();
}
catch (Exception e)
{
System.out.println (e);
}
}
}
.start ();
}
// test client
public static void main (String[] args)
{
String filename = args [0]; /////////////////this is where he threw the error
MP3 mp3 = new MP3 (filename);
mp3.play ();
// 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 ();
}
}
/*************************************************************************
* 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....er/sources.html
*
*
* To execute, get the file jl1.0.jar from the website above or from
*
* http://www.cs.prince...inout/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 MP3
{
private String filename = "20 In Die Hemel Is Die Heer.mp3";
private Player player;
// constructor that takes the name of an MP3 file
public MP3 (String filename)
{
this.filename = filename;
}
public void close ()
{
if (player != null)
player.close ();
}
// play the MP3 file to the sound card
public void play ()
{
try
{
FileInputStream fis = new FileInputStream (filename);
BufferedInputStream bis = new BufferedInputStream (fis);
player = new Player (bis);
}
catch (Exception e)
{
System.out.println ("Problem playing file " + filename);
System.out.println (e);
}
// run in new thread to play in background
new Thread ()
{
public void run ()
{
try
{
player.play ();
}
catch (Exception e)
{
System.out.println (e);
}
}
}
.start ();
}
// test client
public static void main (String[] args)
{
String filename = args [0]; /////////////////this is where he threw the error
MP3 mp3 = new MP3 (filename);
mp3.play ();
// 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 ();
}
}
#7
Re: Audio files
Posted 29 May 2008 - 10:46 AM
you have to specify the name of the mp3 when you run your program, so run like this:
java MP3 whatever.mp3
(in an IDE you can usually specify command line parameters to your application as well)
java MP3 whatever.mp3
(in an IDE you can usually specify command line parameters to your application as well)
#8
Re: Audio files
Posted 29 May 2008 - 10:54 AM
Im truly sory, but my knowlidge of java is extremely limited.
im runing this program from "ready to program" and dont know anything about what u said. can u maybe simplify??
im runing this program from "ready to program" and dont know anything about what u said. can u maybe simplify??
#9
Re: Audio files
Posted 29 May 2008 - 11:08 AM
I don't know what is "ready to program", but I would like to suggest you these getting started tutorials from Sun, they'll show you how to compile and run a Java program (and what is needed to do so), and at the end my last post will make sense, hopefully 
If you have any questions, feel free to ask!
If you have any questions, feel free to ask!
#10
Re: Audio files
Posted 29 May 2008 - 11:18 AM
Almost forgot to thank u. il be shure to ask u if i get stuck. thanks again.
#11
Re: Audio files
Posted 29 May 2008 - 01:28 PM
It works perfectly. Thanks so much, ive been trying to make it work for a week now. u dont happen to have a code like that for wma? thanks again
#12
Re: Audio files
Posted 29 May 2008 - 04:24 PM
Unless I am mistaken, I think someone posted a tutorial on playing sounds. You cannot play mp3s, anyway, so you need to convert them to .ogg s or something...
#13
Re: Audio files
Posted 29 May 2008 - 10:57 PM
no, i need code wich enables u to play wma. 1lacca showed me a great link wich enables playing of mp3's, but not wma. Anybody got anything, because ive been searching the web but this is the first helpfull site i got.
#14
Re: Audio files
Posted 30 May 2008 - 02:27 AM
WMA is a container format, it can hold audio compressed with different codecs. Unfortunately I know no easy way to play it from Java, so you are probably better off converting your wma files to mp3 and playing those. You can find a lot of converters with google.
#15
Re: Audio files
Posted 30 May 2008 - 05:54 AM
thanks again. you dont know how much u guys helped me.
if I struggle with something again here is the first place I'm going to.
(oh yea, sory about all my bad english, its my second language)
thanks again
if I struggle with something again here is the first place I'm going to.
(oh yea, sory about all my bad english, its my second language)
thanks again
|
|

New Topic/Question
Reply




MultiQuote





|