使用EncodeString/DecodeString 傳輸wav 檔案,將檔案保存到本地后播放,在這其中遇到一個奇怪的問題是:
有些wav檔案可以完整的保存到本地,而有些保存到本地的檔案與原wav檔案相差1kb,導致無法播放這些檔案,不知道這是為什么,麻煩大家給以解答~,謝謝大家~
服務器端介面實作代碼如下:
{ Invokable implementation File for TWavServer which implements IWavServer }
unit WavServerImpl;
interface
uses Soap.InvokeRegistry, System.Types, Soap.XSBuiltIns, WavServerIntf,
System.SysUtils,System.Classes,Vcl.Forms,Soap.EncdDecd;
type
{ TWavServer }
TWavServer = class(TInvokableClass, IWavServer)
public
function GetWavList :string; stdcall;
function GetWavFile(const szfilename : string): string;stdcall;
end;
implementation
{ TWavServer }
function TWavServer.GetWavFile(const szfilename: string): string;
var
ass : TStringStream;
ams : TMemoryStream;
begin
if FileExists(ExtractFilePath(Application.ExeName)+'Wav\' + szfilename) then
begin
ams := TMemoryStream.Create;
ass := TStringStream.Create('');
try
ams.LoadFromFile( ExtractFilePath(Application.ExeName)+'Wav\'+ szfilename);
ams.SaveToStream(ass);
Result := EncodeString(ass.DataString);
finally
ams.Free;
ass.Free;
end;
end;
end;
function TWavServer.GetWavList: string;
var
sr : TSearchRec;
lst : TstringList;
begin
lst := TStringList.Create;
try
if FindFirst(ExtractFilePath(Application.ExeName) +'wav\'+ '*.wav',faAnyFile and not faDirectory,sr) = 0 then
begin
try
repeat
lst.Add(sr.Name);
until (FindNext(sr) <> 0);
finally
FindClose(sr);
end;
end;
Result := lst.CommaText;
finally
lst.Free;
end;
end;
initialization
{ Invokable classes must be registered }
InvRegistry.RegisterInvokableClass(TWavServer);
end.
客戶端呼叫代碼如下:
unit WavClients;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Soap.InvokeRegistry, Vcl.StdCtrls,
Vcl.MPlayer, Soap.Rio, Soap.SOAPHTTPClient;
type
TForm2 = class(TForm)
HTTPRIO1: THTTPRIO;
mp1: TMediaPlayer;
btn1: TButton;
btn2: TButton;
lst1: TListBox;
procedure btn1Click(Sender: TObject);
procedure btn2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form2: TForm2;
implementation
uses
WavServerIntf,Soap.EncdDecd;
{$R *.dfm}
procedure TForm2.btn1Click(Sender: TObject);
var
aintf : IWavServer;
begin
try
aintf := Self.HTTPRIO1 as IWavServer;
lst1.Items.CommaText := aintf.GetWavList;
finally
aintf := nil;
end;
end;
procedure TForm2.btn2Click(Sender: TObject);
var
aintf : IWavServer;
ass : TStringStream;
ams : TMemoryStream;
bitStr : string;
begin
aintf := Self.HTTPRIO1 as IWavServer;
try
bitStr := aintf.GetWavFile(lst1.Items.Strings[lst1.ItemIndex]);
ass := TStringStream.Create(DecodeString(bitStr));
ams := TMemoryStream.Create;
try
ams.LoadFromStream(ass);
ams.SaveToFile(lst1.Items.Strings[lst1.ItemIndex]);
with mp1 do
begin
Close;
mp1.FileName := lst1.Items.Strings[lst1.ItemIndex];
Open;
Play;
end;
finally
ass.Free;
ams.Free;
end;
finally
aintf := nil;
end;
end;
end.
uj5u.com熱心網友回復:
把加密和解密去掉試試?轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/117376.html
標籤:網絡通信/分布式開發
