Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#159 How to use the libmpg123.dll to play mp3 file with C#?

1.13.3
closed-invalid
nobody
mpg123 (103)
5
2012-05-01
2011-07-21
yemx21
No

How could I use the libmpg123.dll to play mp3 file with C#?
I use mpg123-1.13.3\ports\MSVC++\2008\libmpg123\ succeed to create a new dll .
I use Wrapper Class to read the mp3 file ,but it output the zero buffer.

[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_init();
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void mpg123_exit();
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl,CharSet = CharSet.Ansi)]
public static extern IntPtr mpg123_new(string decoder, IntPtr error);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern void mpg123_delete(IntPtr mh);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
public static extern int mpg123_open(IntPtr mh, string path);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_read(IntPtr mh, byte[] outmemory, int outmemsize, out IntPtr done);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_outblock(IntPtr mh);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_seek(IntPtr mh, int sampleoff, int whence);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_id3(IntPtr mh, out IntPtr v1, out IntPtr v2);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_getformat(IntPtr mh, out IntPtr rate, out IntPtr channels, out IntPtr encoding);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_decode(IntPtr mh, IntPtr inmemory, int inmemsize, IntPtr outmemory, int outmemsize, IntPtr done);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_format_none(IntPtr mh);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_format_all(IntPtr mh);
[DllImport("MP#.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern int mpg123_format(IntPtr mh, int rate, int channels, int encodings);
public IntPtr rate;
public IntPtr channels;
public IntPtr encoding;

{
mpg123_init();
IntPtr handle_mpg = new IntPtr();
IntPtr errPtr = new IntPtr();
string s = @"D:\1.mp3";
handle_mpg = mpg123_new(null, errPtr);
mpg123_open(handle_mpg, s);
mpg123_getformat(handle_mpg, out rate, out channels, out encoding);
int _rate = rate.ToInt32();
int _channels = channels.ToInt32();
int _encoding = encoding.ToInt32();
IntPtr id3v1 = new IntPtr();
IntPtr id3v2 = new IntPtr();
mpg123_id3(handle_mpg, out id3v1, out id3v2);
mpg123_id3v1 MP3Data = new mpg123_id3v1();
mpg123_id3v2 MP3Data2 = new mpg123_id3v2();
MP3Data = (mpg123_id3v1)Marshal.PtrToStructure(id3v1, typeof(mpg123_id3v1));
MP3Data2 = (mpg123_id3v2)Marshal.PtrToStructure(id3v2, typeof(mpg123_id3v2));
mpg123_format_none(handle_mpg);
mpg123_format(handle_mpg, _rate, _channels, _encoding);
int FrameSize = mpg123_outblock(handle_mpg);
byte[] Buffer = new byte[FrameSize];
IntPtr done= new IntPtr();
MemoryStream MS = new MemoryStream();
mpg123_seek(handle_mpg, 0, 0);
while (0 == mpg123_read(handle_mpg, Buffer, FrameSize, out done))
{
MS.Write(Buffer, 0, Buffer.Length);
}
int N=0;
byte[] bb = new byte[1];
for (int i = 0; i < MS.Length; i++)
{
MS.Read(bb, 0, bb.Length);
if (bb[0] != 0) N++;
}
}
the code above i try to get the whole decoded buffers ,and write them to a new memorystream,but finally the memorystream is filled with zero.
So,I want to know how to use the APIs of libmpg123.
Could you give me an example of using the APIs of libmpg123 to read a mp3 file with C#.
Thank you!

My e-mail is 464489717@qq.com

Discussion

  • Thomas Orgis
    Thomas Orgis
    2011-08-24

    Did you perhaps resolve your issues meanwhile? I waited for some of our windows folks to comment here, but perhaps the report slipped by. I'll poke someone.

     
  • Thomas Orgis
    Thomas Orgis
    2012-05-01

    • status: open --> closed-invalid
     
  • Thomas Orgis
    Thomas Orgis
    2012-05-01

    I really hope the question is resolved now, if not, please report to mpg123-users mailing list or IRC for support.