Answered
0
0

JAVA

static {
        System.loadLibrary("native-lib");
        System.loadLibrary("fmod");
        System.loadLibrary("fmodL");
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


    setContentView(R.layout.activity_main);
        TextView tv = (TextView) findViewById(R.id.sample_text);

        org.fmod.FMOD.init(getApplicationContext());
        org.fmod.AudioDevice.class.getResource("file:///storage/emulated/0/mp3.mp3");
        tv.setText(stringFromJNI("file:///storage/emulated/0/mp3.mp3",2));
    }
    public native String stringFromJNI(String path, int type);

JAVA C++

Java_com_example_administrator_myapplication_MainActivity_stringFromJNI(
    JNIEnv *env,
    jobject /* this */, jstring path_, jint type) {
std::string hello = "Hello from C++";
const char *path = env->GetStringUTFChars(path_, NULL);
System *system = NULL;
Sound *sound = NULL;
Channel *channel = NULL;
DSP *dsp = NULL;
bool isPlaying = true;
float frequency;

//fmod初始化
System_Create(&system);
//指定最大的声轨数等参数
system->init(32, FMOD_INIT_NORMAL, NULL);

//创建声音对象
system->createSound(path, FMOD_DEFAULT, NULL, &sound);

switch (type) {
    case TYPE_NORMAL:
        //原声播放
        //指定的是音轨0,最后update的时候才会播放
        system->playSound(sound, 0, false, &channel);
        break;

    case TYPE_LUOLI:
        //创建一个数字信号处理对象DSP
        //DSP(数字信号处理)主要原理是:通过改变声音的两个参数:响度(振幅) 声调(频率)
        system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &dsp);
        //设置参数,提高频率,升高一个八度
        dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH, 2.5);
        //把处理对象添加到Channel的音轨0中(注意这里要先播放然后添加音轨特效)
        system->playSound(sound, 0, false, &channel);
        channel->addDSP(0, dsp);
        LOGE("%s", "luo li");
        break;

    case TYPE_DASHU:
        //创建一个数字信号处理对象DSP
        system->createDSPByType(FMOD_DSP_TYPE_PITCHSHIFT, &dsp);
        //设置参数,提高频率,升高一个八度
        dsp->setParameterFloat(FMOD_DSP_PITCHSHIFT_PITCH, 0.8);
        //把处理对象添加到Channel的音轨0中
        system->playSound(sound, 0, false, &channel);
        channel->addDSP(0, dsp);
        LOGE("%s", "da shu");
        break;

    case TYPE_JINGSONG:
        //惊悚
        system->createDSPByType(FMOD_DSP_TYPE_TREMOLO, &dsp);
        dsp->setParameterFloat(FMOD_DSP_TREMOLO_SKEW, 0.5);
        system->playSound(sound, 0, false, &channel);
        channel->addDSP(0, dsp);
        break;

    case TYPE_GAOGUAI:
        //搞怪
        //提高说话的速度
        system->playSound(sound, 0, false, &channel);
        channel->getFrequency(&frequency);
        frequency = frequency * 1.6;
        channel->setFrequency(frequency);
        LOGI("%s", "fix gaoguai");
        break;

    case TYPE_KONGLING:
        //空灵
        system->createDSPByType(FMOD_DSP_TYPE_ECHO, &dsp);
        dsp->setParameterFloat(FMOD_DSP_ECHO_DELAY, 300);
        dsp->setParameterFloat(FMOD_DSP_ECHO_FEEDBACK, 20);
        system->playSound(sound, 0, false, &channel);
        channel->addDSP(0, dsp);
        LOGI("%s", "fix kongling");
        break;

    default:
        break;
}

//update的时候才会播放
system->update();

//每秒钟判断一下是否在播放
while (isPlaying) {
    channel->isPlaying(&isPlaying);
    usleep(1 * 1000 * 1000);//单位是微妙,这里是1秒延时
}

//CMake默认支持异常处理。
//播放的时候可能会有异常,例如文件找不到等等,然后把异常抛给Java,这里就省略了
//    try {
//
//    } catch (...) {
//
//    }


//释放资源
sound->release();
system->close();
system->release();
return env->NewStringUTF(hello.c_str());

I cann’t hear the sound. Why???

  • You must to post comments
Best Answer
0
0

I highly recommend you take a look at our Android examples and build on them to get started.

Firstly, please ensure you System.loadLibrary “fmod” OR “fmodL” not both.
Also, ensure you load the FMOD library before your “native-lib”.

You should check all error codes for clues as to what is not working.
I suspect you are getting FILE_NOT_FOUND as “file:///storage/emulated/0/mp3.mp3” isn’t a valid path.

  • You must to post comments
Showing 1 result
Your Answer

Please first to submit.