Forum > Beginners

Use music (wav) from resource

(1/2) > >>

tonse:
Hi
I need some audio feedback when the user makes (or forgets to make) a choice. Therefor i have soms simple wav sounds. I am testing it with a buttonclick on the form, as the audio needs to be played instantly. When I do this with PlaySound, then there is a delay. One of the answers i read on the forum was to use  resources. And play the sound from the resource. At this point it gets confusing for me. Because there is *.lrs, or *.res. And i can add resources at the project options. I have read wiki's and forums, but things get even more confusing for me.
My questions are:
- What is the best way to approach this?
- How can i edit a resource file (so i can alter or add sounds in a later stage of development)

Thanks

Sieben:
I'd add the wav sounds via project options, because it is the most convenient way and you don't need any additional tools. A file 'MySound.wav' would be added as resource MYSOUND - ie in upper case and without extension. Type should evaluate to RCDATA. You can then access the resource like this:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var Stream: TResourceStream;    ResName: string;    hNd: THandle;  // ...  ResName := UpperCase('MySound');  hNd := FindResource(hInstance,PChar(ResName),RT_RCDATA);  if (hNd <> 0) then  begin    Stream := TResourceStream.Create(hInstance,ResName,RT_RCDATA);    try     // feed stream to player    finally      Stream.Free;    end;  end; 
Using FindResource prior to creating the resource stream prevents an exception being thrown in case the resource is missing. You can alter or add sounds by simply replacing them in project options or by adding new ones.

tonse:
THanks, but i have got one problem left: How do i feed the stream to a player. Both the PlaySound and the sndPlaySound want a name, not a stream.
I can investigate other players, but that seems so "heavy" as i only want to use beeps or other short tones.
By the way: THe Windows.beep function comes with a very long delay that is also interupting the program.
Let me clarify a bit more. I need sounds on different positions of a Progressbar that is progressing with a timer.

wp:
Search the forum for "PlaySound"...

https://forum.lazarus.freepascal.org/index.php/topic,46860.msg439783.html#msg439783

tonse:
the problem is not really solved. I got a sound from the resource on a buttonclick, but there is a real big delay. Almost a second. I need it to be almost instant.
Is there a way to do this. (playing from resource didn't do the trick)

Navigation

[0] Message Index

[#] Next page

Go to full version