Solved

When has a sound become to the end of playing?

Posted on 1998-04-18
1
202 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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