C# mp3 player

mp3csharp

New member
Joined
Nov 10, 2003
Messages
1
Im trying to make my own C# mp3 playingf library I am using mci, but dont like it - it is bad quality. I dont like using the windows media player or directx stuff either, because they are also bad quality when it works, and you need more stuff installed to run them.

So I look around for open source mp3 playing libraries and the best one out there seems to be mpg123. They have a win32 library call mpglib.dll which plays very well. Unfortunately, its in C, and Im using C#. C# can call functions in C though, so I think it is quite possible. I also have a sample C program that uses the library and it works well.

The C example is at http://www.inet.hr/~zcindori/download/rawmp3_source_dll_version.zip

mpglib.dll is at
http://www.rz.uni-frankfurt.de/~pesch/mpglibdll.zip
or
http://www.lancs.ac.uk/postgrad/ramdhany/code/mp3lib/mpglibdll.zip

Here is my problem and code:

using System;
using System.Runtime.InteropServices;

namespace csmpglibtest
{
/// <summary>
/// Summary description for csMpgLib.
/// </summary>
public class csMpgLib
{
[StructLayout(LayoutKind.Sequential)]
private struct mpstr
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=40000)]
private char[] c;
}

[DllImport("mpglib.dll", EntryPoint="_InitMP3", CharSet=CharSet.Ansi)]
private static extern bool InitMp3(mpstr mp);

[DllImport("mpglib.dll", EntryPoint="_ExitMP3", CharSet=CharSet.Ansi)]
private static extern void ExitMp3(mpstr mp);

[DllImport("mpglib.dll", EntryPoint="_decodeMp3", CharSet=CharSet.Ansi)]
private static extern int DecodeMp3(mpstr mp, char[] inmemory, int inmemsize, char[] outmemory, int outmemsize, out int done);

public csMpgLib()
{
//
// TODO: Add constructor logic here
//
}

public void Test()
{
mpstr something = new mpstr();
InitMp3(something);
//ExitMp3(something);
}
}
}

the error I get when running is:

An unhandled exception of type System.NullReferenceException occurred in csmpglibtest.exe

Additional information: Object reference not set to an instance of an object.

and the line it yellows is InitMp3(something);


what am I doing wrong? are my functions modified incorrectly? do I need to do something to the class before InitMp3?

I think this is a good project because there are really no good C# mp3 playing libraries out there.

Thanks all,
 
Hmm

Im trying to do this too. Looks like the probem is that the struct needs to be MarshalAs UnmanagedType.Struct ???? Not too sure how to do that!

I tried passing just a char array which works but u need to send it by ref as when the C function returns its empty =/

Im wondering if a more siple C wrapper would be better.

Let me know if u have any more luck and Ill do the same

Ta

D
 
Ive sort of got this doing summot, only it seem to work ok upuntil I get a bogus returncode from the convert function, and then it fails =/

This is wot I have

[DllImport("mpglib.dll", EntryPoint="_InitMP3", CharSet=CharSet.Ansi)]
private static extern bool InitMp3([In,Out]char[] data);

[DllImport("mpglib.dll", EntryPoint="_ExitMP3", CharSet=CharSet.Ansi)]
private static extern void ExitMp3([In,Out] char[] data);

[DllImport("mpglib.dll", EntryPoint="_decodeMP3", CharSet=CharSet.Ansi)]
private static extern int DecodeMp3([In,Out] char[] Data, char[] inmemory, int inmemsize,[In,Out] char[] outmemory, int outmemsize, out int done);

private void button1_Click(object sender, System.EventArgs e)
{
char[] Data = new char[40000];

InitMp3(Data);

FileStream fs = new FileStream("Test.Wav", FileMode.OpenOrCreate);

// Create the writer for data.
BinaryWriter w = new BinaryWriter(fs);

fs = new FileStream("Test.mp3", FileMode.Open, FileAccess.Read);

BinaryReader r = new BinaryReader(fs);

while(true)
{
byte[] trBuffer = r.ReadBytes(16384);
int rLen = trBuffer.Length;

if(rLen <= 0)
{
break;
}

char[] rBuffer = Encoding.ASCII.GetChars(trBuffer);

char[] wBuffer = new char[8192];
int Done = 0;

int ret = DecodeMp3(Data,rBuffer,rLen ,wBuffer,8192 ,out Done);

while(ret == 0)
{
w.Write(wBuffer,0,Done);
ret = DecodeMp3(Data,null,0,wBuffer,8192 ,out Done);
};
};

ExitMp3(Data);
r.Close();
w.Close();
}
 
Last edited by a moderator:
Back
Top