Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Audio Not Playing  (Read 770 times)
0 Members and 1 Guest are viewing this topic.
Offline tyeeeee1
« Posted 2013-04-03 20:37:13 »

After about an hour of googling and trying a ton of different code samples I finally managed to get to the below code which has no errors but it also doesn't play the audio file; if anyone can see what I've done wrong that'll help.

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  
package Core;

import java.io.InputStream;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;


public class AudioHandler
{
   public AudioHandler()
   {
        InputStream in;
        AudioStream as;
        try
        {
            in = this.getClass().getClassLoader().getResourceAsStream("Resources/Sound/grenade.wav");
            as = new AudioStream(in);
            AudioPlayer.player.start(as);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
   }
}


Most of the other code samples either wouldn't work with a URL path or they had this weird error "javax.sound.sampled.LineUnavailableException". So I ended up smashing a bunch of things together to make the above; it doesn't show any errors at all when compiling/running so I have no idea what could be wrong.
Offline Cero
« Reply #1 - Posted 2013-04-03 21:34:40 »

Never use JavaAudio.  Not ever.
Try easyogg / jogg / jorbis

Offline davedes
« Reply #2 - Posted 2013-04-03 21:35:08 »

You shouldn't use the "sun.*" packages as they are intended for internal use only and subject to change.

JavaSound is all around a hellish experience. If you want audio, you could use TinySound:
https://github.com/finnkuusisto/TinySound

Paul's 3D Sound System is another option. Generally speaking, anything based on OpenAL will be better and more reliable than JavaSound.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline tyeeeee1
« Reply #3 - Posted 2013-04-03 21:59:10 »

I just figured out how to use TinySound and after a little bit of messing around with the example code I got the sound to play. The sound did play properly but only if the code was like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public AudioHandler()
   {
        //initialize TinySound
     TinySound.init();
      //load a sound and music
     //note: you can also load with Files, URLs and InputStreams
               URL url = this.getClass().getClassLoader().getResource("Resources/Sound/grenade.wav");
      Sound grenade = TinySound.loadSound(url);
                grenade.play();
   }


It would not play if I had 'TinySound.shutdown();' at the end. Is there some way to delay the execution of the code until the sound has finished playing so that TinySound isn't shut down until the sound has finished playing?

TinySound should replace the garbage that Java currently comes with.
Offline davedes
« Reply #4 - Posted 2013-04-03 22:27:09 »

Generally your game loops should be written like this:

1  
2  
3  
4  
5  
6  
7  
8  
9  
public void loop() {
    .. initialize resources ..    

    while ( running ) {
        .. update and render game ..
    }

    .. destroy resources ..
}


So you would initialize TinySound before your loop and load/play any sounds. If, say, a user clicks a button while the game is running, you may choose to play the sounds again. Then, at the end of the game, when the user is quitting and the game loop is finished running, you would destroy TinySound and let it consume any resources.

Offline Jimmt
« League of Dukes »

JGO Kernel


Medals: 133
Projects: 4
Exp: 3 years



« Reply #5 - Posted 2013-04-03 22:28:45 »

It would not play if I had 'TinySound.shutdown();' at the end. Is there some way to delay the execution of the code until the sound has finished playing so that TinySound isn't shut down until the sound has finished playing?
Why would your thought process be like this, shutting down what you seek to create in the end of the constructor?
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.

Pippogeek (38 views)
2014-09-24 16:13:29

Pippogeek (29 views)
2014-09-24 16:12:22

Pippogeek (18 views)
2014-09-24 16:12:06

Grunnt (42 views)
2014-09-23 14:38:19

radar3301 (24 views)
2014-09-21 23:33:17

BurntPizza (61 views)
2014-09-21 02:42:18

BurntPizza (31 views)
2014-09-21 01:30:30

moogie (36 views)
2014-09-21 00:26:15

UprightPath (49 views)
2014-09-20 20:14:06

BurntPizza (53 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!