Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (521)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (589)
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  
  How to have control on audio (wave file ), like pause, resume, forward, rewind  (Read 1835 times)
0 Members and 1 Guest are viewing this topic.
Offline kavetivinesh2k

Junior Newbie





« Posted 2005-08-18 10:19:02 »

Hi all,

I’m developing audio mixer to play audio (wave files) and to have control on it.
I'm able to play and stop the audio with AudioInputStream and SourceDataLine.  But unable to pause and resume the playing/running audio file.  Sad
Can any on help me how to get the control on audio file like pause, resume and forward/rewind (5sec, 10sec,....).  Embarrassed .

I'm attaching the code, which I’m using for my application.

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  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
/*
 * Created on Aug 5, 2005
 */

package Samples.audio;

import java.io.File;
import java.io.IOException;

import java.net.URL;

import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;
import javax.sound.sampled.FloatControl;
/**
 * @author vinesh
 */

public class FoundationAudioStream
   implements   Runnable{
      // better size
      private static final int   EXTERNAL_BUFFER_SIZE = 4000 * 4;

      private Thread            m_thread = null;
      private Object            m_dataSource;
      private AudioInputStream   m_audioInputStream;
      private SourceDataLine      m_sourceDataLine;
      public static long          lastMicrosecondPosition=0;
      public static long          currentMicrosecondPosition=0;
     
      private boolean          pauseFlag=false;
      private int             pauseCount;
      private int             bufferSize;
      private int             audioLength;            // Length of the sound.
      public static   int       audioPosition = 0;      // Current position within the sound
      /**   This variable is used to distinguish stopped state from paused state. */
      private boolean         m_bRunning;

      protected FoundationAudioStream()
      {
         m_dataSource = null;
         m_audioInputStream = null;
         m_sourceDataLine = null;
         m_gainControl = null;
         m_panControl = null;
      }

      protected void setDataSource(File file)
         throws   UnsupportedAudioFileException, LineUnavailableException, IOException
      {
         m_dataSource = file;
         initAudioInputStream();
      }

      private void initAudioInputStream()
         throws   UnsupportedAudioFileException, LineUnavailableException, IOException{
         if (m_dataSource instanceof File)initAudioInputStream((File) m_dataSource);
      }

      private void initAudioInputStream(File file)
         throws   UnsupportedAudioFileException, IOException{
            m_audioInputStream = AudioSystem.getAudioInputStream(file);
            bufferSize = (int)file.length();
      }

      protected void initLine()
         throws   LineUnavailableException{
         if (m_sourceDataLine == null){
            createLine();
            openLine();
         }else{
            AudioFormat   lineAudioFormat = m_sourceDataLine.getFormat();
            AudioFormat   audioInputStreamFormat = m_audioInputStream == null ? null : m_audioInputStream.getFormat();
            if (!lineAudioFormat.equals(audioInputStreamFormat)){
               m_sourceDataLine.close();
               openLine();
            }
         }
      }

      private void createLine()
         throws   LineUnavailableException{
         if (m_sourceDataLine != null)return;
         AudioFormat   audioFormat = m_audioInputStream.getFormat();
         DataLine.Info   info =
            new DataLine.Info(SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED);
         m_sourceDataLine = (SourceDataLine) AudioSystem.getLine(info);
         
         /** reseting the lastMicrosecondPosition for progress bar. */
         lastMicrosecondPosition=0;
      }

      private void openLine()
         throws   LineUnavailableException{
         if (m_sourceDataLine == null)return;
         AudioFormat   audioFormat = m_audioInputStream.getFormat();
         m_sourceDataLine.open(audioFormat, m_sourceDataLine.getBufferSize());
      }

      /** Start the audio */
      public void start()
      {
         if (!(m_thread == null || !m_thread.isAlive()))
               out("WARNING: old thread still running!!");
         m_thread = new Thread(this);
         m_thread.start();
         m_sourceDataLine.start();
      }

      /** Stop the audio. */
      protected void stop()
      {
         if (m_bRunning)
         {
            if (m_sourceDataLine != null)
            {
               lastMicrosecondPosition=m_sourceDataLine.getMicrosecondPosition();
               m_sourceDataLine.stop();
               m_sourceDataLine.flush();
            }
            m_bRunning = false;
            /**   We re-initialize the AudioInputStream. */
            try{
               initAudioInputStream();
            }catch (UnsupportedAudioFileException e){}
             catch (LineUnavailableException e){}
             catch (IOException e){}
         }
      }

      /** Pause the audio */
      public void pause(){
         m_sourceDataLine.stop();}

      /** Continue playing audio after pause */
      public void resume(){
         m_sourceDataLine.start();}

       /** Stop playing the audio and reset the position to 0 on progress bar */
       public void resetAudio( ) {
           stop( );
           audioPosition = 0;
           TestAudioPlayer.songPerMillisecond.setValue(0);
       }
       
      public void run()
      {
         int   nBytesRead = 0;
         m_bRunning = true;
         byte[]   abData = new byte[EXTERNAL_BUFFER_SIZE];
         int currentCount=0;
         while (nBytesRead != -1 && m_bRunning)
         {
            try{
               nBytesRead = m_audioInputStream.read(abData, 0, abData.length);
            }catch (IOException e){
               e.printStackTrace();}
            if (nBytesRead >= 0 )
               int   nBytesWritten = m_sourceDataLine.write(abData, 0, nBytesRead);
         }
         /** for pause to lock the song played */
         if(pauseFlag)pauseCount=currentCount;
         m_sourceDataLine.drain();
         stop();

         /** Reset the progress bar position after audio is completed. */
         lastMicrosecondPosition=m_sourceDataLine.getMicrosecondPosition();
         if(!TestAudioPlayer.start.isEnabled())TestAudioPlayer.start.setEnabled(true);
         if(!TestAudioPlayer.select.isEnabled())TestAudioPlayer.select.setEnabled(true);
      }

      private static void out(String strMessage){
         System.out.println(strMessage);
      }

}


When I call pause method the audio will stop playing, but when I call resume method the audio will drain and stop playing. Sad

-vinesh
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.

xFryIx (60 views)
2014-11-13 12:34:49

digdugdiggy (39 views)
2014-11-12 21:11:50

digdugdiggy (32 views)
2014-11-12 21:10:15

digdugdiggy (28 views)
2014-11-12 21:09:33

kovacsa (50 views)
2014-11-07 19:57:14

TehJavaDev (54 views)
2014-11-03 22:04:50

BurntPizza (53 views)
2014-11-03 18:54:52

moogie (68 views)
2014-11-03 06:22:04

CopyableCougar4 (67 views)
2014-11-01 23:36:41

DarkCart (153 views)
2014-11-01 14:51:03
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

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