|
18 | 18 | import android.view.View.OnClickListener; |
19 | 19 | import android.widget.ImageButton; |
20 | 20 | import android.widget.TextView; |
| 21 | +import android.widget.Toast; |
21 | 22 |
|
22 | 23 | public class AudioRecorderWAudioRecord extends Activity implements Runnable { |
23 | | -private static final String MEDIAFILE= "/sdcard/audiorecordwaudiorecordexample.pcm"; |
24 | | - |
25 | | -private AudioRecord recorder; |
26 | | -private DataOutputStream dos; |
27 | | -private boolean recordablestate = false; |
28 | | -private int time = 0; |
29 | | - |
30 | | -@Override |
31 | | -public void onCreate(Bundle savedInstanceState) { |
32 | | -super.onCreate(savedInstanceState); |
33 | | - |
34 | | -setupView(); |
35 | | -} |
36 | | - |
37 | | -@Override |
38 | | -public void onDestroy() { |
39 | | -super.onDestroy(); |
40 | | -stopRecord(); |
41 | | -} |
42 | | - |
43 | | -private void setupView() { |
44 | | -setContentView(R.layout.audiorecorder); |
45 | | - |
46 | | -findViewById(R.id.recordstop).setOnClickListener(new OnClickListener() { |
47 | | - |
48 | | -@Override |
49 | | - public void onClick(View v) { |
50 | | -recordOrStop(); |
51 | | -} |
52 | | - |
53 | | -}); |
54 | | - |
55 | | -findViewById(R.id.play).setOnClickListener(new OnClickListener() { |
56 | | - |
57 | | -@Override |
58 | | - public void onClick(View v) { |
59 | | -playAudio(); |
60 | | -} |
61 | | - |
62 | | -}); |
63 | | -} |
64 | | - |
65 | | -private void recordOrStop() { |
66 | | -if(!recordablestate) { |
67 | | -try { |
68 | | -recordablestate = true; |
69 | | -ImageButton button = (ImageButton) findViewById(R.id.recordstop); |
70 | | -button.setImageResource(R.drawable.stop); |
71 | | -findViewById(R.id.play).setVisibility(View.GONE); |
72 | | - |
73 | | -File mediafile = new File(MEDIAFILE); |
74 | | -if(mediafile.exists()) { // delete mediafile if it already exists |
75 | | -mediafile.delete(); |
76 | | -} |
77 | | - |
78 | | -// create mediafile |
79 | | -mediafile.createNewFile(); |
80 | | - |
81 | | -// setup DataOutputStream to write to file |
82 | | -dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(mediafile))); |
83 | | - |
84 | | -// setup AudioRecord |
85 | | -final short[] buffer = new short[10000]; |
86 | | - |
87 | | -recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, // source to record from |
88 | | -11025,// frequency |
89 | | -AudioFormat.CHANNEL_CONFIGURATION_MONO,// channel config.. mono, stereo, etc |
90 | | -AudioFormat.ENCODING_PCM_16BIT,// audio encoding |
91 | | -buffer.length// buffer size |
92 | | -); |
93 | | - |
94 | | -// start recording in a separate thread |
95 | | -(new Thread() { |
96 | | -@Override |
97 | | - public void run() { |
98 | | -if(recorder != null) { |
99 | | -recorder.startRecording(); |
100 | | -while(recordablestate) { |
101 | | -try { |
102 | | -int readBytes = recorder.read(buffer, 0, buffer.length); // read in up to buffer size |
103 | | -for(int i=0;i<readBytes;i++) { |
104 | | -dos.writeShort(buffer[i]); // write out to file |
105 | | -} |
106 | | -} catch (Exception t) { |
107 | | -recordablestate = false; |
108 | | -t.printStackTrace(); |
109 | | -} |
110 | | -} |
111 | | -stopRecord(); // stop recording |
112 | | -} |
113 | | -} |
114 | | -}).start(); |
115 | | - |
116 | | -(new Thread(this)).start(); |
117 | | -} catch (Exception e) { |
118 | | -e.printStackTrace(); |
119 | | -} |
120 | | -} else { |
121 | | -recordablestate = false; |
122 | | -ImageButton button = (ImageButton) findViewById(R.id.recordstop); |
123 | | -button.setImageResource(R.drawable.record); |
124 | | -findViewById(R.id.play).setVisibility(View.VISIBLE); |
125 | | -} |
126 | | -} |
127 | | - |
128 | | -private void stopRecord() { |
129 | | -recordablestate = false; |
130 | | -if(recorder != null) { |
131 | | -try { recorder.stop(); } catch (IllegalStateException ise) { ise.printStackTrace(); } |
132 | | -recorder.release(); |
133 | | -recorder = null; |
134 | | -} |
135 | | -if(dos != null) { |
136 | | -try { dos.flush(); } catch (IOException io) { io.printStackTrace(); } |
137 | | -try { dos.close(); } catch (IOException io) { io.printStackTrace(); } |
138 | | -dos = null; |
139 | | -} |
140 | | -} |
141 | | - |
142 | | -private final Handler timeupdater = new Handler() { |
143 | | -@Override |
144 | | - public void handleMessage(Message msg) { |
145 | | -time++; |
146 | | -((TextView) findViewById(R.id.time)).setText(""+time+" secs"); |
147 | | -} |
148 | | -}; |
149 | | - |
150 | | -@Override |
151 | | - public void run() { |
152 | | -try { |
153 | | -time = 0; |
154 | | -timeupdater.sendEmptyMessage(0); |
155 | | -while(recordablestate) { |
156 | | -Thread.sleep(1000); // per sec |
157 | | -timeupdater.sendEmptyMessage(0); |
158 | | -} |
159 | | -} catch (Exception t) { } |
160 | | -} |
161 | | - |
162 | | -private void playAudio() { |
163 | | -finish(); |
164 | | - |
165 | | -Intent intent = new Intent(this, AudioPlayerWAudioTrack.class); |
166 | | -intent.putExtra(AudioPlayer.AUDIOFILEPATH, MEDIAFILE); |
167 | | -startActivity(intent); |
168 | | -} |
| 24 | + private static final String MEDIAFILE= "/sdcard/audiorecordwaudiorecordexample.pcm"; |
| 25 | + |
| 26 | + private AudioRecord recorder; |
| 27 | + private DataOutputStream dos; |
| 28 | + private boolean recordablestate = false; |
| 29 | + private int time = 0; |
| 30 | + |
| 31 | + @Override |
| 32 | + public void onCreate(Bundle savedInstanceState) { |
| 33 | + super.onCreate(savedInstanceState); |
| 34 | + |
| 35 | + setupView(); |
| 36 | + } |
| 37 | + |
| 38 | + @Override |
| 39 | + public void onDestroy() { |
| 40 | + super.onDestroy(); |
| 41 | + stopRecord(); |
| 42 | + } |
| 43 | + |
| 44 | + private void setupView() { |
| 45 | + setContentView(R.layout.audiorecorder); |
| 46 | + |
| 47 | + findViewById(R.id.recordstop).setOnClickListener(new OnClickListener() { |
| 48 | + |
| 49 | + @Override |
| 50 | + public void onClick(View v) { |
| 51 | + recordOrStop(); |
| 52 | + } |
| 53 | + |
| 54 | + }); |
| 55 | + |
| 56 | + findViewById(R.id.play).setOnClickListener(new OnClickListener() { |
| 57 | + |
| 58 | + @Override |
| 59 | + public void onClick(View v) { |
| 60 | + playAudio(); |
| 61 | + } |
| 62 | + |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + private void recordOrStop() { |
| 67 | + if(!recordablestate) { |
| 68 | + try { |
| 69 | + recordablestate = true; |
| 70 | + ImageButton button = (ImageButton) findViewById(R.id.recordstop); |
| 71 | + button.setImageResource(R.drawable.stop); |
| 72 | + findViewById(R.id.play).setVisibility(View.GONE); |
| 73 | + |
| 74 | + File mediafile = new File(MEDIAFILE); |
| 75 | + if(mediafile.exists()) { // delete mediafile if it already exists |
| 76 | + mediafile.delete(); |
| 77 | + } |
| 78 | + |
| 79 | + // create mediafile |
| 80 | + mediafile.createNewFile(); |
| 81 | + |
| 82 | + // setup DataOutputStream to write to file |
| 83 | + dos = new DataOutputStream(new BufferedOutputStream(new FileOutputStream(mediafile))); |
| 84 | + |
| 85 | + // setup AudioRecord |
| 86 | + final short[] buffer = new short[10000]; |
| 87 | + |
| 88 | + recorder = new AudioRecord(MediaRecorder.AudioSource.MIC, // source to record from |
| 89 | + 44100,// frequency |
| 90 | + AudioFormat.CHANNEL_IN_MONO,// channel config.. mono, stereo, etc |
| 91 | + AudioFormat.ENCODING_PCM_16BIT,// audio encoding |
| 92 | + buffer.length// buffer size |
| 93 | + ); |
| 94 | + |
| 95 | + // start recording in a separate thread |
| 96 | + (new Thread() { |
| 97 | + @Override |
| 98 | + public void run() { |
| 99 | + if(recorder != null) { |
| 100 | + try { |
| 101 | + recorder.startRecording(); |
| 102 | + } catch(IllegalStateException ise) { |
| 103 | + Message.obtain(notifyHandler, 0, "This is best run on a real device.").sendToTarget(); |
| 104 | + recorder = null; |
| 105 | + recordablestate = false; |
| 106 | + } |
| 107 | + while(recordablestate) { |
| 108 | + try { |
| 109 | + int readBytes = recorder.read(buffer, 0, buffer.length); // read in up to buffer size |
| 110 | + for(int i=0;i<readBytes;i++) { |
| 111 | + dos.writeShort(buffer[i]); // write out to file |
| 112 | + } |
| 113 | + } catch (Exception t) { |
| 114 | + recordablestate = false; |
| 115 | + t.printStackTrace(); |
| 116 | + } |
| 117 | + } |
| 118 | + stopRecord(); // stop recording |
| 119 | + } |
| 120 | + } |
| 121 | + }).start(); |
| 122 | + |
| 123 | + (new Thread(this)).start(); |
| 124 | + } catch (Exception e) { |
| 125 | + e.printStackTrace(); |
| 126 | + } |
| 127 | + } else { |
| 128 | + recordablestate = false; |
| 129 | + ImageButton button = (ImageButton) findViewById(R.id.recordstop); |
| 130 | + button.setImageResource(R.drawable.record); |
| 131 | + findViewById(R.id.play).setVisibility(View.VISIBLE); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private void stopRecord() { |
| 136 | + recordablestate = false; |
| 137 | + if(recorder != null) { |
| 138 | + try { recorder.stop(); } catch (IllegalStateException ise) { } |
| 139 | + recorder.release(); |
| 140 | + recorder = null; |
| 141 | + } |
| 142 | + if(dos != null) { |
| 143 | + try { dos.flush(); } catch (IOException io) { } |
| 144 | + try { dos.close(); } catch (IOException io) { } |
| 145 | + dos = null; |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private final Handler timeupdater = new Handler() { |
| 150 | + @Override |
| 151 | + public void handleMessage(Message msg) { |
| 152 | + time++; |
| 153 | + ((TextView) findViewById(R.id.time)).setText(""+time+" secs"); |
| 154 | + } |
| 155 | + }; |
| 156 | + |
| 157 | + @Override |
| 158 | + public void run() { |
| 159 | + try { |
| 160 | + time = 0; |
| 161 | + timeupdater.sendEmptyMessage(0); |
| 162 | + while(recordablestate) { |
| 163 | + Thread.sleep(1000); // per sec |
| 164 | + timeupdater.sendEmptyMessage(0); |
| 165 | + } |
| 166 | + } catch (Exception t) { } |
| 167 | + } |
| 168 | + |
| 169 | + private void playAudio() { |
| 170 | + finish(); |
| 171 | + |
| 172 | + Intent intent = new Intent(this, AudioPlayerWAudioTrack.class); |
| 173 | + intent.putExtra(AudioPlayer.AUDIOFILEPATH, MEDIAFILE); |
| 174 | + startActivity(intent); |
| 175 | + } |
| 176 | + |
| 177 | + private final Handler notifyHandler = new Handler() { |
| 178 | + public void handleMessage(Message msg) { |
| 179 | + if(msg.obj != null && msg.obj instanceof String) notifyMsg((String) msg.obj); |
| 180 | + } |
| 181 | + }; |
| 182 | + |
| 183 | + private void notifyMsg(String msg) { |
| 184 | + Toast.makeText(this, msg, Toast.LENGTH_SHORT).show(); |
| 185 | + } |
169 | 186 | } |
0 commit comments