在firemonkey中播放声音

编程入门 行业动态 更新时间:2024-10-25 07:28:48
本文介绍了在firemonkey中播放声音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用Firemonkey部署一个带有音频文件的android应用程序. 我大约有30个动物按钮,当用户选择它时,每个按钮都会听到动物的声音.这是我第一个按钮的代码:

I use Firemonkey to deploy an android apps, with audio files on that. I have about 30 animal button, and each of button will heard animals sound when user select it. Here is my code for the first button:

procedure TFMain.buttonLionClick(Sender: TObject); begin MediaPlayer1.FileName := 'D:\lion.mp3'; MediaPlayer1.Play; end;

但是它失败了.如何将声音部署到我的应用程序中?

But it is failed. How can I deploying that sounds in to my apps?

推荐答案

首先,如果要为android构建应用,则需要将.mp3转换为.3gp才能播放. (您可以使用以下网站: video.online-convert/convert- to-3gp ;只需上传您的文件,然后单击转换.)

First of all, if you want to build your app for android you need to convert the .mp3 to .3gp to be able to play it. (You can use this site: video.online-convert/convert-to-3gp ;just upload your file and click convert.)

第二,您需要将文件包含在资源中;转到project>资源和图像,然后选择查看所有文件,然后选择.3gp文件并添加它们.

Secondly, you need to include your files in your resources; go to project>resources and images then select to look at all files and then select your .3gp files and add them.

现在,您可以创建一个程序来播放资源中的声音和音乐: (使用TMediaPlayer)

Now you can make a procedure to play sounds and music from your resources: (using TMediaPlayer)

procedure TForm1.PlayAudio(ResourceID: string); //PLAY MUSIC FROM RESOURCES var ResStream: TResourceStream; TmpFile: string; begin ResStream := TResourceStream.Create(HInstance, ResourceID, RT_RCDATA); try TmpFile := TPath.Combine(TPath.GetDocumentsPath, 'tmp.3gp'); //TPath.Combine(TPath.GetDocumentsPath, 'filename') { Internal } //TPath.Combine(TPath.GetSharedDocumentsPath, 'filename') { External } ResStream.Position := 0; ResStream.SaveToFile(TmpFile); MediaPlayer1.FileName := TmpFile; MediaPlayer1.Play; finally ResStream.Free; end; end;

注意:您需要执行此操作,因为您不能直接从资产中播放文件,而在部署到android时,资源将保存在资源中.

Note: You need to do this like this because you can't play your files directly from your assets where your resources will be saved when deploying to android.

现在可以播放音乐,例如:PlayAudio('Resource_1')

Now to play music just use for example: PlayAudio('Resource_1')

注意:在播放新音频之前,您需要使用以下命令停止上一个音频:

Note: Before playing a new audio, you need to stop the previous one using:

MediaPlayer1.Stop; MediaPlayer1.Clear;

如果您想循环播放音乐,则可以在结束后使用TTimer播放声音.

And if you want to loop your music then you can use TTimer to replay your sound after it ends.

注意:这特别适用于android.在Windows上,请使用.mp3,.wav或类似的名称,并使用TPath.Combine(TPath.GetTempPath, 'tmp.wav')在Windows上获取路径.

Note: this works specifically on android. On Windows use .mp3, .wav or something like that and TPath.Combine(TPath.GetTempPath, 'tmp.wav') to get your path on windows.

更多推荐

在firemonkey中播放声音

本文发布于:2023-07-11 12:08:41,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1094002.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:声音   firemonkey

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!