Delphi multimedia design-tmediaplayer component (5)

Source: Internet
Author: User
MIDI audio (MIDI audio)

There is not much to say about MIDI audio. All you need to do is set the filename attribute of the mediaplayer to a MIDI file and call the play method. The extensions of MIDI files include. Mid and. RMI.

An example of playing MIDI audio is as follows:

Procedure tform1.btn9click (Sender: tobject); begin if pos ('play' 'crayon shin' 'midi Music', btn9.caption)> 0 then begin mpplayer. wait: = false; mpplayer. filename: = 'lbxx. mid '; mpplayer. open; mpplayer. play; btn9.caption: = 'stopped'; end else if pos ('stopped', btn9.caption)> 0 then begin mpplayer. stop; mpplayer. close; btn9.caption: = 'play' & apos;

The running effect is as follows:

Note

If you do not want to play a simple MIDI file, you must learn some underlying midiinxxx and midioutxxx functions. These functions are available in mmedia. the HLP file indicates that the file is in the c: \ Program Files (x86) \ common files \ Borland shared \ mshelp directory (the path under win7 64-bit, similar to other systems ).

Many early sound cards do not allow simultaneous playback of two waveform files. Likewise, they do not allow simultaneous playback of more than one MIDI file. However, most of them play both waveform files and MIDI files. You may have noticed this effect in the game, where both music track and sound effects are played ). You can use two mediaplayer components to play both the waveform file and the MIDI file. Alternatively, you can use the mediaplayer component for the MIDI file, and use the playsound function for the waveform file to be played.

A simple example of playing Midi and wave music at the same time is as follows:

Procedure tform1.btn10click (Sender: tobject); begin if pos ('play Midi and wave audios simultaneously ', btn10.caption)> 0 then begin mpplayer. wait: = false; mpplayer. filename: = 'lbxx. mid '; mpplayer. open; mpplayer. play; playsound('test.wav ', 0, snd_filename or snd_async); btn10.caption: = 'stopped play'; end else if pos ('stopped play', btn10.caption)> 0 then begin mpplayer. stop; mpplayer. close; playsound (0, 0, snd_filename); btn10.caption: = 'play Midi and wave audios simultaneously '; end;

The sample code interface is as follows:

MIDI files are often used as background music for games. If you use a MIDI file in this way, the music will be restarted at the end. Tmediaplayer does not automatically play audio continuously. However, the mediaplayer onnotify event is used to obtain the cyclic effect. First, you must tell mediaplayer to send a notification when something happens. This part is easy:

Mpplayer. Y: = true; {onnotify event triggered when something occurs}

Then, you need to provide the onnotify event processing code. In the event processing code, you need to re-start playing after the Midi playback is successful. The Code is as follows:

Procedure tform1.mpplayernotify (Sender: tobject); begin with mpplayer do begin if (policyvalue = nvsuccessful) and (btn11.caption = 'stopplay') Then {the playback status of the btn11 button is determined, are you sure this button is valid?} begin position: = 0; play; end;

The sample interface is as follows:

First, check whether the policyvalue property contains the nvsuccessful value. If yes, reset the file location to 0 and call the play method to start playing the file again. It is very simple, but there are some points to understand.

First, note that the position attribute is set to 0, which effectively restores the file to the start point. If the autorewind attribute is set to true, you do not need to set the position attribute.

Next, we should note that some actions make the mediaplayer onnotify event called and change the policyvalue value to nvsuccessful.

For example, if a simple Stop command is executed without any exceptions, the Stop command will generate the nvsuccessful value. You may need to set up a status mechanism to enable the onnotify event to be called after the file is played. The playback ends of other media sets are not affected.

The above code passes the test in Delphi 7. Download the sample code: tmediaplayer.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.