1 /*
2 * Copyright 2022 The Android Open Source Project
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 #include <jni.h>
18 #include <sys/types.h>
19 #include <sys/stat.h>
20 #include <fcntl.h>
21 #include <unistd.h>
22
23 static const char *TAG = "MinimalOboeJNI";
24
25 #include <android/log.h>
26
27 #include <SimpleNoiseMaker.h>
28
29 // JNI functions are "C" calling convention
30 #ifdef __cplusplus
31 extern "C" {
32 #endif
33
34 using namespace oboe;
35
36 // Use a static object so we don't have to worry about it getting deleted at the wrong time.
37 static SimpleNoiseMaker sPlayer;
38
39 /**
40 * Native (JNI) implementation of AudioPlayer.startAudiostreamNative()
41 */
Java_com_example_minimaloboe_AudioPlayer_startAudioStreamNative(JNIEnv *,jobject)42 JNIEXPORT jint JNICALL Java_com_example_minimaloboe_AudioPlayer_startAudioStreamNative(
43 JNIEnv * /* env */, jobject) {
44 __android_log_print(ANDROID_LOG_INFO, TAG, "%s", __func__);
45 Result result = sPlayer.open();
46 if (result == Result::OK) {
47 result = sPlayer.start();
48 }
49 return (jint) result;
50 }
51
52 /**
53 * Native (JNI) implementation of AudioPlayer.stopAudioStreamNative()
54 */
Java_com_example_minimaloboe_AudioPlayer_stopAudioStreamNative(JNIEnv *,jobject)55 JNIEXPORT jint JNICALL Java_com_example_minimaloboe_AudioPlayer_stopAudioStreamNative(
56 JNIEnv * /* env */, jobject) {
57 __android_log_print(ANDROID_LOG_INFO, TAG, "%s", __func__);
58 // We need to close() even if the stop() fails because we need to delete the resources.
59 Result result1 = sPlayer.stop();
60 Result result2 = sPlayer.close();
61 // Return first failure code.
62 return (jint) ((result1 != Result::OK) ? result1 : result2);
63 }
64 #ifdef __cplusplus
65 }
66 #endif
67