Audio files

playing mp3 and wma files

  • (2 Pages)
  • +
  • 1
  • 2

19 Replies - 5100 Views - Last Post: 01 June 2008 - 01:38 AM Rate Topic: -----

#1 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Audio files

Post icon  Posted 29 May 2008 - 09:09 AM

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

Is This A Good Question/Topic? 0
  • +

Replies To: Audio files

#2 1lacca  Icon User is offline

  • code.rascal
  • member icon

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

Re: Audio files

Posted 29 May 2008 - 09:22 AM

Try here
Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

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

#4 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

Re: Audio files

Posted 29 May 2008 - 10:20 AM

View Post1lacca, on 29 May, 2008 - 09:22 AM, said:



I tried, but it gives me this error:
"java.lang.ArrayIndexOutOfBoundsException: 0
at MP3.main(MP3.java:86)"

i'm probably doing something wrong, as I said I'm still an amature, but I have no idee?
Was This Post Helpful? 0
  • +
  • -

#5 1lacca  Icon User is offline

  • code.rascal
  • member icon

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

Re: Audio files

Posted 29 May 2008 - 10:21 AM

Could you post your code?
Was This Post Helpful? 0
  • +
  • -

#6 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

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

}
}
Was This Post Helpful? 0
  • +
  • -

#7 1lacca  Icon User is offline

  • code.rascal
  • member icon

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

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

#8 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

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

#9 1lacca  Icon User is offline

  • code.rascal
  • member icon

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

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

#10 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

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

#11 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

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

#12 herefishyfishy  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 60
  • Joined: 01-May 08

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

#13 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

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

#14 1lacca  Icon User is offline

  • code.rascal
  • member icon

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

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

#15 Servant  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 29-May 08

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

  • (2 Pages)
  • +
  • 1
  • 2