Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

delphi - How to stop music started via SndPlaySound

At this Website I found how to add music into a .res file and then use it in your delphi .exe. Here is the code for starting the WAVE song.

procedure TForm2.FormActivate(Sender: TObject);
var
   hFind, hRes: THandle;
   Song: PChar;
 begin
  hFind := FindResource(HInstance, 'SonicSong', 'WAVE') ;
  if hFind <> 0 then begin
    hRes:=LoadResource(HInstance, hFind) ;
    if hRes <> 0 then begin
      Song:=LockResource(hRes) ;
      if Assigned(Song) then SndPlaySound(Song, snd_ASync or snd_Memory) ;
      UnlockResource(hRes) ;
    end;
    FreeResource(hFind) ;
  end;
end;

So what I would like to know is how do I stop the music when I want to without closing the application?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Call the sndPlaySound function with the first parameter set to nil, which causes the currently playing sound to stop. As the second parameter use the SND_ASYNC flag, because as the reference says, you must use this flag to terminate an asynchronously played waveform sound, which you are playing in your code:

sndPlaySound(nil, SND_ASYNC);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...