标签:
结果:
1.增加ActionList中的Action时,需要跳到Master界面,不能在Android4Phonel界面下。
2.如果不打开权限的话,会提示“该设备不支持停止录音操作”(Record audion改为True)。
3.播放的效果是播放一次就停止了。不是循环的。
实例代码:
1 unit Unit1; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls, 8 FMX.Controls.Presentation, System.Actions, FMX.ActnList, FMX.Media; 9 10 const 11 AUDIO_FILENAME = ‘test.mp3‘; //录音保存的文件名 12 13 type 14 TForm1 = class(TForm) 15 Button1: TButton; 16 Button2: TButton; 17 Button3: TButton; 18 Button4: TButton; 19 Label1: TLabel; 20 Label2: TLabel; 21 ActionList1: TActionList; 22 acStartRecording: TAction; 23 acStopRecording: TAction; 24 acPlay: TAction; 25 acStop: TAction; 26 MediaPlayer1: TMediaPlayer; 27 procedure ActionList1Update(Action: TBasicAction; var Handled: Boolean); 28 procedure acStartRecordingExecute(Sender: TObject); 29 procedure acStopRecordingExecute(Sender: TObject); 30 procedure FormCreate(Sender: TObject); 31 procedure acPlayExecute(Sender: TObject); 32 procedure acStopExecute(Sender: TObject); 33 private 34 { Private declarations } 35 public 36 FMicrophone: TAudioCaptureDevice; 37 function HasMicrophone: Boolean; 38 function IsMicrophoneRecording: Boolean; 39 { Public declarations } 40 end; 41 42 var 43 Form1: TForm1; 44 45 implementation 46 uses 47 system.IOUtils;//需要引入 48 {$R *.fmx} 49 {$R *.NmXhdpiPh.fmx ANDROID} 50 51 //得到不同平台的录音文件保存路径 52 function GetAudioFileName(const AFileName: string): string; 53 begin 54 {$IFDEF ANDROID} 55 Result := TPath.GetTempPath + ‘/‘ + AFileName; 56 {$ELSE} 57 {$IFDEF IOS} 58 Result := TPath.GetHomePath + ‘/Documents/‘ + AFileName; 59 {$ELSE} 60 Result := AFileName; 61 {$ENDIF} 62 {$ENDIF} 63 end; 64 65 procedure TForm1.acPlayExecute(Sender: TObject); 66 begin 67 if IsMicrophoneRecording then //如果在录音,则先停止录音 68 acStopRecording.Execute; 69 //以下播放录音文件 AUDIO_FILENAME 70 MediaPlayer1.FileName := GetAudioFileName(AUDIO_FILENAME); 71 MediaPlayer1.Play; 72 end; 73 74 procedure TForm1.acStartRecordingExecute(Sender: TObject); 75 begin 76 acStop.Execute;//选择停止录音 77 if HasMicrophone then 78 begin 79 //准备将录音保存到文件 ‘test.mp3‘ 80 FMicrophone.FileName := GetAudioFileName(AUDIO_FILENAME); 81 try 82 FMicrophone.StartCapture; //开始录音 83 except 84 ShowMessage(‘该设备不支持录音操作。‘); 85 end; 86 end 87 else 88 ShowMessage(‘没有麦克风设备。‘); 89 end; 90 91 procedure TForm1.acStopExecute(Sender: TObject); 92 begin 93 MediaPlayer1.Stop; 94 end; 95 96 procedure TForm1.acStopRecordingExecute(Sender: TObject); 97 begin 98 if IsMicrophoneRecording then //如果正在录音 99 try 100 FMicrophone.StopCapture; { 停止录音 } 101 except 102 ShowMessage(‘该设备不支持停止录音操作。‘); 103 end; 104 end; 105 106 procedure TForm1.ActionList1Update(Action: TBasicAction; var Handled: Boolean); 107 begin 108 //判断图片的可见性 109 case (HasMicrophone and (FMicrophone.State = TCaptureDeviceState.Capturing)) of 110 True: Label2.Text := ‘录音‘; 111 False: Label2.Text := ‘停止录音‘; 112 end; 113 //判断 4 个按钮的是否可按下 114 acStartRecording.Enabled := not IsMicrophoneRecording and HasMicrophone; 115 acStopRecording.Enabled := IsMicrophoneRecording; 116 acStop.Enabled := Assigned(MediaPlayer1.Media) and (MediaPlayer1.State = 117 TMediaState.Playing); 118 acPlay.Enabled := FileExists(GetAudioFileName(AUDIO_FILENAME)) and 119 (MediaPlayer1.State <> TMediaState.Playing); 120 end; 121 122 procedure TForm1.FormCreate(Sender: TObject); 123 begin 124 //初始化录音设备 125 FMicrophone := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice; 126 end; 127 128 //判断是否有麦克风 129 function TForm1.HasMicrophone: Boolean; 130 begin 131 Result := Assigned(FMicrophone); 132 end; 133 134 //判断是否在录音 135 function TForm1.IsMicrophoneRecording: Boolean; 136 begin 137 Result := HasMicrophone and (FMicrophone.State = TCaptureDeviceState.Capturing); 138 end; 139 140 end.
Android实例-录音与回放(播放MP3)(XE8+小米2)
标签:
原文地址:http://www.cnblogs.com/FKdelphi/p/4784842.html