HarmonyOS 鸿蒙Next oh_audio_play 开发没返回

发布于 1周前 作者 itying888 来自 鸿蒙OS

HarmonyOS 鸿蒙Next oh_audio_play 开发没返回

ret = OH_AudioStreamBuilder_SetSamplingRate(audio_render_builder_, 48000);
ret = OH_AudioStreamBuilder_SetChannelCount(audio_render_builder_, 1);
OH_AudioStreamBuilder_SetSampleFormat(audio_render_builder_, AUDIOSTREAM_SAMPLE_S16LE);
OH_AudioStreamBuilder_SetEncodingType(audio_render_builder_, AUDIOSTREAM_ENCODING_TYPE_RAW);

// OH_AudioStreamBuilder_SetEncodingType(audio_render_builder_, AUDIOSTREAM_ENCODING_TYPE_AUDIOVIVID);
// OH_AudioStreamBuilder_SetRendererInfo(audio_render_builder_, AUDIOSTREAM_USAGE_MUSIC);
OH_AudioStreamBuilder_SetLatencyMode(audio_render_builder_, AUDIOSTREAM_LATENCY_MODE_FAST);

// OH_AudioStreamBuilder_SetFrameSizeInCallback(audio_render_builder_, 480 * 2);
OH_AudioRenderer_Callbacks callbacks;
// 配置回调函数
callbacks.OH_AudioRenderer_OnWriteData = OhAudioPlayer::OnWriteData;
callbacks.OH_AudioRenderer_OnStreamEvent = OhAudioPlayer::OnStreamEvent;
callbacks.OH_AudioRenderer_OnInterruptEvent = OhAudioPlayer::OnInterruptEvent;
callbacks.OH_AudioRenderer_OnError = OhAudioPlayer::OnError;

OH_AudioStreamBuilder_SetRendererCallback(audio_render_builder_, callbacks, this);

OH_AudioStreamBuilder_GenerateRenderer(audio_render_builder_, &audio_render_);

问题: OnWriteData 没有回调回来,前几天是好的


更多关于HarmonyOS 鸿蒙Next oh_audio_play 开发没返回的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html

2 回复

提供一个demo 场景:普通录制和播放实现

具体步骤如下:

步骤一:导入audio模块, 引入OHAudio头文件, 提供播放和录制能力的API

引入音频库和动态链接库

import { BusinessError } from '@ohos.base';
import audioPlayer from 'libentry.so'

动态链接库audioPlayer里面声明相关函数定义

export const audioCapturerLowLatencyInit: () => void;
export const audioCapturerStart: () => void;
export const audioCapturerPause: () => void;
export const audioCapturerStop: () => void;
export const audioCapturerRelease: () => void;
export const audioRendererInit: () => void;
export const audioRendererLowLatencyInit: () => void;
export const audioRendererStart: () => void;
export const audioRendererPause: () => void;
export const audioRendererStop: () => void;
export const audioRendererRelease: () => void;
export const getRendererState: () => number;
export const getFileState: () => number;
export const getFramesWritten: () => number;
export const getFileSize: () => number;
export const closeFile: () => number;

native侧接口映射,native侧和JS侧方法交互

