KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > lucane > applications > audioconf > audio > FileOutput


1 /*
2  * Lucane - a collaborative platform
3  * Copyright (C) 2004 Vincent Fiack <vfiack@mail15.com>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  */

19 package org.lucane.applications.audioconf.audio;
20
21 import java.io.*;
22
23 /**
24  * A simple test for AudioRecorder and friends
25  */

26 public class FileOutput implements AudioRecorderListener
27 {
28     //-- attributes
29
private String JavaDoc filename;
30     private FileOutputStream output;
31     
32     /**
33      * Constructor
34      *
35      * @param filename the file to write
36      */

37     public FileOutput(String JavaDoc filename)
38     {
39         this.filename = filename;
40     }
41     
42     /**
43      * Recording started, init output stream
44      */

45     public void audioRecordingStarted(AudioConfig config)
46     {
47         try {
48             this.output = new FileOutputStream(this.filename);
49             System.out.println("recording 10 seconds...");
50         } catch (FileNotFoundException e) {
51             e.printStackTrace();
52         }
53     }
54     
55     /**
56      * Audio recorded, write to file
57      */

58     public void audioRecorded(byte[] data, int length)
59     {
60         try {
61             System.out.println("recorded : " +length);
62             this.output.write(data, 0, length);
63         } catch (IOException e) {
64             e.printStackTrace();
65         }
66     }
67
68     /**
69      * Recording ended, close everything
70      */

71     public void audioRecordingEnded()
72     {
73         try {
74             this.output.close();
75             System.out.println("ended.");
76         } catch (IOException e) {
77             e.printStackTrace();
78         }
79     }
80     
81     /**
82      * Main method
83      */

84     public static void main(String JavaDoc[] args)
85     throws Exception JavaDoc
86     {
87         AudioConfig config = new AudioConfig(AudioConfig.NARROWBAND, 3);
88         FileOutput fo = new FileOutput("test.spx");
89         AudioRecorder ar = new AudioRecorder(config);
90         ar.addAudioListener(fo);
91         
92         Thread JavaDoc t = new Thread JavaDoc(ar);
93         
94         t.start();
95         Thread.sleep(10*1000);
96         ar.stop();
97         
98         System.out.println(t.isAlive());
99     }
100 }
Popular Tags