本文整理自网络,侵删。

结果:
1.增加ActionList中的Action时,需要跳到Master界面,不能在Android4Phonel界面下。
2.如果不打开权限的话,会提示“该设备不支持停止录音操作”(Record audion改为True)。
3.播放的效果是播放一次就停止了。不是循环的。
实例代码:
unit Unit1;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
FMX.Controls.Presentation, System.Actions, FMX.ActnList, FMX.Media;
const
AUDIO_FILENAME = 'test.mp3'; //录音保存的文件名
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Button4: TButton;
Label1: TLabel;
Label2: TLabel;
ActionList1: TActionList;
acStartRecording: TAction;
acStopRecording: TAction;
acPlay: TAction;
acStop: TAction;
MediaPlayer1: TMediaPlayer;
procedure ActionList1Update(Action: TBasicAction; var Handled: Boolean);
procedure acStartRecordingExecute(Sender: TObject);
procedure acStopRecordingExecute(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure acPlayExecute(Sender: TObject);
procedure acStopExecute(Sender: TObject);
private
{ Private declarations }
public
FMicrophone: TAudioCaptureDevice;
function HasMicrophone: Boolean;
function IsMicrophoneRecording: Boolean;
{ Public declarations }
end;
var
Form1: TForm1;
implementation
uses
system.IOUtils;//需要引入
{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID}
//得到不同平台的录音文件保存路径
function GetAudioFileName(const AFileName: string): string;
begin
{$IFDEF ANDROID}
Result := TPath.GetTempPath + '/' + AFileName;
{$ELSE}
{$IFDEF IOS}
Result := TPath.GetHomePath + '/Documents/' + AFileName;
{$ELSE}
Result := AFileName;
{$ENDIF}
{$ENDIF}
end;
procedure TForm1.acPlayExecute(Sender: TObject);
begin
if IsMicrophoneRecording then //如果在录音,则先停止录音
acStopRecording.Execute;
//以下播放录音文件 AUDIO_FILENAME
MediaPlayer1.FileName := GetAudioFileName(AUDIO_FILENAME);
MediaPlayer1.Play;
end;
procedure TForm1.acStartRecordingExecute(Sender: TObject);
begin
acStop.Execute;//选择停止录音
if HasMicrophone then
begin
//准备将录音保存到文件 'test.mp3'
FMicrophone.FileName := GetAudioFileName(AUDIO_FILENAME);
try
FMicrophone.StartCapture; //开始录音
except
ShowMessage('该设备不支持录音操作。');
end;
end
else
ShowMessage('没有麦克风设备。');
end;
procedure TForm1.acStopExecute(Sender: TObject);
begin
MediaPlayer1.Stop;
end;
procedure TForm1.acStopRecordingExecute(Sender: TObject);
begin
if IsMicrophoneRecording then //如果正在录音
try
FMicrophone.StopCapture; { 停止录音 }
except
ShowMessage('该设备不支持停止录音操作。');
end;
end;
procedure TForm1.ActionList1Update(Action: TBasicAction; var Handled: Boolean);
begin
//判断图片的可见性
case (HasMicrophone and (FMicrophone.State = TCaptureDeviceState.Capturing)) of
True: Label2.Text := '录音';
False: Label2.Text := '停止录音';
end;
//判断 4 个按钮的是否可按下
acStartRecording.Enabled := not IsMicrophoneRecording and HasMicrophone;
acStopRecording.Enabled := IsMicrophoneRecording;
acStop.Enabled := Assigned(MediaPlayer1.Media) and (MediaPlayer1.State =
TMediaState.Playing);
acPlay.Enabled := FileExists(GetAudioFileName(AUDIO_FILENAME)) and
(MediaPlayer1.State <> TMediaState.Playing);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
//初始化录音设备
FMicrophone := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;
end;
//判断是否有麦克风
function TForm1.HasMicrophone: Boolean;
begin
Result := Assigned(FMicrophone);
end;
//判断是否在录音
function TForm1.IsMicrophoneRecording: Boolean;
begin
Result := HasMicrophone and (FMicrophone.State = TCaptureDeviceState.Capturing);
end;
end.
感谢作者得无私分享:https://www.cnblogs.com/FKdelphi/p/4784842.html
相关阅读 >>
Delphi中如何判断mediaplayer控件所播放的文件是否播放完毕?
更多相关阅读请进入《Delphi》频道 >>