示範如何利用第三方的SCListener進行麥克風輸入的偵測
說明:示範如何利用第三方的SCListener進行麥克風輸入的偵測
示範:
偵測前功能畫面。
啟動麥克風偵測後按下取得音量資料可獲得收音後的音量平均值與最大值。
此範例採取的是手動抓取音量,正式應用時可利用NSTimer持續抓取。
程式碼:
MicDemoViewController.h
#import <UIKit/UIKit.h>
#import "SCListener.h"
@interface MicDemoViewController : UIViewController {
IBOutlet UIProgressView *pv_averagePower;
IBOutlet UIProgressView *pv_peakPower;
SCListener *listener;
}
@property (nonatomic,retain) UIProgressView *pv_averagePower;
@property (nonatomic,retain) UIProgressView *pv_peakPower;
@property (nonatomic,retain) SCListener *listener;
-(IBAction) Start:(id)sender;
-(IBAction) Pause:(id)sender;
-(IBAction) Stop:(id)sender;
-(IBAction) GetPower:(id)sender;
@end
MicDemoViewController.m
#import "MicDemoViewController.h"
#import "SCListener.h"
@implementation MicDemoViewController
@synthesize pv_averagePower,pv_peakPower,listener;
-(IBAction) Start:(id)sender{
//開始偵測
[[SCListener sharedListener] listen];
//取得平均音量
[[SCListener sharedListener] averagePower];
//取得最大音量
[[SCListener sharedListener] peakPower];
SCListener *listener_t = [SCListener sharedListener];
self.listener=listener_t;
[listener_t release];
}
-(IBAction) Pause:(id)sender{
[listener pause];
[listener listen]; // Quick.
}
-(IBAction) Stop:(id)sender{
[listener stop];
[listener listen]; // Slower.
}
-(IBAction) GetPower:(id)sender{
AudioQueueLevelMeterState *levels = [listener levels];
Float32 peak = levels[0].mPeakPower;
Float32 average = levels[0].mAveragePower;
if (![listener isListening]) // If listener has paused or stopped…
return; // …bail.
pv_averagePower.progress=average;
pv_peakPower.progress=peak;
}