May 15, 2024, 09:40:32 AM

News:

IonicWind Snippit Manager 2.xx Released!  Install it on a memory stick and take it with you!  With or without IWBasic!


MCI mp3 play

Started by kryton9, September 02, 2006, 03:40:40 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

kryton9

I found this site explaining how to use MCI to play mp3 files with c#.

I tried converting it, but am getting errors, and I see why, but I am not sure how to fix them.

I need to paste it as the info is in a wordpad file.
This is the c# explanation:
QuoteUsing MCI for MP3 Playback
C#

Using MCI for playing MP3 is very simple. I was so happy when I clicked the play button on my application and I did not get and Exception error. It took me 2min to code the class after looking at this & this. In my free time I will make a sexy UI and add some more functions but for now you could play you self.






private void button1_Click(object sender, System.EventArgs e)
      {
      Ãƒâ€šÃ,  // my error checking lol
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  if(textBox1.Text.Length!=0)
         test.Open(textBox1.Text);
      }

private void button2_Click(object sender, System.EventArgs e)
      {
         test.Play(true);
      }

private void button3_Click(object sender, System.EventArgs e)
      {
         test.Close();
   }   


Class

To use the MCI Functions we have to import the winmm.dll.

[DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);

Then you go to the MSDN look up a command and its party time.


Code

public class Player
{
      
   Ãƒâ€šÃ,  ÂÃ, private string Pcommand;
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ, private bool isOpen;

   [DllImport("winmm.dll")]
private static extern long mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback);
      
public Player()
   {
         
   }

public void Close()
   {
      Pcommand = "close MediaFile";
      mciSendString(Pcommand, null, 0, IntPtr.Zero);
   Ãƒâ€šÃ,  ÂÃ,  ÂÃ,  isOpen=false;
   }


public void Open(string sFileName)
   {
      Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
      mciSendString(Pcommand, null, 0, IntPtr.Zero);
      isOpen = true;
   }


public void Play(bool loop)
   {
      if(isOpen)
      {
         Pcommand = "play MediaFile";
         if (loop)
            Pcommand += " REPEAT";
            mciSendString(Pcommand, null, 0, IntPtr.Zero);
         }
   }


}   

If you have a question
                              http://csharp-home.com/
ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  ÂÃ,  nerdandy@gmail.com

Here is what I got so far:
declare import, mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback),long;


class Player
{
ÂÃ,  ÂÃ, declare player();
ÂÃ,  ÂÃ, declare _player();
ÂÃ,  ÂÃ, declare close();
ÂÃ,  ÂÃ, declare open(string sFileName);
ÂÃ,  ÂÃ, declare Play(int loop);
ÂÃ,  ÂÃ, string Pcommand;
ÂÃ,  ÂÃ, int isOpen;
}

Player::Player()
{

}

Player::_Player()
{

}

Player::Close()
{
Pcommand = "close MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
ÂÃ,  ÂÃ,  isOpen=false;
}


Player::Open(string sFileName)
{
Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
isOpen = true;
}


Player::Play(int loop)
{
if(isOpen)
{
Pcommand = "play MediaFile";
if (loop)
{
Pcommand += " REPEAT";
mciSendString(Pcommand, null, 0, IntPtr.Zero);
}
}
}

global sub main()
{
CWindow w;
w.Create(0,0,640,480,AWS_VISIBLE|AWS_MINIMIZEBOX|AWS_CAPTION|AWS_SYSMENU|AWS_AUTODRAW,0,"MCI mp3 play test",0);
string sFilter="",SFileName="";
sFilter = "mp3 files *.mp3|*.mp3";
sFileName = FileRequest("Load MP3 file",w,1,sFilter);

}

Ionic Wind Support Team

Don't look at C# if you can help it ;)

declare import, mciSendString(string strCommand,StringBuilder strReturn,int iReturnLength, IntPtr hwndCallback),long;

Will obviously not work.  Here is the actual API definition from MSDN

Quote
mciSendString
The mciSendString function sends a command string to an MCI device. The device that the command is sent to is specified in the command string.

MCIERROR mciSendString(

LPCTSTR lpszCommand, 
LPTSTR lpszReturnString, 
UINT cchReturn, 
HANDLE hwndCallback 
); 


Parameters
lpszCommand
Address of a null-terminated string that specifies an MCI command string. For more information about the command strings, see Command Strings.

lpszReturnString
Address of a buffer that receives return information. If no return information is needed, this parameter can be NULL.

cchReturn
Size, in characters, of the return buffer specified by the lpszReturnString parameter.

hwndCallback
Handle of a callback window if the "notify" flag was specified in the command string.
Return Values
Returns zero if successful or an error otherwise. The low-order word of the returned doubleword value contains the error return value. If the error is device-specific, the high-order word of the return value is the driver identifier; otherwise, the high-order word is zero. For a list of possible error values, see Constants: MCIERR Return Values.

To retrieve a text description of mciSendString return values, pass the return value to the mciGetErrorString function.

QuickInfo
  Windows NT: Use version 3.1 and later. Implemented as ANSI and Unicode functions.
  Windows: Use Windows 95 and later. Implemented only as an ANSI function.
  Header: Declared in mmsystem.h.
  Import Library: Link with winmm.lib.

See Also
MCI Overview, MCI Functions, mciGetErrorString

So the conversion based on the information presented is this:

#1 the import exists as two functions, not just 1.  Because there is ANSI and Unicode versions.  We'll stick with ANSI

declare import, mciSendStringA....

The parameters are listed as:

LPCTSTR lpszCommand, 
LPTSTR lpszReturnString, 
UINT cchReturn, 
HANDLE hwndCallback 


The first one is just a string, the second one is a pointer to a buffer, and the third and forth are UNIT's.  So here you go:


declare import, mciSendStringA(string lpszCommand, string *lpszReturnString, UINT cchReturn, UINT hwndCallback), int;


Now as for sending the commands.....

mciSendString(Pcommand, null, 0, IntPtr.Zero);

Obviously you'll get an error since Aurora doesn't know what 'IntPtr.Zero' is.  So they all need to be:

mciSendStringA(Pcommand, null, 0, 0);

And finally don't use variables if they haven't been initialized.  You have a class constructor, thats what it is for afterall.


Player::Player()
{
    isOpen = FALSE;
    pCommand = "";
}



Using the information above you should be able to modify your code to get it to compile.  Post when you get to the next problem ;)
Ionic Wind Support Team

kryton9

Thanks Paul, cooking dinner now, then going to play with friends online and then have a great night working on it. Now my mind will be on this till I can get to it. Thanks for the help!!

kryton9

September 02, 2006, 10:44:06 PM #3 Last Edit: September 02, 2006, 10:48:33 PM by kryton9
I got it working now. It plays lots of media types, so it is neat. I didn't add extra stuff yet. I was playing around with it and you can do lots with it. I just wanted to get the minimum so the code was not a mess and while I figure what all I want to put for the class.

The big question is this: If I wanted to use this for games, that is load various background music, sound effects ... etc. Do I just make more instances of my class for each song and sound file?

The minimal code is attached.
Just run it, select the media file you want to play and it will play it. Just close the window for now to close and stop the player. I didn't add alot to it yet, as I didn't want to make the code look scary in case anyone else has been waiting to have something like this to study.

declare import, mciSendStringA(string lpszCommand,String *lpszReturnString,UINT cchReturn, UINT hwndCallback),int;
class MyWindow:CWindow{declare virtual OnClose() {Destroy();}}

class CPlayer
{
   declare player();
   declare _player();
   declare close();
   declare open(string sFileName);
   declare Play(int options);
   string Pcommand;
   int isOpen;
}


global sub main()
{
MyWindow w;
CPlayer p;
w.Create(0,0,640,100,AWS_VISIBLE|AWS_MINIMIZEBOX|AWS_CAPTION|AWS_SYSMENU|AWS_AUTODRAW,0,"MCI Class Development and Test",0);
string sFilter="",SFileName="";
sFilter = "*.mp3,*.wav,*.mid,*.avi,*.wmv,*.mpg,*.asf|*.mp3;*.wav;*.mid;*.avi;*.wmv;*.mpg;*.asf";
sFileName = FileRequest("Load Media File",w,1,sFilter);
p.Open(sFileName);
p.Play(1);
do
{
w.WriteText(4,4, "Playing: "+Right$(sFileName,50));
w.WriteText(4,24, "For now, just close the window to stop the player, thanks");
wait();
} until !w.IsValid();

}

CPlayer::Player()
{
isOpen = FALSE;
pCommand = "";
}

CPlayer::_Player()
{

}

CPlayer::Close()
{
Pcommand = "close MediaFile";
mciSendStringA(Pcommand, null, 0, 0);
isOpen=false;
}


CPlayer::Open(string sFileName)
{
Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendStringA(Pcommand, null, 0, 0);
isOpen = true;
}


CPlayer::Play(int options)
{
if(isOpen)
{
Pcommand = "play MediaFile";
if (options)
{
Pcommand += " NOTIFY";
mciSendStringA(Pcommand, null, 0, 0);
}
}
}

ExMember001

For Game Sounds there is Audiere DLL that has been ported to Ibasic recently.
Maybe someone can translate that to Aurora ;)
Play mp3,ogg,flac ect...

http://audiere.sourceforge.net/

J B Wood (Zumwalt)

Hey, I screwed something up and I am clueless at the moment:

SRC file:

declare import, mciSendStringA(string lpszCommand,String *lpszReturnString,UINT cchReturn, UINT hwndCallback),int;
#include "Player.inc"

CPlayer::Player()
{
isOpen = FALSE;
pCommand = "";
}

CPlayer::_Player()
{

}

CPlayer::Close()
{
Pcommand = "close MediaFile";
mciSendStringA(Pcommand, null, 0, 0);
isOpen=false;
}


CPlayer::Open(string sFileName)
{
Pcommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendStringA(Pcommand, null, 0, 0);
isOpen = true;
}


CPlayer::Play(int options)
{
if(isOpen)
{
Pcommand = "play MediaFile";
if (options)
{
Pcommand += " NOTIFY";
mciSendStringA(Pcommand, null, 0, 0);
}
}
}



INC file:

class CPlayer
{
   declare player();
   declare _player();
   declare close();
   declare open(string sFileName);
   declare Play(int options);
   string Pcommand;
   int isOpen;
}



Errors:

Linking...
Aurora Linker v1.0 Copyright @2005,2006 Ionic Wind Software
Unresolved external CPlayer@open
Error: Unresolved external CPlayer@open
Error: Unresolved external CPlayer@play
Error(s) in linking Aurora3D.exe











Any clue?

Ionic Wind Support Team

Did you remember to add player.src to your project?

Ionic Wind Support Team

kryton9

September 03, 2006, 01:52:19 PM #7 Last Edit: September 03, 2006, 02:11:36 PM by kryton9
The finished version for now.
When you open the file, it doesn't play. You need to tell it to play. Enjoy

declare import, mciSendStringA(string lpszCommand,String *lpszReturnString,UINT cchReturn, UINT hwndCallback),int;
class MyWindow:CWindow{declare virtual OnClose() {Destroy();}}

class CPlayer
{
   declare CPlayer();
   declare _CPlayer();
   declare Close();
   declare Open(pointer window);
   declare Stop();
   declare Pause();
   declare Play(int options);
   string sCommand;
   string sFilter;
   string SFileName;
   int isOpen;
}


global sub main()
{
MyWindow w;
CPlayer p;
w.Create(0,0,640,100,AWS_VISIBLE|AWS_MINIMIZEBOX|AWS_CAPTION|AWS_SYSMENU|AWS_AUTODRAW,0,"MCI Class Development and Test",0);
do
{
if GetKeyState(0x4F) p.Open(w);
if GetKeyState(0x50) p.Play(1);
if GetKeyState(0x4C) p.Play(2);
if GetKeyState(0x53) p.Stop();
if GetKeyState(0x20) p.Pause();

w.WriteText(4,4, "Playing: "+Right$(p.sFileName,50));
w.WriteText(4,24, "(O)penFile   (P)layOnce   (L)oopPlay   (S)top   (Spacebar)Pause");
wait();
} until !w.IsValid();
p.close();

}

CPlayer::CPlayer()
{
isOpen = FALSE;
pCommand = "";
sFilter="";
SFileName="";
}

CPlayer::_CPlayer()
{

}

CPlayer::Close()
{
Pcommand = "close MediaFile";
mciSendStringA(sCommand, null, 0, 0);
isOpen=false;
}


CPlayer::Open(pointer window)
{
if(isOpen)
{
close();
}
sFilter = "*.mp3,*.wav,*.mid,*.avi,*.wmv,*.mpg,*.asf|*.mp3;*.wav;*.mid;*.avi;*.wmv;*.mpg;*.asf";
sFileName = FileRequest("Load Media File",window,1,sFilter);
sCommand = "open \"" + sFileName + "\" type mpegvideo alias MediaFile";
mciSendStringA(sCommand, null, 0, 0);
isOpen = true;
}

CPlayer::Pause()
{
if(isOpen)
{
sCommand = "pause MediaFile";
mciSendStringA(sCommand, null, 0, 0);
}
}

CPlayer::Stop()
{
if(isOpen)
{
sCommand = "stop MediaFile";
mciSendStringA(sCommand, null, 0, 0);
sCommand = "seek MediaFile to start";
mciSendStringA(sCommand, null, 0, 0);
}
}

CPlayer::Play(int options)
{
if(isOpen)
{
sCommand = "play MediaFile";
if (options)
{
select(options)
{
case 1:
sCommand += " NOTIFY";// play once
case 2:
sCommand += " NOTIFY REPEAT";//repeat play
case 3:
sCommand += " NOTIFY FULLSCREEN";// for video files
case 4:
sCommand += " NOTIFY REVERSE";// depends on the media
case 5:
sCommand += " NOTIFY SLOW";// depends on the media
case 6:
sCommand += " NOTIFY FAST";// depends on the media
case 7:
sCommand += " NOTIFY SCAN";// depends on the media
}
mciSendStringA(sCommand, null, 0, 0);
}
}
}

kryton9

Quote from: krypt on September 02, 2006, 11:02:55 PM
For Game Sounds there is Audiere DLL that has been ported to Ibasic recently.
Maybe someone can translate that to Aurora ;)
Play mp3,ogg,flac ect...

http://audiere.sourceforge.net/


Thanks Krypt, looks like they support a lot of formats and open source too.

J B Wood (Zumwalt)

Yea Paul I did, what it ended up being was  syntax..
Player vs player.

Ionic Wind Support Team

Yes the class constructor and destructor are case sensetive.
Ionic Wind Support Team