Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Playing mp3  (Read 5497 times)
0 Members and 1 Guest are viewing this topic.
Offline alexxz4

Junior Member


Projects: 1



« Posted 2007-12-05 21:13:09 »

Im trying to play an mp3 but it throws UnsupportedAudioFileException
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
import javax.sound.sampled.*;
import java.io.*;
public class mp3play{
   public static void main(String args[]) throws IOException{
      File file = new File("clip.mp3");
      AudioFileFormat baseFileFormat = null;
      AudioFormat baseFormat = null;
      try{
         baseFileFormat = AudioSystem.getAudioFileFormat(file);                         //     <----------     here
     }   catch(UnsupportedAudioFileException ex){
         System.out.println("Audio File not Supported");  
      }
      baseFormat = baseFileFormat.getFormat();
      AudioFileFormat.Type type = baseFileFormat.getType();
      float frequency = baseFormat.getSampleRate();
      AudioInputStream in=null;
      try{
         in= AudioSystem.getAudioInputStream(file);
      }   catch(UnsupportedAudioFileException ex){}
      AudioInputStream din = null;
      baseFormat = in.getFormat();
      AudioFormat decodedFormat =
       new AudioFormat(AudioFormat.Encoding.PCM_SIGNED,
                    baseFormat.getSampleRate(),
                    16,
                    baseFormat.getChannels(),
                    baseFormat.getChannels() * 2,
                    baseFormat.getSampleRate(),
                    false);
      din = AudioSystem.getAudioInputStream(decodedFormat, in);
      try{
         rawplay(decodedFormat, din);
      }   catch(LineUnavailableException ex){}
      in.close();
   }  
   private static void rawplay(AudioFormat targetFormat,AudioInputStream din)
         throws IOException, LineUnavailableException{
       byte[] data = new byte[4096];
        SourceDataLine line = getLine(targetFormat);
        if (line != null){
          line.start();
          int nBytesRead = 0, nBytesWritten = 0;
          while (nBytesRead != -1){
              nBytesRead = din.read(data, 0, data.length);
              if (nBytesRead != -1)
               nBytesWritten = line.write(data, 0, nBytesRead);
          }
          line.drain();
          line.stop();
          line.close();
          din.close();
        }
   }
   private static SourceDataLine getLine(AudioFormat audioFormat) throws LineUnavailableException{
        SourceDataLine res = null;
        DataLine.Info info =
          new DataLine.Info(SourceDataLine.class, audioFormat);
        res = (SourceDataLine) AudioSystem.getLine(info);
        res.open(audioFormat);
        return res;
   }
}  


Here is the source and the clip:    http://www.mediafire.com/?ecvm4ldfmz9

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #1 - Posted 2007-12-05 21:47:32 »

JavaSound doesn't support mp3.
I suggest to use .ogg instead, using the jorbis library: http://www.jcraft.com/jorbis/index.html

Offline alexxz4

Junior Member


Projects: 1



« Reply #2 - Posted 2007-12-05 22:00:28 »

weird cause i got that code from the internet and the file was an mp3.
Quote
I suggest to use .ogg instead, using the jorbis library
Ohh... those JAR-library-things... they never work for me...

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline broumbroum

Junior Member





« Reply #3 - Posted 2007-12-05 23:44:24 »

JLayer is the lib for MPEG-Layer 3 yet  Smiley

::::... :..... :::::: ;;;:::™ b23:production 2006 GNU/GPL @ http://b23prodtm.webhop.info
on sf.net: /projects/sf3jswing
Java (1.6u10 plz) Web Start pool
dev' VODcast[/ur
Offline erikd

JGO Ninja


Medals: 16
Projects: 4
Exp: 14 years


Maximumisness


« Reply #4 - Posted 2007-12-06 11:21:27 »

those JAR-library-things... they never work for me...

jorbis is widely used, I still suggest to give it a try before dismissing it.

Offline alexxz4

Junior Member


Projects: 1



« Reply #5 - Posted 2007-12-06 14:32:26 »

I spent like 2 hrs on trying to understand the JOrbisPlayer.java and still dont know how i can implement all that stuff in my game so that it can play .ogg files.
Does anyone have an example of a game that uses it?

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline dishmoth
« Reply #6 - Posted 2007-12-11 11:44:53 »

A tutorial on playing OGG files in Java: http://www.cokeandcode.com/node/325

Offline kevglass

JGO Kernel


Medals: 121
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #7 - Posted 2007-12-11 12:18:21 »

If you're using Java Sound you might be more interested in EasyOgg.

Kev

Offline appel

JGO Wizard


Medals: 50
Projects: 4


I always win!


« Reply #8 - Posted 2007-12-28 03:48:08 »

What MP3 to OGG converter are you guys using?

I've tried several of the freewares I've found using "free mp3 to ogg" google search, but they all basically suck. One converted my mp3 song into some 2x speed audio file, and after I used another converter then Jogg could not read it (not a valid ogg file).

No proper ones out there?

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline woogley
« Reply #9 - Posted 2007-12-28 07:47:40 »

Audacity does this I believe, though MediaMonkey is also good
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline oNyx

JGO Coder


Medals: 1


pixels! :x


« Reply #10 - Posted 2007-12-28 15:56:07 »

>What MP3 to OGG converter are you guys using?

None. Converting from one lossy format to another is futile. Well, of course it can be done, but the artifacts introduced by mp3 compression are harder to compress than the original. So, basically you lose some quality for things you don't actually want.

The proper way to do it is to compress the original (lossless) data again. It's pretty easy with OggDropXPd for example. The Foobar audio player is also an option - you can convert all kinds of source material to Ogg Vorbis via rmb->convert.

Oh and don't use the bitrate control stuff. Use the quality setting instead. The -1 setting works for most game related stuff very well. A 35 second mono BGM loop can get as small as 210kb with q=-1 and it still sounds pretty good.

A big advantage of Ogg Vorbis is the subjective good quality at low bitrates. It only sounds a bit damped whereas mp3 starts to create painful "edges" (eg high hats are pretty annoying with mp3 artifacts ).

弾幕 ☆ @mahonnaiseblog
Offline alexxz4

Junior Member


Projects: 1



« Reply #11 - Posted 2007-12-28 17:00:31 »

Ok, ive given up on playing ogg or mp3. I use midi's now. I have one problem though:
If a midi is playing in an applet in a separate tab and i close that tab, its still playing.  Huh

[ Motherload Unlimited looking for programmers ] [ Solitaire ]
Offline cylab

JGO Ninja


Medals: 38



« Reply #12 - Posted 2007-12-28 17:07:13 »

You have to stop the playing of the midi-file yourself in the stop()-method of your applet. See http://java.sun.com/docs/books/tutorial/deployment/applet/index.html for details on the applet's lifecycle.

Mathias - I Know What [you] Did Last Summer!
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

CogWheelz (15 views)
2014-07-30 21:08:39

Riven (21 views)
2014-07-29 18:09:19

Riven (14 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (32 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (42 views)
2014-07-23 21:16:32

Riven (30 views)
2014-07-23 21:07:15

Riven (31 views)
2014-07-23 20:56:16
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!