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

标签 c# c pointers mono

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

wrapper不是自己写的,我也联系了开发者,但是他好像不可用。所以也许有人有指针编程的知识,我自己也做了一些事情,但目前我只在调用方法时得到一个AccessViolationException。

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

[DllImport(Mpg123Dll)]public static extern int mpg123_index(IntPtr mh, IntPtr offsets, IntPtr step, IntPtr fill);

网站规范 (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 )

Give access to the frame index table that is managed for seeking. You are asked not to modify the values... Use mpg123_set_index to set the seek index

Parameters mh handle offsets pointer to the index array step one index byte offset advances this many MPEG frames fill number of recorded index offsets; size of the array

Returns MPG123_OK on success

我尝试在 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!");
}

那么,有没有人知道如何在 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;
}

关于 result = MPGImport.mpg123_set_index(_destinationHandle, offsets, step, fill);结果为-1,即MPG123_ERR = -1, /**< Generic Error */ .

最佳答案

工作代码:

[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;
}

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

关于c# - (lib)mpg123 在 C# 中使用函数 mpg123_index,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49105694/

相关文章:

c - C语言中的复杂指针语句

c++ - 当 const 指针用作函数的参数时

c# - "Safely"存储(在客户端) token 以供重用?

c - 在 tic tac toe 中进行随机移动 - C 语言

c - 使用 fscanf 的段错误 - 未分配内存问题

c - 使用 netfilter hook 时打印 skb 数据会使计算机崩溃

java - 在运行时获取对象的引用数量

c# - Unity中GameObject的引用脚本类

使用 RDP 的 C# 远程桌面应用程序。如何生成证书?

C# - 接口(interface)拆分?