iOS 声音多媒体

1,首先介绍播放系统音频。这个系统声音就是相对比较短的,只有几秒中,不能重复,立即播放的那种声音。如短信铃声。Beep,Boun...

声明两个变量:

    CFURLRef soundFileURLRef; //声音文件路径

    SystemSoundID soundFileObject; //声音id

然后定义函数使用。      

 1 -(void)soundPlay{
 2 {
 3         CFBundleRef mainBundle = CFBundleGetMainBundle ();
 4         soundFileURLRef = CFBundleCopyResourceURL (mainBundle,
 5                                                    CFSTR ("pageflip"),
 6                                                    CFSTR ("aif"),
 7                                                    NULL);
 8         
 9         AudioServicesCreateSystemSoundID(soundFileURLRef, &soundFileObject);
10         AudioServicesAddSystemSoundCompletion(soundFileObject,
11                                               NULL, // uses the main run loop
12                                               NULL, // uses kCFRunLoopDefaultMode
13                                               SoundFinished, // the name of our custom callback function
14                                               NULL // for user data, but we don't need to do that in this case, so we just pass NULL
15                                               );
16 
17         AudioServicesPlaySystemSound (soundFileObject); 
18 }
19 
20 //这个函数是C风格的。
21 void SoundFinished(SystemSoundID sound_id, void* user_data){
22     AudioServicesDisposeSystemSoundID(sound_id);
23 }
"pageflip.aif"是文件全名。

  AudioServicesCreateSystemSoundID是注册声音,

    AudioServicesPlaySystemSound播放声音

    AudioServicesAddSystemSoundCompletion监听完成事件方法,完成后你要记得清楚这个声音,因为它要占用资源的。

    AudioServicesDisposeSystemSoundID清除声音  

 

2,然后介绍录制和播放音频。

利用AVFoundation中的AVAudioPlayer,AVAudioRecoder可以很方便的实现iOS程序的录音和播放录音的功能。

 1 @interface ChatRoomViewController ()
 2 {
 3     AVAudioRecorder *recoder;
 4     AVAudioPlayer *player;
 5 }
 6 @end;
 7 
 8 @implementation ChatRoomViewController
 9 - (IBAction)inputSource:(id)sender
10 {
11     [self audioInput];
12 }
13 
14 -(void)audioInput
15 {
16     NSString *cacheDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
17     NSURL *soundFileURL = [NSURL URLWithString:[cacheDirectory stringByAppendingPathComponent:@"myReturnSound.wav"]];
18     
19     if (recoder.recording){
20         [recoder stop];
21 
22         player = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:NULL];
23         [player play];
24     }else{
25         if (!recoder){
26             recoder = [[AVAudioRecorder alloc] initWithURL:soundFileURL settings:nil error:NULL];
27             recoder.meteringEnabled = YES;
28         }
29         [recoder record];
30     }
31 }

 

 在上面,我使用的目录为系统临时目录:NSCachesDirectory

声音文件的名字就随便取了。

[[AVAudioRecorder alloc] initWithURL:soundFileURL settings:nil error:NULL];中的setting可以根据下面要求自定义设置。
 1 NSMutableDictionary *settings = [NSMutableDictionary dictionary];
 2     [settings setValue:[NSNumber numberWithInt:kAudioFormatLinearPCM] 
 3                 forKey:AVFormatIDKey];
 4     [settings setValue:[NSNumber numberWithFloat:44100.0] 
 5                 forKey:AVSampleRateKey]; //采样率
 6     [settings setValue:[NSNumber numberWithInt:1] 
 7                 forKey:AVNumberOfChannelsKey];//通道的数目
 8     [settings setValue:[NSNumber numberWithInt:16] 
 9                 forKey:AVLinearPCMBitDepthKey];//采样位数  默认 16
10     [settings setValue:[NSNumber numberWithBool:NO] 
11                 forKey:AVLinearPCMIsBigEndianKey];//大端还是小端 是内存的组织方式
12     [settings setValue:[NSNumber numberWithBool:NO] 
13                 forKey:AVLinearPCMIsFloatKey];//采样信号是整数还是浮点数

 

如果没有在录音就判断当前是否有Recoder,没有就创建,有则开始录音。如果已经在录音中,再点击按钮就是停止录音。同时也播放出了刚才录音的内容。

这个地方已经把刚才的声音给了player,所以可以把player对象发给你需要用的地方。

 

另外,在录音是你如果想根据音量的大小显示不同效果,就象微信语音输入时那个话筒的变化。要使用recoder 的meter。

首先要打开meter

recoder.meteringEnabled = YES;

[recoder updateMeters];

float peakPower = [recoder peakPowerForChannel:0];

float averagePower = [recoder averagePowerForChannel:0];

posted @ 2012-12-04 16:35  lynn.liu  阅读(1655)  评论(0编辑  收藏  举报