2

我正在将以下页面中的代码添加到 hello world c++ 应用程序中。

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

这是主要活动:

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();
}

如果我注释掉 oboeSinePlayer(); 然后 MainActivity 保持打开状态,但是当它没有被注释掉时,应用程序会一直停止。

这是 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();
}

以及相关的头文件:

#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;

};

运行时,听起来 onAudioReady 方法在应用停止之前播放了一次。我相信应该一遍又一遍地调用该方法以提供持续的正弦音。

任何有关如何解决此问题的帮助将不胜感激。感谢您花时间查看我的问题。

我查看了 logcat,发现 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]]]找不到“liboboe.so”

这是我的 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})

运行一次 onAudioReady 后会导致该错误的原因是什么?

我通过刷新 c++ 代码摆脱了错误,现在我得到了错误

/com.example.gettingstartedoboe A/libc:致命信号 4 (SIGILL),代码 2,tid 15656 (tingstartedoboe) 中的故障地址 0xa94b6a47

4

1 回答 1

1

您的应用程序正在崩溃,因为oboeSinePlayer在您声明它之后几乎立即超出范围。通过声明oboeSinePlayer为全局来解决这个问题:

改变

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

OboeSinePlayer oboeSinePlayer;

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

    oboeSinePlayer.start();
}
于 2019-11-04T13:40:35.313 回答