简体   繁体   English

为什么 oboe 中名为 OboeSinePlayer 的示例代码一直在 Android 上停止?

[英]Why does the sample code called OboeSinePlayer from oboe keep stopping on Android?

I am adding code from the following page to a hello world c++ application.我正在将以下页面中的代码添加到 hello world c++ 应用程序。

https://github.com/google/oboe/blob/master/docs/GettingStarted.md 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();如果我注释掉 oboeSinePlayer(); then the MainActivity stays open, but when it is not commented out, the app keeps stopping.然后 MainActivity 保持打开状态,但是当它没有被注释掉时,应用程序会一直停止。

Here is the code for oboeSinePlayer:这是 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:和相关的 header 文件:

#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.运行时,听起来 onAudioReady 方法在应用停止之前播放了一次。 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.我查看了 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]]] couldn't find "liboboe.so" /vendor/lib]]] 找不到“liboboe.so”

Here is my CMakeLists.txt这是我的 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?运行一次 onAudioReady 后会导致该错误的原因是什么?

I got rid of the error by refreshing c++ code, now I get the error我通过刷新 c++ 代码摆脱了错误,现在我得到了错误

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

Your app is crashing because oboeSinePlayer goes out of scope almost immediately after you declare it.您的应用程序正在崩溃,因为oboeSinePlayer在您声明它后几乎立即退出 scope。 Solve this by declaring oboeSinePlayer as a global:通过oboeSinePlayer声明为全局变量来解决此问题:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM