Slick Forums

Discuss the Slick 2D Library
It is currently Fri May 24, 2013 7:33 am

All times are UTC




Post new topic Reply to topic  [ 10 posts ] 
Author Message
 Post subject: Cannot get music to play
PostPosted: Sun May 27, 2012 2:41 am 
Offline

Joined: Sun May 27, 2012 2:33 am
Posts: 4
I've been researching for over two days now, I'm new to programming and slick 2d. I really need some help.

I am trying to get music to play and loop when the menu screen starts, everything I try though does not work.

Code:
package theGame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;

public class Menu extends BasicGameState{//BasicGameState creates Screen
   
   public Menu(int state){
      
   }
   
   public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
      
      Music BGM = new Music("res/weekends.ogg");
      BGM.play();
      
   }
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{//drawing graphics
      
      Image townBack = new Image("res/mapMGSky.png");
      Image townFront = new Image("res/mapBG.png");
      Image heroHome = new Image("res/heroHomeIcon.png");
      Image nillaStore = new Image("res/VanillaStoreIcon.png");
      Image statue = new Image("res/statueIcon.png");
      Image board = new Image("res/boardIcon.png");
      
      //Music music = new Music("res/weekends.ogg");
      //music.loop();
      
      g.drawImage(townBack, 0 , 0);
      g.drawImage(townFront, 0 , 0);
      g.drawImage(heroHome, 447 , 35);
      g.drawImage(nillaStore, -45 , 177);
      g.drawImage(statue, 312 , 120);
      g.drawImage(board, 545 , 259);
      
      g.drawString("Hey, Hero!", 50, 50);//String, x, y
   }
   
   //Music music = new Music("res/weekends.ogg");
   //music.play();
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{//Game AI, User Input, Game Logic
      
   }
   
   public int getID(){//returns State ID
      return 0;
   }

}


I apologize for pasting the whole source I'm just not sure where it goes, according to other tutorials I'm in the correct place, and everything is correct as far as I know from documentation.


Top
 Profile  
 
PostPosted: Sun May 27, 2012 10:14 am 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
You need to implement MusicListener

Code:
public class Menu extends BasicGameState implements MusicListener{//BasicGameState creates Screen
   
   public Menu(int state){
     
   }


and then, in the init method, you need to add the listener to it

Code:
public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
     
      Music BGM = new Music("res/weekends.ogg");
      BGM.addListener(this);
      BGM.play();
     
   }



That SHOULD work... if it doesn't, then post back here and me or someone else will help you further :)


Top
 Profile  
 
PostPosted: Sun May 27, 2012 3:10 pm 
Offline

Joined: Sun May 27, 2012 2:33 am
Posts: 4
I added the changes you mentioned but I the game is crashing now:

Code:
public class Menu extends BasicGameState implements MusicListener{//BasicGameState creates Screen


My IDE is saying:
Quote:
"The type Menu must implement the inherited abstract method MusicListener.musicEnded(Music)"


When I choose to fix either issue the game still crashes

Here the code:

Code:
public class Menu extends BasicGameState implements MusicListener{//BasicGameState creates Screen
   
   public Menu(int state){
      
   }
   
   public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
      
      Music BGM = new Music("res/weekends.ogg");
      BGM.addListener(this);
      BGM.loop();
      
   }


I appreciate all the help I really do, I'm on break from school, just started going to school for computer Science and this is the project I gave myself to get better.


Top
 Profile  
 
PostPosted: Sun May 27, 2012 3:17 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Perhaps the BGM.loop(); needs to go in the update method. I'm not entirely sure. I've never personally tried using music. It seems it wants a listener for ending the music too..

Someone more versed in slick's musiclistener care to help out? :)


Top
 Profile  
 
PostPosted: Sun May 27, 2012 3:59 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
You don't need a music listener. The following should work:
Code:
package slicktests;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Music;
import org.newdawn.slick.SlickException;

public class MusicTest extends BasicGame {

   Music music;
   
   public MusicTest() {
      super("music");
   }

   public void init(GameContainer container) throws SlickException {
      music = new Music("res/speech.ogg");
      music.play();
   }


   public void render(GameContainer container, Graphics g) throws SlickException {
     
   }

   public void update(GameContainer container, int delta) throws SlickException {

   }
   
   public static void main(String[] argv) throws SlickException {
      new AppGameContainer(new MusicTest(), 800, 600, false).start();
   }

}


If it doesn't work, it might be because of a corrupt file. You can use Audacity to save Slick-compatible OGGs. Here is an OGG file that should work:
http://www.mediafire.com/?4b4ml2mqa25tlvz


Top
 Profile  
 
PostPosted: Sun May 27, 2012 4:10 pm 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
Oh, apologies. I was using the slick test's "SoundTest" example ^_^;


Top
 Profile  
 
PostPosted: Sun May 27, 2012 4:59 pm 
Offline

Joined: Sun May 27, 2012 2:33 am
Posts: 4
I dont know if I'm doing this right or if I am following you incorrectly

Here is my edited code with what you posted:
Code:
package theGame;

import org.newdawn.slick.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.Music;

public class Menu extends BasicGameState{//BasicGameState creates Screen
   
