Skip to content

Commit caca82c

Browse files
committed
AudioPlayer and AudioRecorder utility classes which utilize the AudioDeviceModule
1 parent 8b33c7c commit caca82c

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2021 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.onvoid.webrtc.media.audio;
18+
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
21+
public class AudioPlayer {
22+
23+
private final AtomicBoolean playing;
24+
25+
private AudioDeviceModule module;
26+
27+
private AudioDevice device;
28+
29+
private AudioSource source;
30+
31+
32+
public AudioPlayer() {
33+
playing = new AtomicBoolean();
34+
}
35+
36+
public void setAudioDevice(AudioDevice device) {
37+
this.device = device;
38+
}
39+
40+
public void setAudioSource(AudioSource source) {
41+
this.source = source;
42+
}
43+
44+
public void start() {
45+
if (playing.compareAndSet(false, true)) {
46+
module = new AudioDeviceModule();
47+
module.setPlayoutDevice(device);
48+
module.setAudioSource(source);
49+
module.initPlayout();
50+
module.startPlayout();
51+
}
52+
}
53+
54+
public void stop() {
55+
if (playing.compareAndSet(true, false)) {
56+
module.stopPlayout();
57+
module.dispose();
58+
}
59+
}
60+
61+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2021 Alex Andres
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package dev.onvoid.webrtc.media.audio;
18+
19+
import java.util.concurrent.atomic.AtomicBoolean;
20+
21+
public class AudioRecorder {
22+
23+
private final AtomicBoolean capturing;
24+
25+
private AudioDeviceModule module;
26+
27+
private AudioDevice device;
28+
29+
private AudioSink sink;
30+
31+
32+
public AudioRecorder() {
33+
capturing = new AtomicBoolean();
34+
}
35+
36+
public void setAudioDevice(AudioDevice device) {
37+
this.device = device;
38+
}
39+
40+
public void setAudioSink(AudioSink sink) {
41+
this.sink = sink;
42+
}
43+
44+
public void start() {
45+
if (capturing.compareAndSet(false, true)) {
46+
module = new AudioDeviceModule();
47+
module.setRecordingDevice(device);
48+
module.setAudioSink(sink);
49+
module.initRecording();
50+
module.startRecording();
51+
}
52+
}
53+
54+
public void stop() {
55+
if (capturing.compareAndSet(true, false)) {
56+
module.stopRecording();
57+
module.dispose();
58+
}
59+
}
60+
61+
}

0 commit comments

Comments
 (0)