Почему пример кода под названием OboeSinePlayer от oboe останавливается на Android? - PullRequest
2 голосов
/ 30 октября 2019

Я добавляю код со следующей страницы в приложение hello world c ++.

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

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

Если я комментируюout 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, адрес ошибки0xa94b6a47 в тид 15656 (тингстартедобое)

1 Ответ

0 голосов
/ 04 ноября 2019

Ваше приложение падает, потому что 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();
}
...