简体   繁体   中英

How to play mp3 content from resource in Delphi?

I have a TResourceStream contains a simple WAV sound.

I wrote that line into the resource.res file: sound WAV "res\\notify.wav"

I have the following method that works with WAV:

Res := TResourceStream.Create(HInstance, 'sound', 'WAV');
Res.Position := 0;
SndPlaySound(res.Memory, SND_MEMORY or SND_ASYNC);
Res.free;

I converted the WAV into MP3 and did the following things:

resource.res file: sound MP3 "res\\notify.MP3"

Changed the play method first line to:

Res := TResourceStream.Create(HInstance, 'sound', 'MP3');

But nothing happens. It doesn't throw any exception, simple no sound heard.

How can I play MP3 as simple as WAV files?

The SndPlaySound API only supports waveform audio. It is not a general purpose multi-media API and as such does not (directly nor easily) support MP3 playback.

To play your audio through this API you would first need to decode the MP3 into the waveform format that the API expects.

( I should note that it appears to be possible to get the SndPlaySound API to play MP3 data by attaching a WAV header to the data. But detailed information about the audio is required in that header and the process is a decidely non-trivial exercise. It is almost certainly harder than using an API more suited to the task from the start. )

Your approach appears to be correct for obtaining a stream containing your MP3 data and with that data in memory there are a number of options available for playing that MP3 audio.

The BASS Audio Library is one such option though this is a commercial library and is not particularly cheap. It is however capable of exactly what you need.

There are numerous alternatives however, some cheaper, some even free which might also do the job though you may find it harder to get assistance with these if they are not as widely used or as well supported.

Even so, you might wish to review some of the alternatives listed on the torry.net directory. Specifically in the Components \\ Effects and Multimedia section of the catalog.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM