检测声音时调用动作? (无线电流)

时间:2013-02-26 22:51:30

标签: iphone ios xcode streaming avplayer

我已经在Xcode中编程总共三个月而且我遇到了这个小问题:

目前我正在使用 AVPlayer 构建一个无线电流应用,您可以在 UICollectionView 中从一系列不同的无线电平台中进行选择。当您选择一个工作站时,会出现一个子视图,它会使 UICollectionView 变暗,并显示一个带有工作站名称和 ActivityIndi​​cator 的加载视图。到现在为止还挺好。

问题是我不知道如何在视频开始播放后摆脱它。现在我通过触摸视图暂时关闭它,但当然我希望当应用检测到声音时这会自动发生。

当收音机开始播放/完成加载时,我应该在哪里放置我的dismiss-method来关闭subView?

subView的屏幕截图: http://dl.dropbox.com/u/49270819/iOS%20Simulator%20Screen%20shot%2026%20feb%202013%2023.54.04.png

谢谢!

1 个答案:

答案 0 :(得分:0)

尝试使用此代码启动声音监控:

#pragma mark - AudioStuff

double lowPassResultAudio;
-(void) initAudioRecorder {
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    [audioSession setDelegate: self];
    NSError *audioSessionError;
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&audioSessionError];
    [[AVAudioSession sharedInstance] setActive: YES error: &audioSessionError];

    NSMutableDictionary* recordSetting = [[NSMutableDictionary alloc] init];
    // We can use kAudioFormatAppleIMA4 (4:1 compression) or kAudioFormatLinearPCM for nocompression
    [recordSetting setValue :[NSNumber numberWithInt:kAudioFormatAppleIMA4] forKey:AVFormatIDKey];
    // We can use 44100, 32000, 24000, 16000 or 12000 depending on sound quality
    [recordSetting setValue:[NSNumber numberWithFloat:44100.0] forKey:AVSampleRateKey];
    // We can use 2(if using additional h/w) or 1 (iPhone only has one microphone)
    [recordSetting setValue:[NSNumber numberWithInt: 1] forKey:AVNumberOfChannelsKey];

    //initiate recorder
    inputAudioUrl = [NSURL fileURLWithPath:pathToFile];
    recorder = [[AVAudioRecorder alloc] initWithURL:inputAudioUrl settings:recordSetting error:&error];
    if(recorder){
        //setting to speaker for appropritae recording
        UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,
                                 sizeof (audioRouteOverride),&audioRouteOverride);
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        recorder.meteringEnabled = YES;

        //for monitoring with same recorder
        [recorder record];

        [[NSTimer alloc] initWithFireDate:[NSDate date] interval:0.1 target:self selector:@selector(monitorAudioController:) userInfo:,mil repeats:YES]

    } else
        NSLog(@"%@",[error description]);
}

现在,如果声音值超过限制,即正在播放某些近声音,则删除您的视图。

//monitor audion with same recorder
-(void) monitorAudioController: (NSTimer *) timer {
    [recorder updateMeters];
    const double ALPHA = 0.05;
    double peakPowerForChannel_ = pow(10, (ALPHA * [recorder peakPowerForChannel:0]));
//    double lowpassMultiplier = (lowPassResultAudio > 0.2f) ? 0.2f : lowPassResultAudio;
    double AUDIOMONITOR_THRESHOLD = 0.7;
    lowPassResultAudio = ALPHA * peakPowerForChannel_ + (1.0 - ALPHA) * AUDIOMONITOR_THRESHOLD;

   // NSLog(@"peakPowerForChannel_=%lf, lowpassMultiplier = %lf, lowPassResultAudio = %lf", peakPowerForChannel_, AUDIOMONITOR_THRESHOLD, lowPassResultAudio);
    if (lowPassResultAudio >= AUDIOMONITOR_THRESHOLD) {
           //remove here
           //invalidate timer
           //stop recorder monitoring
    }
}