Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Can You PLAY an audio file in C++...!?

Akula77Akula77 Member Posts: 5
Hello, I know it's possible, but I need someone to give me code or info in C++ to play and stop a audio file. I need the C++ program to be able to start an audio file when it wants to, and be able to stop that file at any time it wants to.
Help would be great as it is very hard to find info on the net on this subject.

-Jacob Middleton

Comments

  • edocecrousedocecrous Member Posts: 49
    : Hello, I know it's possible, but I need someone to give me code or info in C++ to play and stop a audio file. I need the C++ program to be able to start an audio file when it wants to, and be able to stop that file at any time it wants to.
    : Help would be great as it is very hard to find info on the net on this subject.
    :
    : -Jacob Middleton
    :
    Hi!
    assuming you program under windows:
    PlaySound("C:\SOUNDS\BELLS.WAV", NULL, SND_SYNC);
    it starts music playback
    PlaySound(NULL,NULL,SND_SYNC);
    it stops sound playback

    The function description:
    The PlaySound function plays a sound specified by the given filename, resource, or system event. (A system event may be associated with a sound in the registry or in the WIN.INI file.)

    BOOL PlaySound(
    LPCSTR pszSound,
    HMODULE hmod,
    DWORD fdwSound
    );

    Parameters
    pszSound
    A string that specifies the sound to play. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.
    Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a filename, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a filename.

    hmod
    Handle of the executable file that contains the resource to be loaded. This parameter must be NULL unless SND_RESOURCE is specified in fdwSound.
    fdwSound
    Flags for playing the sound. The following values are defined:
    SND_APPLICATION
    The sound is played using an application-specific association.
    SND_ALIAS
    The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.
    SND_ALIAS_ID
    The pszSound parameter is a predefined sound identifier.
    SND_ASYNC
    The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
    SND_FILENAME
    The pszSound parameter is a filename.
    SND_LOOP
    The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event.
    SND_MEMORY
    A sound event's file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory.
    SND_NODEFAULT
    No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound.
    SND_NOSTOP
    The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.
    If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound.

    SND_NOWAIT
    If the driver is busy, return immediately without playing the sound.
    SND_PURGE
    Sounds are to be stopped for the calling task. If pszSound is not NULL, all instances of the specified sound are stopped. If pszSound is NULL, all sounds that are playing on behalf of the calling task are stopped.
    You must also specify the instance handle to stop SND_RESOURCE events.

    SND_RESOURCE
    The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource.
    SND_SYNC
    Synchronous playback of a sound event. PlaySound returns after the sound event completes.
    Return Values
    Returns TRUE if successful or FALSE otherwise.


  • Akula77Akula77 Member Posts: 5
    Great! That's EXACTIALY what I wanted.
    I will also need to mix up to 4 audio files and play them at the same time and stop them INDIVIDUALLY... I will read the 'Mixing in Windows' artical but I don't know if that will tell me what I need to know. Any help would be great as this is the only place that will answer my quections on the web.

    Also, can you access sounds using PlaySound in under 110 seconds? I will need to eventually.

    Thanks for all your help, this is great!

    -Jacob
  • JMan69JMan69 Member Posts: 5
    : Great! That's EXACTIALY what I wanted.
    : I will also need to mix up to 4 audio files and play them at the same time and stop them INDIVIDUALLY... I will read the 'Mixing in Windows' artical but I don't know if that will tell me what I need to know. Any help would be great as this is the only place that will answer my quections on the web.
    :
    : Also, can you access sounds using PlaySound in under 110 seconds? I will need to eventually.
    :
    : Thanks for all your help, this is great!
    :
    : -Jacob
    :


    I would suggest learning how to use DirectX (DirectSound). A little complex at first, but if you've used windows API functions before you should be able to get through it. Makes playing multiple sounds at the same time easier in my experience.
  • ctimactima Member Posts: 3
    Hi,

    I found your thread very informative. I have a question I felt you will be able to answer.

    Summary: I am trying to take a wave file or voice being recorded via microphone and convert it to binary, save it to a text file, parse that text file and play bits and pieces of it.

    Detail: Basically I am trying to find a code in C/C++ that will be able to recognize certain patterns in the voice, save it to memory and play it at a certain time. I want it done by using C to convert a wave file to binary file and then analize that data and play bits and pieces of it.

    I will extremely appreciate your help or guidence into the right direction.

    Thank you.

    Amit


    : : Hello, I know it's possible, but I need someone to give me code or info in C++ to play and stop a audio file. I need the C++ program to be able to start an audio file when it wants to, and be able to stop that file at any time it wants to.
    : : Help would be great as it is very hard to find info on the net on this subject.
    : :
    : : -Jacob Middleton
    : :
    : Hi!
    : assuming you program under windows:
    : PlaySound("C:\SOUNDS\BELLS.WAV", NULL, SND_SYNC);
    : it starts music playback
    : PlaySound(NULL,NULL,SND_SYNC);
    : it stops sound playback
    :
    : The function description:
    : The PlaySound function plays a sound specified by the given filename, resource, or system event. (A system event may be associated with a sound in the registry or in the WIN.INI file.)
    :
    : BOOL PlaySound(
    : LPCSTR pszSound,
    : HMODULE hmod,
    : DWORD fdwSound
    : );
    :
    : Parameters
    : pszSound
    : A string that specifies the sound to play. If this parameter is NULL, any currently playing waveform sound is stopped. To stop a non-waveform sound, specify SND_PURGE in the fdwSound parameter.
    : Three flags in fdwSound (SND_ALIAS, SND_FILENAME, and SND_RESOURCE) determine whether the name is interpreted as an alias for a system event, a filename, or a resource identifier. If none of these flags are specified, PlaySound searches the registry or the WIN.INI file for an association with the specified sound name. If an association is found, the sound event is played. If no association is found in the registry, the name is interpreted as a filename.
    :
    : hmod
    : Handle of the executable file that contains the resource to be loaded. This parameter must be NULL unless SND_RESOURCE is specified in fdwSound.
    : fdwSound
    : Flags for playing the sound. The following values are defined:
    : SND_APPLICATION
    : The sound is played using an application-specific association.
    : SND_ALIAS
    : The pszSound parameter is a system-event alias in the registry or the WIN.INI file. Do not use with either SND_FILENAME or SND_RESOURCE.
    : SND_ALIAS_ID
    : The pszSound parameter is a predefined sound identifier.
    : SND_ASYNC
    : The sound is played asynchronously and PlaySound returns immediately after beginning the sound. To terminate an asynchronously played waveform sound, call PlaySound with pszSound set to NULL.
    : SND_FILENAME
    : The pszSound parameter is a filename.
    : SND_LOOP
    : The sound plays repeatedly until PlaySound is called again with the pszSound parameter set to NULL. You must also specify the SND_ASYNC flag to indicate an asynchronous sound event.
    : SND_MEMORY
    : A sound event's file is loaded in RAM. The parameter specified by pszSound must point to an image of a sound in memory.
    : SND_NODEFAULT
    : No default sound event is used. If the sound cannot be found, PlaySound returns silently without playing the default sound.
    : SND_NOSTOP
    : The specified sound event will yield to another sound event that is already playing. If a sound cannot be played because the resource needed to generate that sound is busy playing another sound, the function immediately returns FALSE without playing the requested sound.
    : If this flag is not specified, PlaySound attempts to stop the currently playing sound so that the device can be used to play the new sound.
    :
    : SND_NOWAIT
    : If the driver is busy, return immediately without playing the sound.
    : SND_PURGE
    : Sounds are to be stopped for the calling task. If pszSound is not NULL, all instances of the specified sound are stopped. If pszSound is NULL, all sounds that are playing on behalf of the calling task are stopped.
    : You must also specify the instance handle to stop SND_RESOURCE events.
    :
    : SND_RESOURCE
    : The pszSound parameter is a resource identifier; hmod must identify the instance that contains the resource.
    : SND_SYNC
    : Synchronous playback of a sound event. PlaySound returns after the sound event completes.
    : Return Values
    : Returns TRUE if successful or FALSE otherwise.
    :
    :
    :

  • Shawn CarterShawn Carter Member Posts: 0

    ______ < http://forcoder.org /> free video tutorials and ebooks about [ Visual Basic .NET PHP Visual Basic Objective-C Ruby Swift Go Perl Java C Assembly JavaScript Scratch C# MATLAB PL/SQL Delphi R C++ Python Fortran Awk COBOL Hack Erlang Prolog FoxPro SAS LabVIEW Dart Kotlin Alice Rust Ada Logo F# Bash VBScript Crystal Transact-SQL Scala Lisp Apex D ABAP ML Julia Scheme Clojure Lua ] ____

Sign In or Register to comment.