Mirror

How to store and retrieve WAV files to / from a Paradox blob field at runtime (Views: 100)


Problem/Question/Abstract:

How can I write and read to/ from a blob field? I want to save OLE objects like Excel sheets or wav sound into a Paradox table rather than to a file.

Answer:

Solve 1:

procedure TForm1.bnSaveClick(Sender: TObject);
begin
  MediaPlayer1.Close;
  tabWaves.Append;
  {tabWavesAudioData is a TBlobField}
  tabWavesAudioData.LoadFromFile(MediaPlayer1.FileName);
  {MediaPlayer1.FileName is path to wave file}
  tabWavesDescription.Value := MediaPlayer1.FileName;
  tabWaves.Post;
end;

procedure TForm1.LoadWaveFromTable;
begin
  MediaPlayer2.Close;
  {Save the Blob to a file}
  tabWavesAudioData.SaveToFile('C:\Temp\WAVETABLE.wav');
  {Load wave into MediaPlayer}
  MediaPlayer2.FileName := 'C:\Temp\WAVETABLE.wav';
  MediaPlayer2.Open;
end;


Solve 2:

uses
  MMSystem;

procedure TFrmPlayWaveFromDB.BtnWaveToDBClick(Sender: TObject);
var
  theBStream: TBlobStream;
begin
  if FlbSelectWave.Filename <> EmptyStr then
  begin
    FMemStream.Seek(0, soFromBeginning);
    FMemStream.LoadFromFile(FlbSelectWave.Filename);
    Table1.Edit;
    theBStream := TBlobStream.Create(Table1ABlobField, bmReadWrite);
    try
      FMemStream.SaveToStream(theBStream);
    finally
      theBStream.Free;
    end;
    Table1.Post;
  end;
end;

procedure TFrmPlayWaveFromDB.FormCreate(Sender: TObject);
begin
  {The MemoryStream must persist while the sound is being played, so it can't be a local variable}
  FMemStream := TMemoryStream.Create;
  Table1.Open;
end;

procedure TFrmPlayWaveFromDB.BtnPlayWaveClick(Sender: TObject);
var
  theBStream: TBlobStream;
begin
  theBStream := TBlobStream.Create(Table1ABlobField, bmRead);
  try
    FMemStream.LoadFromStream(theBStream);
    sndPlaySound(FMemStream.Memory, SND_ASYNC or SND_MEMORY);
  finally
    theBStream.Free;
  end;
end;

procedure TFrmPlayWaveFromDB.FormDestroy(Sender: TObject);
begin
  FMemStream.Free;
  Table1.Close;
end;

procedure TFrmPlayWaveFromDB.FlbSelectWaveChange(Sender: TObject);
begin
  if FlbSelectWave.Filename <> EmptyStr then
    BtnWaveToDB.Enabled := true
  else
    BtnWaveToDB.Enabled := false;
end;


Solve 3:

unit Main;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls, DBCtrls, DB, DBTables, StdCtrls, Mask, Buttons, ComCtrls;

type
  TMainForm = class(TForm)
    tblSounds: TTable;
    dsSounds: TDataSource;
    tblSoundsWaveTitle: TStringField;
    tblSoundsWave: TBlobField;
    edTitle: TDBEdit;
    edFileName: TDBEdit;
    Label1: TLabel;
    Label2: TLabel;
    OpenDialog: TOpenDialog;
    tblSoundsFileName: TStringField;
    SaveDialog: TSaveDialog;
    pnlToobar: TPanel;
    sbPlay: TSpeedButton;
    sbAdd: TSpeedButton;
    sbSave: TSpeedButton;
    sbExit: TSpeedButton;
    Bevel1: TBevel;
    dbnNavigator: TDBNavigator;
    stbStatus: TStatusBar;
    procedure sbPlayClick(Sender: TObject);
    procedure sbAddClick(Sender: TObject);
    procedure sbSaveClick(Sender: TObject);
    procedure sbExitClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    procedure OnAppHint(Sender: TObject);
  end;

var
  MainForm: TMainForm;

implementation

{$R *.DFM}

uses
  MMSystem;

procedure TMainForm.sbPlayClick(Sender: TObject);
var
  B: TBlobStream;
  M: TMemoryStream;
begin
  B := TBlobStream.Create(tblSoundsWave, bmRead); {create blob stream}
  Screen.Cursor := crHourGlass; {wait hourglass}
  try
    M := TMemoryStream.Create; {create memory stream}
    try
      M.CopyFrom(B, B.Size); {copy from blob to memory stream}
      {Attempt to play sound.  Raise exception if something goes wrong}
      Win32Check(PlaySound(M.Memory, 0, SND_SYNC or SND_MEMORY));
    finally
      M.Free;
    end;
  finally
    Screen.Cursor := crDefault;
    B.Free;
  end;
end;

procedure TMainForm.sbAddClick(Sender: TObject);
begin
  if OpenDialog.Execute then
  begin
    tblSounds.Append;
    tblSounds['FileName'] := ExtractFileName(OpenDialog.FileName);
    tblSoundsWave.LoadFromFile(OpenDialog.FileName);
    edTitle.SetFocus;
  end;
end;

procedure TMainForm.sbSaveClick(Sender: TObject);
begin
  with SaveDialog do
  begin
    FileName := tblSounds['FileName']; {initialize file name}
    if Execute then {execute dialog}
      tblSoundsWave.SaveToFile(FileName); {save blob to file}
  end;
end;

procedure TMainForm.sbExitClick(Sender: TObject);
begin
  Close;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  Application.OnHint := OnAppHint;
end;

procedure TMainForm.OnAppHint(Sender: TObject);
begin
  stbStatus.SimpleText := Application.Hint;
end;

end.

<< Back to main page