2

I am adding code from the following page to a hello world c++ application.

https://github.com/google/oboe/blob/master/docs/GettingStarted.md

Here is the MainActivity:

package com.example.gettingstartedoboe;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        oboeSinePlayer();
        setContentView(R.layout.activity_main);

        TextView tv = findViewById(R.id.sample_text);
        tv.setText(stringFromJNI());
        play();

    }

    /**
     * A native method that is implemented by the 'native-lib' native library,
     * which is packaged with this application.
     */
    public native String stringFromJNI();

    public native void oboeSinePlayer();
}

If I comment out oboeSinePlayer(); then the MainActivity stays open, but when it is not commented out, the app keeps stopping.

Here is the code for oboeSinePlayer:

#include <jni.h>
#include "OboeSinePlayer.h"

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_gettingstartedoboe_MainActivity_oboeSinePlayer(JNIEnv *env, jobject /* this */) {
    OboeSinePlayer oboeSinePlayer;
    oboeSinePlayer.start();
}

oboe::DataCallbackResult
OboeSinePlayer::onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) {
    float *floatData = (float *) audioData;
    for (int i = 0; i < numFrames; i++) {
        float sampleValue = kAmplitude * sinf(mPhase);
        for (int j = 0; j < kChannelCount; j++) {
            floatData[i * kChannelCount + j] = sampleValue;
        }
        mPhase += mPhaseIncrement;
        if (mPhase >= kTwoPi) {
            mPhase -= kTwoPi;
        }
    }
    return oboe::DataCallbackResult::Continue;
}

bool OboeSinePlayer::start() {
    oboe::AudioStreamBuilder builder;
    builder.setSharingMode(oboe::SharingMode::Shared)
            ->setPerformanceMode(oboe::PerformanceMode::LowLatency)
            ->setChannelCount(kChannelCount)
            ->setSampleRate(kSampleRate)
            ->setFormat(oboe::AudioFormat::Float)
            ->setCallback(this)
            ->openManagedStream(managedStream);
    managedStream->requestStart();
}

and the associated header file:

#include <oboe/Oboe.h>
#include <math.h>

#ifndef GETTINGSTARTEDOBOE_OBOESINEPLAYER_H
#define GETTINGSTARTEDOBOE_OBOESINEPLAYER_H

#endif //GETTINGSTARTEDOBOE_OBOESINEPLAYER_H

class OboeSinePlayer : public oboe::AudioStreamCallback {

public:
    bool start();

    oboe::DataCallbackResult onAudioReady(oboe::AudioStream *oboeStream, void *audioData, int32_t numFrames) override;

private:
    oboe::ManagedStream managedStream;
    int kSampleRate = 44100;
    int kChannelCount = oboe::ChannelCount::Mono;
    float kAmplitude = 0.5f;
    float mPhase = 0.0f;
    float kFrequency = 440.0f;
    float kTwoPi = 2.0f * M_PI;
    double mPhaseIncrement = kTwoPi * kFrequency / (double) kSampleRate;

};

When run, it sounds like the onAudioReady method is being played once before the app stops. I believe the method is supposed to be called over and over to provide a sustained sine tone.

Any help on how to solve this would be greatly appreciated. Thank you for taking the time to look over my question.

I looked at logcat and found the line java.lang.UnsatisfiedLinkError: dalvik.system.PathClassLoader[DexPathList[[zip file "/data/app/com.example.hellosound-2/base.apk"],nativeLibraryDirectories=[/data/app/com.example.hellosound-2/lib/x86, /data/app/com.example.hellosound-2/base.apk!/lib/x86, /system/lib, /vendor/lib]]] couldn't find "liboboe.so"

Here is my CMakeLists.txt

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html

# Sets the minimum version of CMake required to build the native library.

cmake_minimum_required(VERSION 3.4.1)

# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.

add_library( # Sets the name of the library.
        native-lib

        # Sets the library as a shared library.
        SHARED

        # Provides a relative path to your source file(s).
        native-lib.cpp
        OboeSinePlayer.cpp)

# For using Oboe
set(OBOE_DIR "C:/Users/Al/Documents/Goboe")
add_subdirectory(${OBOE_DIR} ./oboe)
include_directories (${OBOE_DIR}/include)


# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.

find_library( # Sets the name of the path variable.
        log-lib

        # Specifies the name of the NDK library that
        # you want CMake to locate.
        log)

# Specifies libraries CMake should link to your target library. You
# can link multiple libraries, such as libraries you define in this
# build script, prebuilt third-party libraries, or system libraries.

target_link_libraries( # Specifies the target library.
        native-lib
        oboe
        # Links the target library to the log library
        # included in the NDK.
        ${log-lib})

What would cause that error after running through onAudioReady once?

I got rid of the error by refreshing c++ code, now I get the error

/com.example.gettingstartedoboe A/libc: Fatal signal 4 (SIGILL), code 2, fault addr 0xa94b6a47 in tid 15656 (tingstartedoboe)

Al G Johnston
  • 129
  • 2
  • 10

1 Answers1

1

Your app is crashing because oboeSinePlayer goes out of scope almost immediately after you declare it. Solve this by declaring oboeSinePlayer as a global:

Change

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_gettingstartedoboe_MainActivity_oboeSinePlayer(JNIEnv *env, 
jobject /* this */) {
    OboeSinePlayer oboeSinePlayer;
    oboeSinePlayer.start();
}

to

OboeSinePlayer oboeSinePlayer;

extern "C" JNIEXPORT jstring JNICALL
Java_com_example_gettingstartedoboe_MainActivity_oboeSinePlayer(JNIEnv *env, 
jobject /* this */) {

    oboeSinePlayer.start();
}
donturner
  • 17,867
  • 8
  • 59
  • 81
  • It still crashes – Al G Johnston Nov 04 '19 at 15:03
  • Are you getting any build errors? Make sure you are looking in the right place for them: https://stackoverflow.com/questions/55476461/error-computing-cmake-server-result-in-android-studio/55476636#comment99078465_55476636 – donturner Nov 05 '19 at 16:05