今天开发了第一个andriod程序,测试录音和播放功能。源码是网上抄来的。
代码:
unit Unit2;
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.Media, System.IOUtils;
type
TForm2 = class(TForm)
btnStartRecord: TButton;
btnStopRecord: TButton;
imgOn: TLabel;
btnPlay: TButton;
procedure btnStartRecordClick(Sender: TObject);
procedure btnStopRecordClick(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure btnPlayClick(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
FMicrophone: TAudioCaptureDevice;
implementation
{$R *.fmx}
procedure TForm2.btnPlayClick(Sender: TObject);
var
a: TMediaPlayer;
begin
a := TMediaPlayer.Create(Self);
try
a.FileName := TPath.GetHomePath + '/RecordTest.3GP';
a.Play
finally
FreeAndNil(a);
end;
end;
procedure TForm2.btnStartRecordClick(Sender: TObject);
//开始录音
begin
FMicrophone := TCaptureDeviceManager.Current.DefaultAudioCaptureDevice;
if Assigned(FMicrophone) then
begin
FMicrophone.FileName := TPath.GetHomePath + '/RecordTest.3GP';
try
FMicrophone.StartCapture;
imgOn.Visible := True;
except
on e: Exception do
begin
imgOn.Visible := False;
ShowMessage( '设备不识别或不支持。' + e.Message);
end
end;
end
else
ShowMessage('没有可用的麦克风。');
end;
procedure TForm2.btnStopRecordClick(Sender: TObject);
//停止录音
begin
if Assigned(FMicrophone) then
try
if FMicrophone.State = TCaptureDeviceState.Capturing then
begin
FMicrophone.StopCapture;
imgOn.Visible := False;
end;
except
imgOn.Visible := False;
ShowMessage('设备不识别或不支持');
end;
end;
procedure TForm2.FormCreate(Sender: TObject);
begin
imgOn.Visible := False;
end;
end.
几个问题:
1.发布:project->deployment,点击deploy就可以了。就可以产生一个APK文件,通过QQ传输到手机上,就可以安装运行了,好神奇。
2.运行时提示:setaudiosource failed,网上查了下,发现是权限问题,
在AndroidManifest.xml文件中添加:
<uses-permission
android:name="android.permission.RECORD_AUDIO">
</uses-permission>
3.230上的DELPHI7一直运行有问题,之前也没有去解决,一编译就出错,网上查了以后,删除hkey_current_user\software\embarcadero\bds\15.0就可以了。
4.家里测试的时候,XE7上一直无法显示连接的手机,后来换了一跟连接线就可以了,郁闷。