コード例 #1
0
        public void ChangeMediaPlayer(MediaPlayer newPlayer)
        {
            // When changing the media player, handle event subscriptions
            if (_mediaPlayer != null)
            {
                _mediaPlayer.AudioSource = null;
                _mediaPlayer.Events.RemoveListener(OnMediaPlayerEvent);
                _mediaPlayer = null;
            }

            _mediaPlayer = newPlayer;
            if (_mediaPlayer != null)
            {
                _mediaPlayer.Events.AddListener(OnMediaPlayerEvent);
                _mediaPlayer.AudioSource = _audioSource;
            }

            if (_supportPositionalAudio)
            {
                if (_audioSource.clip == null)
                {
                    // Position audio is implemented from hints found on this thread:
                    // https://forum.unity.com/threads/onaudiofilterread-sound-spatialisation.362782/
                    int       frameCount  = 2048 * 10;
                    int       sampleCount = frameCount * Helper.GetUnityAudioSpeakerCount();
                    AudioClip clip        = AudioClip.Create("dummy", frameCount, Helper.GetUnityAudioSpeakerCount(), Helper.GetUnityAudioSampleRate(), false);
                    float[]   samples     = new float[sampleCount];
                    for (int i = 0; i < samples.Length; i++)
                    {
                        samples[i] = 1f;
                    }
                    clip.SetData(samples, 0);
                    _audioSource.clip = clip;
                    _audioSource.loop = true;
                }
            }
            else if (_audioSource.clip != null)
            {
                _audioSource.clip = null;
            }
        }