(lib)mpg123 函数 mpg123_index 在 C# 中的使用

Sve*_*ven 5 c c# mono pointers

我在我的项目中使用 mpg123 来解码 mp3。我与开发人员(Thomas Orgis)讨论了搜索时的性能问题,他有一个好主意:扫描文件,然后将扫描的索引设置为播放句柄。所以我想在我的 C# 项目中使用 mpg123_index 和 mpg123_set_index 并在 libmpg123 周围使用包装器。

包装器不是自己写的,我也联系了开发人员,但他似乎不可用。所以也许有人了解指针编程,我自己也做了一些事情,但目前我只在调用方法时得到 AccessViolationException。

这里有一些代码:包装规范(也许这是错误的?!):

[DllImport(Mpg123Dll)]public static extern int mpg123_index(IntPtr mh, IntPtr offsets, IntPtr step, IntPtr fill);
Run Code Online (Sandbox Code Playgroud)

网站规格 (C):

https://www.mpg123.de/api/group__mpg123__seek.shtml#gae1d174ac632ec72df7dead94c04865fb

MPG123_EXPORT int mpg123_index ( mpg123_handle * mh, off_t ** offsets, off_t * step, size_t * fill )

授予访问为查找而管理的帧索引表的权限。要求您不要修改值...使用 mpg123_set_index 设置搜索索引

参数 mh handle offsets 指向索引数组的指针 step 1 index byte offset 推进这么多 MPEG 帧填充记录的索引偏移量;数组的大小

成功返回 MPG123_OK

我尝试在 C# 中使用该函数:

IntPtr pIndex = IntPtr.Zero;
IntPtr pFill = IntPtr.Zero;
IntPtr pStep = IntPtr.Zero;
if (MPGImport.mpg123_index(this.mp3Handle,pIndex,pStep,pFill) != (int)MPGImport.mpg123_errors.MPG123_OK)) 
{
    log.error("Failed!");
}
Run Code Online (Sandbox Code Playgroud)

那么,有没有人知道如何在 C# 端正确使用该函数?我刚刚从 mpg123 的作者那里得到了信息,pIndex 将是一个指向内部索引的指针。所以这个指针会被这个函数改变。

谢谢你的帮助。

真诚的斯文

编辑:现在以下代码几乎有效:

[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_index(IntPtr mh, long **offsets, long *step, ulong *fill);
[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_set_index(IntPtr mh, long* offsets, long step, ulong fill);

public static Boolean CopyIndex(IntPtr _sourceHandle,IntPtr _destinationHandle)
{
    Boolean copiedIndex = false;
    unsafe
    {
        long* offsets = null; 
        long step = 0;
        ulong fill = 0;
        int result = MPGImport.mpg123_index(_sourceHandle, &offsets, &step, &fill);
        if (result == (int)MPGImport.mpg123_errors.MPG123_OK)
        {
            result = MPGImport.mpg123_set_index(_destinationHandle, offsets, step, fill);
            if (result == (int)mpg123_errors.MPG123_OK)
            {
                copiedIndex = true;
            }
        }
    }
    return copiedIndex;
}
Run Code Online (Sandbox Code Playgroud)

result = MPGImport.mpg123_set_index(_destinationHandle, offsets, step, fill);结果是-1,这是MPG123_ERR = -1, /**< Generic Error */

Sve*_*ven 2

工作代码:

[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_index(IntPtr mh, long **offsets, long *step, ulong *fill);
[DllImport(Mpg123Dll)]public unsafe static extern int mpg123_set_index(IntPtr mh, long* offsets, long step, ulong fill);

public static Boolean CopyIndex(IntPtr _sourceHandle,IntPtr _destinationHandle)
{
    Boolean copiedIndex = false;
    unsafe
    {
        long* offsets = null; 
        long step = 0;
        ulong fill = 0;
        int result = MPGImport.mpg123_index(_sourceHandle, &offsets, &step, &fill);
        if (result == (int)MPGImport.mpg123_errors.MPG123_OK)
        {
            result = MPGImport.mpg123_set_index(_destinationHandle, offsets, step, fill);
            if (result == (int)mpg123_errors.MPG123_OK)
            {
                copiedIndex = true;
            }
        }
    }
    return copiedIndex;
}
Run Code Online (Sandbox Code Playgroud)

_sourceHandle 和 _destinationHandle 可能不是 IntPtr.Zero,那是我的错误。感谢 Stargateur,非常好的帮助!