{
    napi_property_descriptor desc[] = {
        {"closeFile", nullptr, CloseFile, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioCapturerLowLatencyInit", nullptr, AudioCapturerLowLatencyInit, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioCapturerInit", nullptr, AudioCapturerInit, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioCapturerStart", nullptr, AudioCapturerStart, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioCapturerPause", nullptr, AudioCapturerPause, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioCapturerRelease", nullptr, AudioCapturerRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioCapturerStop", nullptr, AudioCapturerStop, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioRendererLowLatencyInit", nullptr, AudioRendererLowLatencyInit, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioRendererInit", nullptr, AudioRendererInit, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioRendererStart", nullptr, AudioRendererStart, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioRendererPause", nullptr, AudioRendererPause, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioRendererRelease", nullptr, AudioRendererRelease, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"getRendererState", nullptr, GetRendererState, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"getFileSize", nullptr, GetFileSize, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"getFramesWritten", nullptr, GetFramesWritten, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"getFileState", nullptr, GetFileState, nullptr, nullptr, nullptr, napi_default, nullptr},
        {"audioRendererStop", nullptr, AudioRendererStop, nullptr, nullptr, nullptr, napi_default, nullptr}};
    napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
    return exports;
}

EXTERN_C_END

步骤二:录制实现

Step1:创建录制和播放构造器

static OH_AudioRenderer *audioRenderer;
static OH_AudioStreamBuilder *builder;
static OH_AudioStreamBuilder *rendererBuilder;

Step2:构造录制音频流,设置音频流参数和音频回调函数

{
    if (audioCapturer) {
        OH_AudioCapturer_Release(audioCapturer);
        OH_AudioStreamBuilder_Destroy(builder);
        fclose(g_file);
        g_file = nullptr;
        audioCapturer = nullptr;
        builder = nullptr;
    }
    g_file = fopen(g_filePath.c_str(), "wb");
    // create builder
    OH_AudioStream_Type type = AUDIOSTREAM_TYPE_CAPTURER;
    OH_AudioStreamBuilder_Create(&builder, type);
    // set params and callbacks
    OH_AudioStreamBuilder_SetSamplingRate(builder, g_samplingRate);
    OH_AudioStreamBuilder_SetChannelCount(builder, g_channelCount);
    callbacks.OH_AudioCapturer_OnReadData = AudioCapturerOnReadData;
    OH_AudioStreamBuilder_SetCapturerCallback(builder, callbacks, nullptr);
    // create OH_AudioCapturer
    OH_AudioStreamBuilder_GenerateCapturer(builder, &audioCapturer);
    return nullptr;
}

Step3:开始录制,暂停录制,停止录制

{
    // start
    OH_AudioCapturer_Start(audioCapturer);
    return nullptr;
}

static napi_value AudioCapturerPause(napi_env env, napi_callback_info info)
{
    OH_AudioCapturer_Pause(audioCapturer);
    return nullptr;
}

static napi_value AudioCapturerStop(napi_env env, napi_callback_info info)
{
    OH_AudioCapturer_Stop(audioCapturer);
    return nullptr;
}

Step4:将采集到的音频数据写入文件中

{
    size_t count = 1;
    if (fwrite(buffer, bufferLen, count, g_file) != count) {
        printf("buffer fwrite err");
    }
    return 0;
}

Step5:释放构造器

{
    if (audioCapturer) {
        OH_AudioCapturer_Release(audioCapturer);
        OH_AudioStreamBuilder_Destroy(builder);
        audioCapturer = nullptr;
        builder = nullptr;
    }
    return nullptr;
}

步骤三:播放实现

Step1:构造播放音频流,设置音频流参数和音频回调函数

{
    if (audioRenderer) {
        OH_AudioRenderer_Release(audioRenderer);
        OH_AudioStreamBuilder_Destroy(rendererBuilder);
        fclose(g_file);
        g_file = nullptr;
        audioRenderer = nullptr;
        rendererBuilder = nullptr;
    }
    g_file = fopen(g_filePath.c_str(), "rb");
    if (g_file == nullptr) {
        return 0;
    }
    // create builder
    OH_AudioStream_Type type = AUDIOSTREAM_TYPE_RENDERER;
    OH_AudioStreamBuilder_Create(&rendererBuilder, type);
    // set params and callbacks
    OH_AudioStreamBuilder_SetSamplingRate(rendererBuilder, g_samplingRate);
    OH_AudioStreamBuilder_SetChannelCount(rendererBuilder, g_channelCount);
    rendererCallbacks.OH_AudioRenderer_OnWriteData = AudioRendererOnWriteData;
    OH_AudioStreamBuilder_SetRendererCallback(rendererBuilder, rendererCallbacks, nullptr);
    OH_AudioRenderer_Callbacks callbacks;
    // 配置回调函数
    callbacks.OH_AudioRenderer_OnWriteData = MyOnWriteData;
    callbacks.OH_AudioRenderer_OnStreamEvent = MyOnStreamEvent;
    callbacks.OH_AudioRenderer_OnInterruptEvent = MyOnInterruptEvent;
    callbacks.OH_AudioRenderer_OnError = MyOnError;
    OH_AudioStreamBuilder_GenerateRenderer(rendererBuilder, &audioRenderer);
    g_readEnd = false;
    g_rendererLowLatency = false;
    return nullptr;
}

Step2:读取音频文件进行渲染

{
    size_t readCount = fread(buffer, bufferLen, 1, g_file);
    if (!readCount) {
        if (ferror(g_file)) {
            printf("Error reading myfile");
        } else if (feof(g_file)) {
            OH_AudioRenderer_Stop(audioRenderer);
            OH_AudioRenderer_Release(audioRenderer);
            OH_AudioStreamBuilder_Destroy(rendererBuilder);
            fclose(g_file);
            g_file = fopen(g_filePath.c_str(), "rb");
            // 1. create builder
            OH_AudioStream_Type type = AUDIOSTREAM_TYPE_RENDERER;
            OH_AudioStreamBuilder_Create(&rendererBuilder, type);
            // 2. set params and callbacks
            OH_AudioStreamBuilder_SetSamplingRate(rendererBuilder, g_samplingRate);
            OH_AudioStreamBuilder_SetChannelCount(rendererBuilder, g_channelCount);
            if (g_rendererLowLatency) {
                OH_AudioStreamBuilder_SetLatencyMode(rendererBuilder, (OH_AudioStream_LatencyMode)g_latencyMode);
            }
            rendererCallbacks.OH_AudioRenderer_OnWriteData = AudioRendererOnWriteData;
            OH_AudioStreamBuilder_SetRendererCallback(rendererBuilder, rendererCallbacks, nullptr);
            // 3. create OH_AudioRenderer
            OH_AudioStreamBuilder_GenerateRenderer(rendererBuilder, &audioRenderer);
            g_readEnd = true;
        }
    }
    return 0;
}

Step3:开始播放,暂停播放,停止播放

{
    g_readEnd = false;
    // start
    OH_AudioRenderer_Start(audioRenderer);
    return nullptr;
}

static napi_value AudioRendererPause(napi_env env, napi_callback_info info)
{
    g_readEnd = false;
    OH_AudioRenderer_Pause(audioRenderer);
    return nullptr;
}

static napi_value AudioRendererStop(napi_env env, napi_callback_info info)
{
    g_readEnd = false;
    OH_AudioRenderer_Stop(audioRenderer);
    return nullptr;
}

Step4:释放构造器

{
    if (audioRenderer) {
        OH_AudioRenderer_Release(audioRenderer);
        OH_AudioStreamBuilder_Destroy(rendererBuilder);
        audioRenderer = nullptr;
        rendererBuilder = nullptr;
    }
    return nullptr;
}

步骤四:OHAudio监听音频打断实现

int32_t MyOnWriteData(OH_AudioRenderer *renderer, void *userData, void *buffer, int32_t length) {
    // 将待播放的数据,按length长度写入buffer
    if (g_file == nullptr) {
        LOGI("MyOnWriteData length fopen");
        g_file = fopen(g_filePath.c_str(), "r");
    } else if (feof(g_file)) {
        LOGI("MyOnWriteData length fseek");
        fseek(g_file, 0, SEEK_SET);
    }
    LOGI("MyOnWriteData length %d", length);
    size_t readCount = fread(buffer, length, 1, g_file);
    return 0;
}

// 自定义音频流事件函数
int32_t MyOnStreamEvent(OH_AudioRenderer *renderer, void *userData, OH_AudioStream_Event event) {
    // 根据event表示的音频流事件信息,更新播放器状态和界面
    LOGI("MyOnStreamEvent");
    return 0;
}

// 自定义音频中断事件函数
int32_t MyOnInterruptEvent(OH_AudioRenderer *renderer, void *userData, OH_AudioInterrupt_ForceType type,
                          OH_AudioInterrupt_Hint hint) {
    // 根据type和hint表示的音频中断信息,更新播放器状态和界面
    LOGI("MyOnInterruptEvent ForceType: %d, Hint: %d", type, hint);
    switch (hint) {
    case AUDIOSTREAM_INTERRUPT_HINT_NONE:
        break;
    case AUDIOSTREAM_INTERRUPT_HINT_RESUME:
        OH_AudioRenderer_Start(audioRenderer);
        break;
    case AUDIOSTREAM_INTERRUPT_HINT_PAUSE:
        OH_AudioRenderer_Pause(audioRenderer);
        break;
    case AUDIOSTREAM_INTERRUPT_HINT_STOP:
        OH_AudioRenderer_Stop(audioRenderer);
        break;
    case AUDIOSTREAM_INTERRUPT_HINT_DUCK:
        break;
    case AUDIOSTREAM_INTERRUPT_HINT_UNDUCK:
        break;
    default:
        LOGI("无法识别中断事件");
    }
    return 0;
}

// 自定义异常回调函数
int32_t MyOnError(OH_AudioRenderer *renderer, void *userData, OH_AudioStream_Result error) {
    // 根据error表示的音频异常信息,做出相应的处理
    LOGI("MyOnError");
    return 0;
}

更多关于HarmonyOS 鸿蒙Next oh_audio_play 开发没返回的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


针对帖子标题“HarmonyOS 鸿蒙Next oh_audio_play 开发没返回”的问题,以下是专业且直接的回答:

在HarmonyOS鸿蒙系统中,若oh_audio_play函数在调用后没有返回预期结果,可能的原因包括但不限于:

  1. 资源占用:系统或其他应用可能已占用音频资源,导致播放请求无法立即执行。
  2. 参数错误:检查传递给oh_audio_play的参数是否正确,包括音频文件路径、播放模式等。
  3. 权限问题:应用可能未获得播放音频的必要权限,导致播放失败。
  4. 系统状态:系统处于某些特定状态(如低功耗模式)时,可能限制后台音频播放。
  5. API异常:oh_audio_play API本身可能存在异常或限制,导致无法正常工作。

为诊断问题,可以尝试以下步骤:

  • 确认音频文件路径和格式是否正确。
  • 检查应用是否已获得音频播放权限。
  • 尝试在系统不同状态下播放音频,观察是否存在特定状态限制。
  • 查看系统日志,了解是否有关于音频播放的错误信息。

如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html

回到顶部