Exemplo n.º 1
0
        /// <summary>
        /// Plays an audio using the underlying OS's native method. A number stored in this object will be used to determine which loaded audio data at native side that we would like to play. If you previously call `Prepare()` it will take effect here.

        /// [iOS] (Unprepared) Native Audio remembered which of the total of 32 OpenAL source that we just played. It will get the next one (wraps to the first instance when already at 32nd instance), find the correct sound buffer and assign it to that source, then play it.
        /// [iOS] (Prepared) Instead of using buffer index (the sound, not the sound player) it will play the source at the time you call `Prepare` immediately without caring if the sound in that source is currently the same as when you call `Prepare` or not. After calling this `Play`, the prepare status will be reset to unprepared, and the next `Play` will use buffer index as usual.

        /// [Android] (Unprepared) It searches for `AudioTrack` which already contains the `byte[]` representing this sound that is not currently playing. If not found, we load the byte array into any non playing `AudioTrack`. If every `AudioTrack` is playing, it stops and replaces the first instance. Then play it. We use this "searching" approach instead of just assign and play round-robin style of iOS because on Android we are bounded with very low number of AudioTrack and the `write` method takes more time than looping.
        /// [Android] (Prepared) Same as unprepared, but `Prepare` method have already make sure such `AudioTrack` with the correct `byte[]` data exists so the search should found it quickly. Prepare status is never reset when calling `Play()`.

        /// </summary>
        /// <param name="volume">From 0.0f to 1.0f. The interpolation is in linear space.</param>
        /// <param name="pan">-1.0f : Left, 0.0f : Center, 1.0f : Right [iOS] Not implemented yet. All sounds will be at center 0.0f. [Android] It works.</param>
        /// <returns> Please treat this return as `void` for now. The class will be usable in the future version.</returns>
        public NativeAudioController Play(float volume = 1, float pan = 0)
        {
            if (isUnloaded)
            {
                throw new System.Exception("You cannot play an unloaded NativeAudio.");
            }

            int playedSourceIndex = -1;

#if UNITY_IOS
            if (prepared)
            {
                //This is using source index. It means we have already loaded our sound to that source with Prepare.
                NativeAudio._PlayAudioWithSourceIndex(this.prepareIndex, volume, pan);
                playedSourceIndex = this.prepareIndex;
            }
            else
            {
                //This is using buffer index. Which source it use will be determined at native side.
                playedSourceIndex = NativeAudio._PlayAudio(this.NextIndex, volume, pan);
            }
            prepared = false;
#elif UNITY_ANDROID
            playedSourceIndex = NativeAudio.AndroidNativeAudio.CallStatic <int>(NativeAudio.AndroidPlayAudio, this.NextIndex, volume, pan);
#endif
            return(new NativeAudioController(playedSourceIndex)); //Empty object for now
        }