2011年5月2日 星期一

About AVAudioPlayer

There are many different ways to get an iPhone app to generate and record audio, but AVFoundation is the best compromise between simplicity and functionality

AVFoundation.framework : AVAudioPlayer, AVAudioRecorder



AVAudioPlayer
AVAudioPlayer handles the playback of any type of supported audio file(wav, mp3,etc) with minimal configuration.

The file has to be on the device!!
easy setup :

NSString *path;
NSURL *url;

path = [[NSBundle mainBundle] pathForResource:@"2" ofType:@"wav"];
url = [NSURL fileURLWithPath:path];
playerRe = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];
/*  ready to play!  */

You can also load the AVAudioPlayer from memory





NSData *data = ....; /* binary data of an audio file */
AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithData:data error:nil];

/*  ready to play!  */

Each AVAudioPlayer instance can only play a single audio file !

The audio file is determined at object creation time. You cannot make an AVAudioPlayer play new audio data after it has been created

You need to create an AVAudioPlayer object for each unique sound !!


Also holds true for playing multiple simultaneous instances of the same sound each needs its own AVAudioPlayer


You only need one AVAudioPlayer per sound if you are ok stopping playback of existing instances( so only one instance of each sound is playing at a time)


Simple programmatic playback control



[player play]; 
[player stop];  /* 音樂停止播放且清空buffer */
[player pause]; /* 音樂停止播放且不清空buffer */
[player prepareToPlay]; /* preloads data:執行音樂播放的準備,降低播放開始所延誤的時間 */
player.numberOfLoops = ...;  /* set # of loops ;  0:播放一次;  1:播放兩次; 負數:無限次播放*/

if (player.playing) ...   /* check if we are active */

player.duration /* how long is the audio? */
player.currentTime  /* current playing offset (RW) */


If you anticipate playing the same sound multiple times ( halting existing playback ), do this to the existing AVAudioPlayer object
[player stop];
player.currentTime =0;  /* dynamic time shifting */

[player play];

Each AVAudioPlayer object has individual control over its own volume
player.volume = 0.5; /* 0.0(最小) to 1.0(最大), R/W */
Volume can be set dynamically while the audio is playing...

Get basic audio level data
player.meteringEnabled = YES;
while{
[player updateMeters];
float average = [player averagePowerForChannel:0];
float peak = [player peakPowerForChannel:0];
}

Returns dB : -160dB [silent] to 0dB [loud]

Use an AVAudioPlayerDelegate to catch events (player.delegate = self;)
- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
/* 音樂播放的完畢通知 */

- (void)audioPlayerDecodeErrorDidOccur:(AVAudioPlayer *)player error:(NSError *)error
/* 解碼發生錯的通知 */

- (void)audioPlayerBeginInterruption:(AVAudioPlayer *)player
/* 音樂播放中斷通知   播放時若發生電話來電或警示等,讓音樂播放暫停 */

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)player
/* 若電話或警示結束後,可利用此委託執行音樂重新播放通知  呼叫play:方法 */


The AVAudioPlayer object needs to be "alive" during the entire sound playback

Use the delegate to free the object


{

               playerRe = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:nil];

         player.delegate = self
        [player play];
/* needs to be released when finished */

}

- (void)audioPlayerDidFinishPlaying:(AVAudioPlayer *)player successfully:(BOOL)flag
{
[playerRe autorelease];
}



沒有留言:

張貼留言