//本文是笔者自己在Symbian OS实际开发中学习的记录,以及遇到的一些问题的总结。
Tips of Symbian OS Audio
Tip1. Brief Introduction
There are three classes commonly used on Symbian OS to output sound: “CMdaAudioPlayer”, “CMdaAudioRecorder”, “CMdaAudioOutputStream”.
These classes are all “C class”, which are derived from CBase. That means, you can allocate objects of these classes on heap, and they are able to be pushed on cleanup stack between two creation phases (after the constructor being called but before the ConstructL() call).
Playing sound data in descriptors (memory) and in files are both supported by Symbian OS.
CMdaAudioPlayer class can only play data in descriptors within PCM format, and also can play files in PCM, AMR and MIDI formats.
According to the documents of Symbian OS, class CMdaAudioRecorder is supposed to be able to play MIDI data in descriptors. Unfortunately, its behaviors are not the same to the description in the document. Practically, this class can not play MIDI data strored in the memory. And this problem was already declared in Nokia Technical Library as a “Known Issue”.Therefore the CMdaAudioRecorder class will not be discussed further more in this paper.
The last one is class CMdaAudioOutputStream. Only those PCM data in the memory is acceptable to this class. That is, if WAV data is offered, you may rip off the header section of WAV and write the raw data to the stream represented by class CMdaAudioOutputStream. Such raw data is indeed in the PCM format.
Tip2. Notices regarding to the use of CMdaAudioPlayer
a.
As calling PlayL() to play a MIDI file via CMdaAudioPlayer, you will get a considerable lag before the audio clip is really played.
b.
When playing an audio clip in AMR format, it is not played completely. About 0.5 second long sound in the end will be devoured. This issue is widely reported in several forums. A probably feasible solution is appending some trivial data to the end of AMR file.
Tip3. How to play AMR in the memory ?
A common method is converting data from AMR to PCM. Class CAmrToPcmDecoder could be used with SDK version 1.2 in the header file amrcodec.h . However, working with SDK version 2.0 or higher, you have to use class CMMFCodec, which is declared in mmf/server/mmfcodec.h . Please refer to source codes for usage details.
Tip4. Introduction to basic process of audio play classes
All these classes which are used to play audio clips have a same basic process in the programming logic. There are several phases in this process.
a.
Construct an object of callback class which is derived from one of class MMdaAudioPlayerCallback and class MMdaAudioOutputStreamCallback which are corresponding to class CMdaAudioPlayer and class CMdaAudioOutputStream. Plus, you must implement all the pure virtual methods declared in class MMdaAudioPlayerCallback and class MMdaAudioOutputStreamCallback.
b.
Prepare an object of an audio play class (one of CMdaAudioPlayer / CMdaAudioOutputStream or one of their derived classes) with a callback object parameter constructed in previous step. Here, the preparation of an object means a function call sequence composed of NewL() and other property setters.
c.
Call Open() or similar function to open audio device, if needed.
d.
Here you have to wait for callback function being called. If you call any other functions to manipulate the audio playing, system will raise a panic.
e.
Now you can manipulate the audio playing via PlayL() (WriteL() for CMdaAudioOutputStream), StopL() and et cetera. However, you must make sure the callback function corresponding to the previous operation has been called before a new operation is applied to audio play objects. You have to deal with this principle carefully, because most of the audio panics are caused by breaking this principle.
Tip5. Reconstruct audio play object in a proper place
There would be a lot of factors can cause sound disappearing, such as interrupts, key press in mess and some unkown reasons. Generally, when these problems happened, the callback functions would receive an error codes which represent all kinds of errors. In most situations, the audio play object used in our sound system need to be destroyed and reconstructed, or the sound would probably disappear for ever.
Tip6. Weird points in the Symbian OS firmed in NOKIA 6680
On 6680, it is necessary to reconstruct audio play object forcely after an interrupt event. Callback functions will get error code of KErrNone which means “nothing wrong” after an interrupt event. However, the phone will show us a silence from then on. You even could not get a leave or a panic as sounds disappearing.