Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (542)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (604)
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  
  Java playing audiofiles  (Read 1354 times)
0 Members and 1 Guest are viewing this topic.
Offline Pyrodragoon

Junior Devvie




Art calculated with java...


« Posted 2008-03-18 10:15:26 »

Hi. I got an audiorelated problem in Java:
I know how to play an audiofile with AudioClip,
but there is no option to stop the Audioclip while playing and resume it after a few seconds.
I also need to know how much of it is left to play, has been played etc.
Is there a java-class that offers these methods and not only a play() and stop() ?
Offline DzzD
« Reply #1 - Posted 2008-03-18 12:02:59 »

There is more advanced sound class to produce sound in java, google for it

about AudioClip:
you can play/stop it that's all (http://java.sun.com/j2se/1.4.2/docs/api/java/applet/AudioClip.html)
to know how much has been played you can do something like that
start=System.currentTimeMillis();
a.play()
//than later use playingtime=System.ucrrentTimeMillis()-start;

you can mix multiple audio file by doing:
a.play()
a2.play


you can simulate volume using a little trick:
create 5 AudioClip using same source file
than to play the sound with volume=3 do
a1.paly()
a2.play()
a3.play()

and so .. for volume =1
a1.play()


finally if you like challenge you can try to produce/get a better control using sinewave as source file for multiple AudioClip and try to produce the sound using FFT Smiley but finally you better use something like that http://java.sun.com/j2se/1.4.2/docs/api/javax/sound/sampled/AudioSystem.html


Offline DzzD
« Reply #2 - Posted 2008-03-18 13:06:42 »

below a sample source code showing how to control volume with AudioClip

it use 5 different sourcefiles
SOUND0.WAV
SOUND1.WAV
SOUND2.WAV
SOUND3.WAV
SOUND4.WAV

to be compatible with JVM 1.1+ but using newAudioClip() you can use only one source file
SOUND.WAV

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  
91  
92  
93  
94  
/**
 *
 * Adding Volume on Applet AudioClip
 *
 * @author Bruno Augi
 * @version 1.00 08/03/18
 */

 
import java.awt.*;
import java.applet.*;
import java.net.*;


public class Test extends Applet implements Runnable
{
   AudioClip a[]=new AudioClip[5];

   public void init()
   {
      try
      {
         System.out.println("Loading sound " +getCodeBase()+"/SOUND.WAV");
         for(int x=0;x<a.length;x++)
            a[x]=this.getAudioClip(new URL(getCodeBase()+"/SOUND"+x+".WAV"));
      }
      catch(MalformedURLException mue)
      {
         mue.printStackTrace(System.out);
      }
     
      //Prepare sounds to be played (avoid unsynchronisation of AudioClip)
      try
      {
         for(int x=0;x<a.length;x++)
            if(this.a[x]!=null)
            {
               a[x].play();
               a[x].stop();
            }
         Thread.sleep(100);
      }
      catch(InterruptedException ie)
      {
      }
     
     
   }
   
   
   public void start()
   {
         
      Thread t=new Thread(this);
      t.start();
     
   }
   
   public void run()
   {
      Graphics g=this.getGraphics();
     
      for(int x=0;x<this.a.length;x++)
      {
     
         g.setColor(Color.white);
         g.fillRect(0,0,this.getSize().width,this.getSize().height);
         g.setColor(Color.black);
         g.drawString("Playing sound volume = " + x,30,30);
         this.playVolume(x);
           
         try
         {
            Thread.sleep(2000);
         }
         catch(InterruptedException ie)
         {
         }
      }
   }
   
   public void playVolume(int volume)
   {
      for(int x=0;x<volume;x++)
      {
         if(x<this.a.length && this.a[x]!=null)
            a[x].play();
      }
   }

   public void paint(Graphics g)
   {
     
   }
}


EDIT:
using 4 differents sources files with differents volume could enable you to change sound volume from 0 to 15

your sound just have to match the below rule:
sound0.wav (volume==1)
sound1.wav (volume==2)
sound2.wav (volume==4)
sound4.wav (volume==8)

then using binary representation of your volume you can play the sound at any volume between 0 and 16 :
sound0 and sound4 for volume=9
sound0 and sound1 and sound2 a for volume=7
etc....

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.

Elsealabs (14 views)
2014-12-28 10:39:27

CopyableCougar4 (18 views)
2014-12-28 02:10:29

BurntPizza (21 views)
2014-12-27 22:38:51

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

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

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

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

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

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

BurntPizza (115 views)
2014-12-08 04:46:31
How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

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
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!