   Music BGM;
   
   public Menu(int state){
      
      super("BGM");
      
   }
   
   public void init(GameContainer gc, StateBasedGame sbg)throws SlickException{
      
      BGM = new Music("res/weekends.ogg");
      BGM.play();
      
   }
   
   public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException{//drawing graphics
      
      Image townBack = new Image("res/mapMGSky.png");
      Image townFront = new Image("res/mapBG.png");
      Image heroHome = new Image("res/heroHomeIcon.png");
      Image nillaStore = new Image("res/VanillaStoreIcon.png");
      Image statue = new Image("res/statueIcon.png");
      Image board = new Image("res/boardIcon.png");
      
      //Music music = new Music("res/weekends.ogg");
      //music.loop();
      
      g.drawImage(townBack, 0 , 0);
      g.drawImage(townFront, 0 , 0);
      g.drawImage(heroHome, 447 , 35);
      g.drawImage(nillaStore, -45 , 177);
      g.drawImage(statue, 312 , 95);
      g.drawImage(board, 545 , 259);
      
      g.drawString("Hey, Hero!", 50, 50);//String, x, y
   }
   
   //Music music = new Music("res/weekends.ogg");
   //music.play();
   
   public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException{//Game AI, User Input, Game Logic
      
   }
   
   public int getID(){//returns State ID
      return 0;
   }

}


Here is what I am getting when the game crashes:
Quote:
Mon May 28 01:54:46 JST 2012 INFO:Slick Build #274
Mon May 28 01:54:46 JST 2012 INFO:LWJGL Version: 2.8.3
Mon May 28 01:54:46 JST 2012 INFO:OriginalDisplayMode: 1920 x 1080 x 32 @60Hz
Mon May 28 01:54:46 JST 2012 INFO:TargetDisplayMode: 760 x 480 x 0 @0Hz
Mon May 28 01:54:46 JST 2012 INFO:Starting display 760x480
Mon May 28 01:54:46 JST 2012 INFO:Use Java PNG Loader = true
Mon May 28 01:54:46 JST 2012 INFO:Controllers not available
Mon May 28 01:54:46 JST 2012 INFO:Initialising sounds..
Mon May 28 01:54:46 JST 2012 INFO:- Sound works
Mon May 28 01:54:46 JST 2012 INFO:- 64 OpenAL source available
Mon May 28 01:54:46 JST 2012 INFO:- Sounds source generated
AL lib: FreeContext: (000000000BAC72F0) Deleting 64 Source(s)
Exception in thread "main" java.lang.NoClassDefFoundError: com/jcraft/jorbis/Info
at org.newdawn.slick.openal.OggInputStream.<init>(OggInputStream.java:35)
at org.newdawn.slick.openal.OggDecoder.getData(OggDecoder.java:311)
at org.newdawn.slick.openal.SoundStore.getOgg(SoundStore.java:835)
at org.newdawn.slick.openal.SoundStore.getOgg(SoundStore.java:793)
at org.newdawn.slick.Music.<init>(Music.java:135)
at org.newdawn.slick.Music.<init>(Music.java:74)
at theGame.Menu.init(Menu.java:19)
at theGame.game.initStatesList(game.java:21)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:164)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at theGame.game.main(game.java:32)
Caused by: java.lang.ClassNotFoundException: com.jcraft.jorbis.Info
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 12 more


I am sure my .ogg file is fine, I've tried it out with 3 others still not working. I even commented out the super to see if that could fix it.

P.S. I live in Okinawa Japan so its 2 am, need to sleep for 8 hours then continue. Thanks to everybody who is helping me out.


Top
 Profile  
 
PostPosted: Sun May 27, 2012 5:31 pm 
Offline
Slick Zombie

Joined: Sat Jan 27, 2007 7:10 pm
Posts: 1469
You should load all resources including Images in init().

The error seems to be because jorbis-0.0.15.jar or jogg-0.0.7.jar are corrupt, or not included in your class path.


Top
 Profile  
 
PostPosted: Mon May 28, 2012 1:00 am 
Offline

Joined: Sun May 27, 2012 2:33 am
Posts: 4
Thank you so much!!!! problem resolved! I jumped out of bed to work on this, thank you all for the help!


Top
 Profile  
 
PostPosted: Mon May 28, 2012 11:33 am 
Offline

Joined: Mon May 07, 2012 11:36 pm
Posts: 93
What did you do to solve it? , because i've just discovered i'm getting the same error.. thanks in advance :)


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 10 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group