コード例 #1
0
    // give user a chance to change MicrophoneDevice in Awake()
    void Start()
    {
        if (photonView.isMine)
        {
            var pvs = PhotonVoiceSettings.Instance;

            Application.RequestUserAuthorization(UserAuthorization.Microphone);
            // put required sample rate into audio source and encoder - both adjust it if needed
            Voice.IBufferReader <float> audioStream;
            int channels           = 0;
            int sourceSamplingRate = 0;
            if (AudioClip == null)
            {
                if (Microphone.devices.Length < 1)
                {
                    // Error already logged in PhotonVoiceNetwork.Awake()
                    return;
                }

                var micDev = this.MicrophoneDevice != null ? this.MicrophoneDevice : PhotonVoiceNetwork.MicrophoneDevice;
                if (PhotonVoiceSettings.Instance.DebugInfo)
                {
                    Debug.LogFormat("PUNVoice: Setting recorder's microphone device to {0}", micDev);
                }
                var mic = new MicWrapper(micDev, (int)pvs.SamplingRate);
                sourceSamplingRate = mic.SourceSamplingRate;
                channels           = mic.Channels;
                audioStream        = mic;
            }
            else
            {
                audioStream        = new AudioClipWrapper(AudioClip);
                sourceSamplingRate = AudioClip.frequency;
                channels           = AudioClip.channels;
                if (this.LoopAudioClip)
                {
                    ((AudioClipWrapper)audioStream).Loop = true;
                }
            }

            Voice.VoiceInfo voiceInfo = Voice.VoiceInfo.CreateAudioOpus(pvs.SamplingRate, sourceSamplingRate, channels, pvs.FrameDuration, pvs.Bitrate, photonView.viewID);
            this.voice                   = createLocalVoice(voiceInfo, audioStream);
            this.VoiceDetector.On        = PhotonVoiceSettings.Instance.VoiceDetection;
            this.VoiceDetector.Threshold = PhotonVoiceSettings.Instance.VoiceDetectionThreshold;
            if (this.voice != Voice.LocalVoiceAudio.Dummy)
            {
                this.voice.Transmit = PhotonVoiceSettings.Instance.AutoTransmit;
            }
            else if (PhotonVoiceSettings.Instance.AutoTransmit)
            {
                Debug.LogWarning("PUNVoice: Cannot Transmit.");
            }
            sendVoiceCreatedMessage(voiceInfo);
        }
    }
コード例 #2
0
    // give user a chance to change MicrophoneDevice in Awake()
    void Start()
    {
        if (photonView.isMine)
        {
            var pvs = PhotonVoiceSettings.Instance;

            if (!this.microphoneDeviceSet)
            {
                this.MicrophoneDevice = PhotonVoiceNetwork.MicrophoneDevice;
            }

            Application.RequestUserAuthorization(UserAuthorization.Microphone);
            // put required sample rate into audio source and encoder - both adjust it if needed
            Voice.IAudioStream audioStream;
            int channels = 0;
            if (AudioClip == null)
            {
                if (PhotonVoiceSettings.Instance.DebugInfo)
                {
                    Debug.Log("PUNVoice: Setting recorder's microphone device to " + this.MicrophoneDevice);
                }
                var mic = new MicWrapper(this.MicrophoneDevice, (int)pvs.SamplingRate);
                this.microphoneDeviceUsed = true;
                channels    = mic.Channels;
                audioStream = mic;
            }
            else
            {
                audioStream = new AudioClipWrapper(AudioClip);
                channels    = AudioClip.channels;
                if (this.LoopAudioClip)
                {
                    ((AudioClipWrapper)audioStream).Loop = true;
                }
            }

            Voice.VoiceInfo voiceInfo = new Voice.VoiceInfo((int)pvs.SamplingRate, channels, (int)pvs.Delay, pvs.Bitrate, photonView.viewID);
            this.voice = PhotonVoiceNetwork.CreateLocalVoice(audioStream, voiceInfo);

            this.VoiceDetector.On        = PhotonVoiceSettings.Instance.VoiceDetection;
            this.VoiceDetector.Threshold = PhotonVoiceSettings.Instance.VoiceDetectionThreshold;
        }
    }
コード例 #3
0
        private IEnumerator GetAudioClip(AudioSource audioSource, string path, bool play)
        {
            if (audioClips.ContainsKey(path))
            {
                AudioClipWrapper acw = audioClips[path];
                while (!acw.loaded)
                {
                    yield return(null);
                }
                audioSource.clip = acw.audioClip;
                if (play)
                {
                    audioSource.Play();
                }
                yield break;
            }
            else
            {
                audioClips.Add(path, new AudioClipWrapper {
                    audioClip = null,
                    loaded    = false
                });
            }
            using (UnityWebRequest www = UnityWebRequestMultimedia.GetAudioClip(path, GetAudioType(path))) {
                yield return(www.SendWebRequest());

                if (www.isNetworkError)
                {
                    Debug.Log(www.error);
                }
                else
                {
                    AudioClip audio = DownloadHandlerAudioClip.GetContent(www);
                    audioClips[path].audioClip = audio;
                    audioClips[path].loaded    = true;
                    audioSource.clip           = audio;
                    if (play)
                    {
                        audioSource.Play();
                    }
                }
            }
        }
コード例 #4
0
    private void InitialVoiceSetUp()
    {
        var pvs = PhotonVoiceSettings.Instance;

        Application.RequestUserAuthorization(UserAuthorization.Microphone);
        // put required sample rate into audio source and encoder - both adjust it if needed
        Voice.IAudioStream audioStream;
        int channels = 0;

        if (AudioClip == null)
        {
            var micDev = microphoneDevice ?? PhotonVoiceNetwork.MicrophoneDevice ?? (Microphone.devices.Any() ? Microphone.devices.First() : null);
            microphoneDevice = micDev;
            if (PhotonVoiceSettings.Instance.DebugInfo)
            {
                Debug.LogFormat("PUNVoice: Setting recorder's microphone device to {0}", micDev);
            }
            var mic = new MicWrapper(micDev, (int)pvs.SamplingRate);
            channels    = mic.Channels;
            audioStream = mic;
        }
        else
        {
            audioStream = new AudioClipWrapper(AudioClip);
            channels    = AudioClip.channels;
            if (LoopAudioClip)
            {
                ((AudioClipWrapper)audioStream).Loop = true;
            }
        }

        Voice.VoiceInfo voiceInfo = new Voice.VoiceInfo((int)pvs.SamplingRate, channels, (int)pvs.Delay, pvs.Bitrate, photonView.viewID);
        voice                   = PhotonVoiceNetwork.CreateLocalVoice(audioStream, voiceInfo);
        VoiceDetector.On        = PhotonVoiceSettings.Instance.VoiceDetection;
        VoiceDetector.Threshold = PhotonVoiceSettings.Instance.VoiceDetectionThreshold;
        MicrophoneDevice        = microphoneDevice;
    }