Search code examples
iosunity-game-engineeveryplay

Everyplay holds my microphone UNITY3D iOS


I have one question regarding implementation of Everyplay Video Recording on iOS devices.

The problem occurs when I finish recording. I go to Everyplay video preview where you can edit video and share it. Then, when I go back to my game when clicking on back or after sharing video, I cannot use the microphone in Unity.

I think that Everyplay still holds my microphone but I don't know how to release it. I have the same implementation on Android and it works perfectly. I think that the problem exists because on iOS you have an option to edit video using microphone or camera.

Any thoughts?


Solution

  • I was unable to replicate your issue on iOS7. However the problem did exist on iOS6. I made a small hack which seems to fix it on my device.

    EveryplayMicHack.cs (copy this to Plugins/Everyplay/Scripts folder):

    using UnityEngine;
    using System.Runtime.InteropServices;
    
    public class EveryplayMicHack {
        public static void EnableRecording() {
    #if UNITY_IPHONE && !UNITY_EDITOR
            SetPreferredSampleRate(AudioSettings.outputSampleRate);
    #endif
        }
    
    #if UNITY_IPHONE && !UNITY_EDITOR
        [DllImport ("__Internal")]
        private static extern void SetPreferredSampleRate(int sampleRate);
    #endif
    }
    

    EveryplayMicHack.h (copy this to Plugins/iOS folder)

    #import <AVFoundation/AVFoundation.h>
    
    void SetPreferredSampleRate(int sampleRate);
    

    EveryplayMicHack.m (copy this to Plugins/iOS folder)

    #import "EveryplayMicHack.h"
    
    void SetPreferredSampleRate(int sampleRate) {
        AVAudioSession *audioSession = [AVAudioSession sharedInstance];
        [audioSession setPreferredHardwareSampleRate:sampleRate error:nil];
    }
    

    And before initializing your microphone, call: EveryplayMicHack.EnableRecording();

    // Something like this
    EveryplayMicHack.EnableRecording();
    
    myMicAudioSource.clip = null;
    myMicAudioSource.clip = Microphone.Start("Built-in Microphone", ...
    

    I hope it works for you too! :)