5

Are there any tutorials or examples on how to play WAV files on Mac from application built by Delphi XE2 FireMonkey?

I'm asking because this code does not work:

var
  //fWaves head and data is initialized and plays properly on Win through OpenAL
  fWaves: array of record 
    Head: TWAVHeaderEx;
    Data: array of byte;
  end;
  D: NSData;
  N: NSSound;
begin
  D := TNSData.Wrap(TNSData.Create.initWithBytes(@fWaves[0].Head, SizeOf(fWaves[0].Head) + fWaves[0].Head.DataSize));
  ShowMessage(IntToStr(D.length)); //--Length is correct
  N := TNSSound.Wrap(TNSSound.Create.initWithData(D));
  ShowMessage(FloatToStr(N.Duration)); //--Displays 0
  if N.play then
    ShowMessage('Yes')
  else
    ShowMessage('No'); //--Always returns NO
end;

I'm loading WAV file header and data from memory and it plays fine with OpenAL. So the problem is somewhere in my NSSound usage. Could anyone please provide some working examples of Delphi XE2 sound playback on Mac?

Andreas Rejbrand
  • 105,602
  • 8
  • 282
  • 384
Kromster
  • 7,181
  • 7
  • 63
  • 111
  • I'm guessing that the format of a fwaves record does not exactly match the format of an actual WAV file. Try using the raw bytes from the file. – Marcus Adams Feb 16 '12 at 18:41
  • Tried that, as well tried initFromFile(path_to_aiff).. Nothing helped. So I have finally switched to OpenAL. – Kromster Feb 16 '12 at 18:59

2 Answers2

3

Freepascal/Lazarus has a unit called "macosall.pas" which is a translation of the os-x C++ headerfiles to pascal, allowing you to call native os-x functions.

Sadly this unit is not a part of delphi, but by opening the unit you can copy the function declaration over to delphi and it should work.

I expect that you will find what you need in the quicktime API which is the central hub of media on apple machines. Also check out Apple developer and have a look at the docs.

Jon Lennart Aasenden
  • 3,920
  • 2
  • 29
  • 44
  • Which does not gives me a clue on whats wrong with NSSound-NSData in quoted code. – Kromster Feb 13 '12 at 17:58
  • While coding in C# and mono for iOS, i experienced much of the same problems but in different areas. If TNSData has the same methods as the original Objective C class, then you can push the data as either a NSStream, NSBuffer or even text. In the code above you use a normal byte buffer it seems, try initializing from a stream. One example is the mail-send dialog which according to apple docs should take just about any NSdata object - but it was not until i used a stream that it worked. – Jon Lennart Aasenden Feb 14 '12 at 21:18
0

Might as well post it as an answer, in case that helps anyone:

Tried that, as well tried initFromFile(path_to_aiff).. Nothing helped. So I have finally switched to OpenAL.

Maybe in future Embarcadero fixes that specific way of accessing NSSound, but for now OpenAL works best. Also, OpenAL is cross-platform, meaning that I can use same sound code on Win and Mac.

Kromster
  • 7,181
  • 7
  • 63
  • 111