syedhali / EZAudio

An iOS and macOS audio visualization framework built upon Core Audio useful for anyone doing real-time, low-latency audio processing and visualizations.
Other
4.94k stars 821 forks source link

EZAudioPlayer can not play when screen is locked #303

Open mydy1987 opened 8 years ago

mydy1987 commented 8 years ago

Hi,thanks for your EZAudio,it helps me a lot by using the functionary of FFT.

I found that EZAudioPlayer can play when app is in background,but can not play when screen is locked.

I want to confirm that whether this EZAudioPlayer can play when screen is locked.

3ks!

mydy1987 commented 8 years ago

I solve it with following codes.

AVAudioSession *session = [AVAudioSession sharedInstance];
[session setActive:YES error:nil];
[session setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
saitjr commented 8 years ago

But the sound comes from microphone, not the speaker. Did you solve it?

dxclancy commented 8 years ago

Hi, I also had this problem, and I believe I've found the solution for this. (Note, for me, it worked on the "lock screen" when it was visible, but no audio when the phone was locked and screen was dark)

There is an Apple Tech Note that says you must set kAudioUnitProperty_MaximumFramesPerSlice to 4096, which EZOutput does do like so:

    [EZAudioUtilities checkResult:AudioUnitSetProperty(self.info->mixerNodeInfo.audioUnit,
                                                       kAudioUnitProperty_MaximumFramesPerSlice,
                                                       kAudioUnitScope_Global,
                                                       0,
                                                       &maximumFramesPerSlice,
                                                       sizeof(maximumFramesPerSlice))
                        operation:"Failed to set maximum frames per slice on mixer node"];

However, the tech note says it must be set "for each Audio Unit being used".

So, I added the same setting for the converterNode and outputNode, and audio started working on the lock screen:

 [EZAudioUtilities checkResult:AudioUnitSetProperty(self.info->converterNodeInfo.audioUnit,
                                                       kAudioUnitProperty_MaximumFramesPerSlice,
                                                       kAudioUnitScope_Global,
                                                       0,
                                                       &maximumFramesPerSlice,
                                                       sizeof(maximumFramesPerSlice))
                        operation:"Failed to set maximum frames per slice on mixer node"];
    [EZAudioUtilities checkResult:AudioUnitSetProperty(self.info->outputNodeInfo.audioUnit,
                                                       kAudioUnitProperty_MaximumFramesPerSlice,
                                                       kAudioUnitScope_Global,
                                                       0,
                                                       &maximumFramesPerSlice,
                                                       sizeof(maximumFramesPerSlice))
                        operation:"Failed to set maximum frames per slice on mixer node"];