| 1 |
{ |
| 2 |
Copyright 2003-2018 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{ Simply load and play sound using CastleSoundEngine. } |
| 17 |
program simplest_play_sound; |
| 18 |
|
| 19 |
{$ifdef MSWINDOWS} {$apptype CONSOLE} {$endif} |
| 20 |
|
| 21 |
uses |
| 22 |
{$if defined(FPC) and (not defined(CASTLE_DISABLE_THREADS))} |
| 23 |
{$info Thread support enabled.} |
| 24 |
{$ifdef UNIX} CThreads, {$endif} |
| 25 |
{$endif} |
| 26 |
SysUtils, |
| 27 |
CastleUtils, CastleLog, CastleSoundEngine, CastleParameters, CastleTimeUtils, |
| 28 |
CastleVectors, CastleApplicationProperties; |
| 29 |
|
| 30 |
var |
| 31 |
Url: String; |
| 32 |
Sound: TCastleSound; |
| 33 |
begin |
| 34 |
ApplicationProperties.OnWarning.Add( |
| 35 |
{$ifdef FPC}@{$endif} ApplicationProperties.WriteWarningOnConsole); |
| 36 |
|
| 37 |
// put in log various info about sound loading |
| 38 |
InitializeLog; |
| 39 |
SoundEngine.LogSoundLoading := true; |
| 40 |
|
| 41 |
{ parse params } |
| 42 |
SoundEngine.ParseParameters; |
| 43 |
Parameters.CheckHighAtMost(1); |
| 44 |
if Parameters.High = 1 then |
| 45 |
Url := Parameters[1] |
| 46 |
else |
| 47 |
Url := 'castle-data:/temple-adam-goh.ogg'; |
| 48 |
//'castle-data:/tone.wav'; |
| 49 |
|
| 50 |
{ Sound backend (like OpenAL) will be automatically initialized when needed below. |
| 51 |
Although you could also initialize it explicitly by SoundEngine.ContextOpen, |
| 52 |
check SoundEngine.Information, SoundEngine.IsContextOpenSuccess etc. } |
| 53 |
|
| 54 |
{ Load and play sound, without any spatialization. } |
| 55 |
Sound := TCastleSound.Create(nil); |
| 56 |
try |
| 57 |
Sound.Url := Url; |
| 58 |
Writeln('Sound loaded, duration in seconds: ', Sound.Duration:1:2); |
| 59 |
SoundEngine.Play(Sound); |
| 60 |
|
| 61 |
{ Wait enough time to finish playing. |
| 62 |
In this simple program, we just sleep enough time |
| 63 |
to finish playing the sound + 0.1 second delay. } |
| 64 |
Sleep(Round(Sound.Duration * 1000) + 100); |
| 65 |
|
| 66 |
{ The above approach to wait is not suitable for most real applications, |
| 67 |
where you don't want to call Sleep as it just hangs your process. |
| 68 |
The proper solution is to use TCastlePlayingSound, |
| 69 |
and watch until TCastlePlayingSound.Playing is false, |
| 70 |
or register event on TCastlePlayingSound.OnStop. |
| 71 |
|
| 72 |
Example: |
| 73 |
|
| 74 |
PlayingSound := TCastlePlayingSound.Create(nil); |
| 75 |
PlayingSound.Sound := Sound; |
| 76 |
PlayingSound.FreeOnStop := true; |
| 77 |
SoundEngine.Play(PlayingSound); |
| 78 |
while PlayingSound.Playing do |
| 79 |
begin |
| 80 |
// do anything, like |
| 81 |
// Sleep(10); |
| 82 |
// Appplication.ProcessAllMessages; |
| 83 |
end; |
| 84 |
} |
| 85 |
finally |
| 86 |
FreeAndNil(Sound); |
| 87 |
end; |
| 88 |
end. |