Skip to content

Conversation

@shshshdy
Copy link

No description provided.

@WCKYWCKF
Copy link
Collaborator

这些绑定经过真机测试码?据我们所知,Marshall.GetFunctionPointerForDelegate是无法在除鸿蒙PC和模拟器以外的鸿蒙系统中工作的

Copy link
Collaborator

@WCKYWCKF WCKYWCKF left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

存疑

@shshshdy
Copy link
Author

我真机测试的,可以


namespace OpenHarmony.NDK.Bindings.Media_Kit.AVPlayer
{
public unsafe delegate void OH_AVPlayerOnInfo(OH_AVPlayer* player, AVPlayerOnInfoType type, int extra);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

你有使用这个回调码?如何使用的?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

调用的另外一个,这个可以

public unsafe delegate void OH_AVPlayerOnErrorCallback(OH_AVPlayer* player, int errorCode, nint errorMsg, void* userData);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那个应该是api12开始废弃的接口没测试

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

除了鸿蒙PC和模拟器外鸿蒙不允许为匿名内存申请执行权限。你是怎么使用的这些delegate 声明的回调?

Copy link
Author

@shshshdy shshshdy May 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

private void PlaySound(string url)
{
    _player = OH_AVPlayer.OH_AVPlayer_Create();
    OH_AVPlayerOnErrorCallback callback = (p, error, msg, userdata) =>
    {
        Logger.LogError("music", "error:" + error + "  " + Marshal.PtrToStringAnsi(msg));
    };
    var back = Marshal.GetFunctionPointerForDelegate(callback);
    var code = OH_AVPlayer.OH_AVPlayer_SetOnErrorCallback(_player, back, IntPtr.Zero);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "errorCallBack " + code);
    }
    OH_AVPlayerOnInfoCallback infoback = (p, type, body, userdata) =>
    {
        Logger.Log("music", "type:" + type);
    };
    var infoBack = Marshal.GetFunctionPointerForDelegate(infoback);
    code = OH_AVPlayer.OH_AVPlayer_SetOnInfoCallback(_player, infoBack, IntPtr.Zero);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "infoCallBack " + code);
    }
    code = OH_AVPlayer.OH_AVPlayer_SetURLSource(_player, url);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "url " + code);
    }
    code = OH_AVPlayer.OH_AVPlayer_SetAudioRendererInfo(_player, OH_AudioStream_Usage.AUDIOSTREAM_USAGE_GAME);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "render " + code);
    }
    code = OH_AVPlayer.OH_AVPlayer_SetLooping(_player, OpenHarmony.NDK.Bindings.OH_Bool.True);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "loop " + code);
    }

    code = OH_AVPlayer.OH_AVPlayer_Prepare(_player);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "prepare " + code);
    }
    int duration = 0;
    code = OH_AVPlayer.OH_AVPlayer_GetDuration(_player, &duration);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "GetDuration " + code);
    }
    else
    {
        Logger.Log("music", "duration:" + duration);
    }
    code = OH_AVPlayer.OH_AVPlayer_Play(_player);
    if (code != OH_AVErrCode.AV_ERR_OK)
    {
        Logger.LogError("music", "play " + code);
    }
}

这么写的

@shshshdy
Copy link
Author

image
真机日志

@WCKYWCKF
Copy link
Collaborator

不建议将这部分代码提交到仓库中,因为我们将在Marshall问题解决之后通过工具重新生成所有NDK能力的C API绑定。建议将这部分代码暂时的库嵌入到项目中,以免被中断性变更影响项目开发。你可以订阅组织的Discussions获得版本发布通知,届时再依赖OpenHarmony.NDK.Bindings也不迟。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants