1. Welcome to TechPowerUp Forums, Guest! Please check out our forum guidelines for info related to our community.

Playing audio files using C#

Discussion in 'Programming & Webmastering' started by MrSeanKon, May 13, 2008.

  1. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    First of all go here and download the source code.
    :respect: Kreij help!!!
    I would like to update my cardgames that's the main reason.
    To be more specific I am thinking to create a professional Preference edition.
    Anyway the attachment file shows clearly what I want to do.
    If I add a *.wav file (like a recourse so it is hidden) I don't know how can I play it!
    Last edited: May 23, 2008
  2. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,553 (2.81/day)
    Thanks Received:
    1,383
    system.media.soundplayer.play()?
  3. DanTheBanjoman Señor Moderator

    Joined:
    May 20, 2004
    Messages:
    10,553 (2.81/day)
    Thanks Received:
    1,383
    Forget the question mark, I tested it:)

    In vb it's:
    Of course chimes is the windows chimes.wav, I added it as resource.

    Translate it to C yourself.
    MrSeanKon says thanks.
  4. Kreij

    Kreij Senior Monkey Moderator Staff Member

    Joined:
    Feb 6, 2007
    Messages:
    13,881 (5.02/day)
    Thanks Received:
    5,615
    Location:
    Cheeseland (Wisconsin, USA)
    Why are you calling unmanaged code (winmm.dll) from a managed C# application (causing trust problems and in general making your life much more difficult? I would use either the DirectX audio API or the XNA XACT libraries (or the System.Media namespace as Dan suggested).

    That being said ...
    Since you are writing a game you want to use SND_ASYNC so that the program does not stop execution while the sound is playing. SND_SYNC halts execution and does not return until the sound has finished playing.

    If you are using a file, you should be able to ditch the "new Sytem.ItrPtr()" and use something like ...
    Code:
    PlaySound(Application.StartupPath + [color=red]"Audio.wav"[/color], [b]0[/b], [color=teal]PlaySoundFlags[/color].SND_ASYNC)
    As for using a resource, after you add the Lazer.Wav file to the resource directory, go into it's properties and try changing the "Build Action" to "Embedded Resource".
    I am not sure if that will work, as I cannot test it at the moment.

    On another note, I think that this whole thing will break if you try to run it on Vista, which changed just about everything in the way audio is handled.
    MrSeanKon says thanks.
  5. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    :D Cos I searched in Visual Studio 2008 help files and just I copied a part of a code.
    Beside of them thanks :toast: I will go back to home later to check your ideas.
  6. MrSeanKon

    MrSeanKon New Member

    Joined:
    Nov 14, 2006
    Messages:
    267 (0.09/day)
    Thanks Received:
    69
    Location:
    Athens in love with Anna :)
    Back:

    Code:
    try
    {
       System.Media.SoundPlayer jh=new SoundPlayer(global::Preference.Properties.Resources.chimes);
       jh.Play();
    }
    catch
    {
    }
    
    F_CK this code crashes sometimes (OK rare) and does not cause exception!!!

    For this alternative solution I am not sure if it crashes cos I did not test it much (not enough free time):
    Code:
    Microsoft.VisualBasic.Devices.Audio jh=new Audio();
    jh.Play(global::Preference.Properties.Resources.chimes,Microsoft.VisualBasic.AudioPlayMode.WaitToComplete);
    
    Moreover the second code can add options (for the first not).
    Of course we must add a new reference for Microsoft.VisualBasic.Devices in C#.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guest)

Share This Page