Solved

When has a sound become to the end of playing?

Posted on 1998-04-18
1
200 Views
Last Modified: 2010-04-06
Hello everyone,

I want to use about 10 different sounds in my application. But when a sound has to start playing, I want to check the position of the playing sound at that moment. The next sound must wait playing until the still playing sound is at the end. How can I do this job?

Thanx for helping,

A Beginning Delphi-developper,
Stefan van Roosmalen.
0
Comment
Question by:roosiedb
1 Comment
 
LVL 5

Accepted Solution

by:
inter earned 30 total points
ID: 1334998
Hi
Use TMediaPlayer component to play the sounds. For example lets say we have 3 sound files as 1.wav, 2.wav and 3.wav which we will going to play in a sequence.

1 - Start a new project with an empty form.
2 - Place a button control on form(is automatically named as button1)
3 - Place a Mediaplayer component on form
4 - add the following decleration to the forms object in the IDE:
   public //this line is there already
       procedure Notification(Sender: TObject); //add this line
   end; // this line is there already
5 - Add the procedure just after the {$R *.dfm}

var
  SeqNo : Integer; //this is sequence no to play the sound files in order
const
   MaxSeq = 3; // this is max number of sounds you want to play
   SoundFiles : array[0..MaxSeq-1] of String =
('1.wav','2.wav','3.wav'); // add the file you want to play here

procedure TForm1.Notification(Sender: TObject); //add this line
begin
  if MediaPlayer1.Mode = mpStopped then
  begin
     SeqNo := (SeqNo + 1) mod MaxSeq; //all the sounds play cyclicly
     MediaPlayer1.Close;
     MediaPlayer1.FileName := SoundFiles[SeqNo];
     MediaPlayer1.Open;
     MediaPlayer1.Play;
  end;
end;

6 - DoubleClick on the form(empy area) to opef FormCreate proc and fill as follows;

procedure TFOrm1.FormCreate(Sender:TObject);
begin
  MediaPlayer1.Wait := false; // media player should not hang our application
  SeqNo := 0;
  MediaPlayer1.FileName := SoundFiles[SeqNo];
  MediaPlayer1.OnNotify := Notification;
  MediaPlayer1.Open;
end;

7 - Doubleclick on Button1 to write

Procedure TForm1.button1click(Sender:TObject);
begin
  If MediaPlayer1.Mode = mpPlaying then
    MediaPlayer1.Pause
  else
    MediaPlayer1.Play;
end;

run the application and be sure that the directory of exe contains the 1.wav, 2.wav, and 3.wav sound files. You can extend the list as you wish. You can hide media player control by setting its visible property to FALSE.

If you want to play sounds in case of an event and do not want to overlap the two sounds, before starting to play something just check

  if MediaPlayer1.Mode = mpStopped then
  begin
     //load and play new tune because previous one is stopped(in general at the end of file)
  end;

Regards,
Igor
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question