您的位置:首页 > 移动开发 > Android开发

Android实例-录音与回放(播放MP3)(XE8+小米2)

2015-09-06 10:53 711 查看


结果:

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.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: