Solved

When has a sound become to the end of playing?

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now