2017-09-12 84 views
-1

我正在嘗試使用名爲OpenSLES的android ndk庫。 當我嘗試在soundPlayer類中調用時,我找不到任何方法,但通過查看github上的NativeAudio示例,我做的一切都正確,或者我跳過了一些我找不到的東西。Android NDK,無法在Java中調用C方法

我在本機LIB-jni.c文件下面的方法:

// create the engine and output mix objects 
void 
Java_com_example_gebruiker_DrumIORemastered_soundPlayer_createEngine(JNIEnv* 
env, jclass clazz) 
    { 
    SLresult result; 

// create engine 
result = slCreateEngine(&engineObject, 0, NULL, 0, NULL, NULL); 
assert(SL_RESULT_SUCCESS == result); 
(void)result; 

// realize the engine 
result = (*engineObject)->Realize(engineObject, SL_BOOLEAN_FALSE); 
assert(SL_RESULT_SUCCESS == result); 
(void)result; 

// get the engine interface, which is needed in order to create other objects 
result = (*engineObject)->GetInterface(engineObject, SL_IID_ENGINE, &engineEngine); 
assert(SL_RESULT_SUCCESS == result); 
(void)result; 

// create output mix, with environmental reverb specified as a non-required interface 
const SLInterfaceID ids[1] = {SL_IID_ENVIRONMENTALREVERB}; 
const SLboolean req[1] = {SL_BOOLEAN_FALSE}; 
result = (*engineEngine)->CreateOutputMix(engineEngine, &outputMixObject, 1, ids, req); 
assert(SL_RESULT_SUCCESS == result); 
(void)result; 

// realize the output mix 
result = (*outputMixObject)->Realize(outputMixObject, SL_BOOLEAN_FALSE); 
assert(SL_RESULT_SUCCESS == result); 
(void)result; 

// get the environmental reverb interface 
// this could fail if the environmental reverb effect is not available, 
// either because the feature is not present, excessive CPU load, or 
// the required MODIFY_AUDIO_SETTINGS permission was not requested and granted 
result = (*outputMixObject)->GetInterface(outputMixObject, SL_IID_ENVIRONMENTALREVERB, 
              &outputMixEnvironmentalReverb); 
if (SL_RESULT_SUCCESS == result) { 
    result = (*outputMixEnvironmentalReverb)->SetEnvironmentalReverbProperties(
      outputMixEnvironmentalReverb, &reverbSettings); 
    (void)result; 
} 
// ignore unsuccessful result codes for environmental reverb, as it is optional for this example 

} 

我的類被稱爲SoundPlayer(我不知道是否有實現Activity工作,我不?這樣想右)

CMakeLists.txt

# Sets the minimum version of CMake required to build your native library. 
# This ensures that a certain set of CMake features is available to 
# your build. 

cmake_minimum_required(VERSION 3.4.1) 

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -Wall") 

add_library( native-lib-jni 

      SHARED 


    C:/Users/Gebruiker/AndroidStudioProjects/DrumIORemastered/ 
    app/src/main/cpp/native-lib-jni.c) 

     include_directories(C:/Users/Gebruiker/AndroidStudioProjects 
    /DrumIORemastered/app/src/main/cpp) 


       target_link_libraries(native-lib-jni 
        android 
        log 
        OpenSLES) 

和我AndroidManifest

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.gebruiker.drumioremastered"> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:roundIcon="@mipmap/ic_launcher_round" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity android:name=".drumPlayerActivity"> 
     android:screenOrientation="portrait" 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 



here is my drumplayer : 


package com.example.gebruiker.drumioremastered; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.widget.Button; 

import com.example.gebruiker.drumioremastered.eventbus.MessageEvent; 
import com.example.gebruiker.drumioremastered.utilities.playButton; 

import org.greenrobot.eventbus.EventBus; 

import butterknife.BindView; 
import butterknife.ButterKnife; 
import butterknife.OnClick; 

public class drumPlayerActivity extends AppCompatActivity { 

@BindView(R.id.playButton1) 
playButton mplayButton; 

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


} 



@OnClick(R.id.playButton1) 
public void playClicked(){ 

    mplayButton.changeButtonstate(); 

    EventBus.getDefault(). 
post(createStateMessage(mplayButton.getButtonState())); 

} 


public MessageEvent createStateMessage(boolean buttonState){ 
    MessageEvent message = new MessageEvent(); 

    if(buttonState){ 
     message.setPlayerState(true); 
     mplayButton.setText("Play"); 
    } 

    else{ 
     message.setPlayerState(false); 
     mplayButton.setText("Stop"); 
    } 

    return message; 
} 
+0

顯示'drumPlayerActivity' – nikis

+0

添加見上 – trOnk12

+0

如果在'native'方法聲明的Java類是'com.example.gebruiker.drumioremastered.SoundPlayer',那你爲什麼在有'com_example_gebruiker_DrumIORemastered_soundPlayer'您C函數名稱? (注意大小寫的區別) – Michael

回答

0

所以@nikis基本上是正確的我把錯誤的路徑給我的類,也忘記了導入庫,我沒有在java類自己寫的本地方法。謝謝:)