Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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  
  background music  (Read 4930 times)
0 Members and 1 Guest are viewing this topic.
Offline Cero
« Posted 2007-08-20 21:16:12 »

Currently I'm using EasyOgg http://www.cokeandcode.com/taxonomy/term/6?from=50, for sounds aswell as music and ambient.
But for long things like music it's not the best solution I think.
How do you do it ? =)

Offline broumbroum

Junior Devvie





« Reply #1 - Posted 2007-08-20 22:09:02 »

I've got implemented JLayer 1.0 to play mp3 background-sound files.

::::... :..... :::::: ;;;:::™ 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 Cero
« Reply #2 - Posted 2007-08-20 22:34:40 »

hm I looked at the documentation (API)
how do you simply load and loop backgroundmusic and stuff like that ? how does it work in Jlayer ?

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline fletchergames

Senior Devvie





« Reply #3 - Posted 2007-08-21 16:52:25 »

I'd recommend buying a book about Java game programming from Amazon.com.  I think "Developing Games in Java" contains an especially clear description, but there are other options available.

You can probably get the book cheap from now because it's several years old.
Offline Cero
« Reply #4 - Posted 2007-08-21 22:09:07 »

Well but if anyone could post these few lines of code to load and play through JLayer, or explain, that would be awesome.

like in OggClip: you can load using the constructor , putting in a string... like this : OggClip sound = new OggClip("soundfile.ogg");
and play simply by using .play()...

What in JLayer is the storage, the buffer, how to load and how to play (loop)...

Offline broumbroum

Junior Devvie





« Reply #5 - Posted 2007-08-21 22:36:55 »

So here's the sound interface I build for my background FXs :
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  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
/** instances a Sound interface
     * @return one sound interfaced with
     * @see Sound*/

    static Sound initSound() {
        return new Sound() {
            AudioClip bgSfx = null;
            Player player = null;
            boolean bgSfx_playing = false;
            boolean mp3Enabled = false;
            String fileRsrc = null;
            public boolean load(String filename) {
                if(filename == null || filename == "")
                    return false;
                URL rsrc = null;
                try {
                    rsrc = (sfxIsRsrc) ? getClass().getResource(filename) : new File(filename).toURL();
                    bgSfx = Applet.newAudioClip(rsrc);
                    player = new Player((sfxIsRsrc)?getClass().getResourceAsStream(filename):new BufferedInputStream(new FileInputStream(filename)));
                } catch (JavaLayerException ex) {
                    ex.printStackTrace();
                } catch (MalformedURLException ex) {
                    ex.printStackTrace();
                } finally {
                    if(rsrc == null)
                        return false;
                    fileRsrc = filename;
                    return true;
                }
            }
            public void loop() {
                if(mp3Enabled) {
                    bgSfx_playing = true;
                    Thread t_mp3 = new Thread(new Runnable() { public void run() {
                        while(bgSfx_playing) {
                            try {
                                if(!load(fileRsrc)) {
                                    stop();
                                    continue;
                                }
                                if(player instanceof Player)
                                    player.play();
                            } catch (JavaLayerException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }},"T-loop-mp3");
                    t_mp3.start();
                } else {
                    if(bgSfx instanceof AudioClip) {
                        bgSfx_playing = true;
                        bgSfx.loop();
                    }
                }
            }
            public void play() {
                assert fileRsrc != null : "Sound : you must call load(String) once before to play the Sound";
                if(mp3Enabled) {
                    if(!load(fileRsrc))
                        return;
                    Thread t_mp3 = new Thread(new Runnable() { public void run() {
                        try {
                            bgSfx_playing = true;
                            if(player instanceof Player)
                                player.play();
                            bgSfx_playing = false;
                        } catch (JavaLayerException ex) {
                            ex.printStackTrace();
                        }
                    }},"T-play-mp3");
                    t_mp3.start();
                } else {
                    if(bgSfx instanceof AudioClip) {
                        bgSfx.play();
                        bgSfx_playing = true;
                    }
                }
            }
            public void stop() {
                if(mp3Enabled) {
                    if(player instanceof Player)
                        player.close();
                    bgSfx_playing = false;
                } else {
                    if(bgSfx instanceof AudioClip)
                        bgSfx.stop();
                    bgSfx_playing = false;
                }
            }
        };
    }
there's the sfxIsRsrc variable to set statically somewhere around and that be the point.  Grin it can play either with the std AudioClip interface or with the JLayer API. As you can see, AudioClip provides loop() as a default method but JLayer doesn't. Hence I make a new Thread that looks up to a Boolean as each (Player).play()-call finishes.  all of the methods can be called at any time asynchronously.
CU! Cool

::::... :..... :::::: ;;;:::™ 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 Cero
« Reply #6 - Posted 2007-08-21 23:04:32 »

ya I just figured out a bit how it works:

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  
import javazoom.jl.player.Player;
import java.io.*;

public class jo
{
   
   public static void main(String blah[])
   {
      InputStream s1 = null;
      Player myplayer = null;
      boolean succ = false;
     
      try
      {
         s1 = new FileInputStream( "fight.mp3" );
         myplayer = new Player(s1);
      }
      catch(Exception l){System.out.println("f**kError : "+l.getMessage());}
     
      if (myplayer != null && s1 != null)
      {
         System.out.println("Playing fight.mp3 \nCrtl + C to abort");
         try{
            myplayer.play();
            succ = true;
            }
         catch(Exception j){System.out.println("Couldn't play because: "+j.getMessage());}
      }
     
      while(succ)
      {  
      }  
   }
}


But whats with :
1  
player = new Player((sfxIsRsrc)?getClass().getResourceAsStream(filename):new BufferedInputStream(new FileInputStream(filename)));

I did it easier, whats the point ? Smiley

And well I tried out AudioClip a while ago, when I started Java... don't like them Cheesy
And I can also write a loop function somehow...

Offline broumbroum

Junior Devvie





« Reply #7 - Posted 2007-08-21 23:38:19 »

I just made the special resource switch to have my code a bit more scalable. It can depends whether I run the app with a full .jar , or with resources nearby the .jar... Then I can change the one rsrc variable on compile-time to quickly adapt. Nothing very important but useful...  Wink

::::... :..... :::::: ;;;:::™ 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 Cero
« Reply #8 - Posted 2007-08-22 01:05:41 »

but something very disturbing is: the JLayer Player stopsverything when it's played, unlike AudioClip or OggClip or even Normal Clips...
so I have to do it in a thread... man I avoided threads wherever I could, but I definitely have to learn and use them properly on this now =/

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.

Mr.CodeIt (8 views)
2014-12-27 04:03:04

TheDudeFromCI (12 views)
2014-12-27 02:14:49

Mr.CodeIt (25 views)
2014-12-23 03:34:11

rwatson462 (56 views)
2014-12-15 09:26:44

Mr.CodeIt (46 views)
2014-12-14 19:50:38

BurntPizza (92 views)
2014-12-09 22:41:13

BurntPizza (113 views)
2014-12-08 04:46:31

JscottyBieshaar (83 views)
2014-12-05 12:39:02

SHC (94 views)
2014-12-03 16:27:13

CopyableCougar4 (102 views)
2014-11-29 21:32:03
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
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!