0

我在 Xcode 中编程总共三个月了,我遇到了这个小问题:

目前,我正在使用AVPlayer构建一个广播流应用程序,您可以在其中从UICollectionView中的一堆不同广播电台中进行选择。当您选择一个站点时,会出现一个子视图,它会使UICollectionView变暗并显示一个带有站点名称和ActivityIndi ​​cator的加载视图。到目前为止,一切都很好。

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

当收音机开始播放/完成加载时,我应该在哪里放置我的关闭方法来关闭子视图?

子视图截图: http ://dl.dropbox.com/u/49270819/iOS%20Simulator%20Screen%20shot%2026%20feb%202013%2023.54.04.png

谢谢!

4

1 回答 1

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
    }
}
于 2013-02-28T09:12:25.417 回答