/// <summary>
        /// Plays a single note. Separate from the rest of the song playing code
        /// </summary>
        public static void PlayNote(Instrument.Note note)
        {
            lock (syncObj)
            {
#if __ANDROID__
                if (playingTrack != null)
                {
                    //We use pause instead of stop because pause stops playing immediately
                    playingTrack.Pause();
                    playingTrack.Release();
                    playingTrack.Dispose();
                }
#endif
#if __IOS__
                if (audioQueue != null)
                {
                    //Pass true to stop immediately
                    audioQueue.Stop(true);
                    audioQueue.Dispose();
                }
#endif

#if __ANDROID__
                playingTrack = new AudioTrack(
                    // Stream type
                    Android.Media.Stream.Music,
                    // Frequency
                    SongPlayer.PLAYBACK_RATE,
                    // Mono or stereo
                    ChannelOut.Mono,
                    // Audio encoding
                    Android.Media.Encoding.Pcm16bit,
                    // Length of the audio clip in bytes
                    (note.data.Length * 2),
                    // Mode. Stream or static.
                    AudioTrackMode.Static);

                playingTrack.Write(note.data, 0, note.data.Length);
                playingTrack.Play();
#endif
#if __IOS__
                audioQueue = new OutputAudioQueue(AudioStreamBasicDescription.CreateLinearPCM(SongPlayer.PLAYBACK_RATE, 1, 16, false));
                unsafe
                {
                    AudioQueueBuffer *buffer;
                    audioQueue.AllocateBuffer(note.data.Length * 2, out buffer);

                    fixed(short *beatData = note.data)
                    {
                        buffer->CopyToAudioData((IntPtr)beatData, note.data.Length * 2);
                    }

                    audioQueue.EnqueueBuffer((IntPtr)buffer, note.data.Length * 2, null);
                }

                audioQueue.Start();
#endif
            }
        }
Exemple #2
0
        public static void ReturnAudioTrack(AudioTrack audioTrack)
        {
            AudioTrackData audioTrackData = null;

            for (int i = 0; i < m_audioTracks.Count; i++)
            {
                if (m_audioTracks[i].AudioTrack == audioTrack)
                {
                    audioTrackData = m_audioTracks[i];
                    break;
                }
            }
            if (!Mixer.EnableAudioTrackCaching)
            {
                if (audioTrackData != null)
                {
                    m_audioTracks.Remove(audioTrackData);
                }
                audioTrack.Pause();
                audioTrack.Release();
                return;
            }
            if (audioTrackData == null)
            {
                audioTrack.Pause();
                audioTrack.Release();
                return;
            }
            bool flag = false;

            if (m_audioTracks.Count > 16)
            {
                flag = true;
                for (int j = 0; j < m_audioTracks.Count; j++)
                {
                    if (m_audioTracks[j].BytesCount < audioTrackData.BytesCount)
                    {
                        flag = false;
                        break;
                    }
                }
            }
            if (flag)
            {
                audioTrack.Pause();
                audioTrack.Release();
                m_audioTracks.Remove(audioTrackData);
            }
            else
            {
                audioTrack.Stop();
                audioTrack.SetPlaybackHeadPosition(audioTrackData.BytesCount / audioTrackData.SoundBuffer.ChannelsCount / 2);
                audioTrackData.ReloadStaticDataTime = Time.FrameStartTime + 0.75;
            }
            LogCacheStats();
        }
Exemple #3
0
        /// <summary>
        /// Stops the currently playing audio
        /// </summary>
        public void StopPlaying()
        {
            //This lock is used to ensure that starting and stopping of songs do not happen at the same time.
            lock (startStopSyncObject)
            {
                if (!IsPlaying)
                {
                    throw new InvalidOperationException("Audio is not playing");
                }
#if __ANDROID__
                //We use pause instead of stop because pause stops playing immediately
                playingTrack.Pause();

                //Lock track disposal so the track is never in a state where it is disposed/released but not null
                lock (trackDisposedOfSyncObject)
                {
                    playingTrack.Release();
                    playingTrack.Dispose();
                    playingTrack = null;
                }
#endif
#if __IOS__
                //Pass true to stop immediately
                audioQueue.Stop(true);

                //Lock track disposal so the track is never in a state where it is disposed but not null
                lock (trackDisposedOfSyncObject)
                {
                    audioQueue.Dispose();
                    audioQueue = null;
                }
#endif
            }
        }
Exemple #4
0
 /// <summary>
 /// Pause the audio
 /// </summary>
 public static void Pause()
 {
     if (active && !paused && song.IsPlaying)
     {
         song.Pause();
         paused = true;
     }
 }
Exemple #5
0
    public void pause()
    {
        if (audioPlayer == null)
        {
            return;
        }

        audioPlayer.Pause();
    }
Exemple #6
0
 internal virtual void InternalDispose()
 {
     Mixer.m_sounds.Remove(this);
     if (m_audioTrack != null)
     {
         m_audioTrack.Pause();
         m_audioTrack.Release();
         m_audioTrack.Dispose();
         m_audioTrack = null;
         Mixer.m_audioTracksDestroyed++;
     }
 }
Exemple #7
0
 public void Pause()
 {
     if (State == SoundStates.Paused)
     {
         return;
     }
     if (instance == null)
     {
         return;
     }
     instance.Pause();
     State = SoundStates.Paused;
 }
Exemple #8
0
        private void StopStreamingAudio()
        {
#if __ANDROID__
            if (playingTrack == null || playingTrack.PlayState != PlayState.Playing)
            {
                throw new InvalidOperationException("Audio is not playing");
            }

            playingTrack.Pause();
            //playingTrack.Flush();
            playingTrack.Release();
            playingTrack.Dispose();
#endif
        }
        public void Pause()
        {
            try
            {
                if (audioTrack == null)
                {
                    return;
                }

                if (audRecorder != null && audRecorder.RecordingState == RecordState.Recording)
                {
                    return;
                }

                audioTrack.Pause();
            }
            catch (System.Exception ex)
            {
                Console.WriteLine("Show something I'm giving up on you!!!");
                MessagingCenter.Send <ISoundRecorder, bool>(this, "ErrorWhileReplaying", true);
            }
        }
            Java.Lang.Object DoRun()
            {
                player.vorbis_buffer.SeekRaw(0);
                Status = PlayerStatus.Playing;
                x      = 0;
                total  = 0;

                audio.Play();
                while (!finish)
                {
                    if (pause)
                    {
                        pause = false;
                        audio.Pause();
                        pause_handle.WaitOne();
                        audio.Play();
                    }
                    long size = player.vorbis_buffer.Read(buffer, 0, buffer.Length);
                    if (size <= 0 || size > buffer.Length)
                    {
                        finish = true;
                        if (size < 0)
                        {
                            player.OnPlayerError("vorbis error : {0}", size);
                        }
                        else if (size > buffer.Length)
                        {
                            player.OnPlayerError("buffer overflow : {0}", size);
                        }
                        break;
                    }

                    if (size + total >= loop_end)
                    {
                        size = loop_end - total; // cut down the buffer after loop
                    }
                    total += size;

                    if (++x % 30 == 0)
                    {
                        player.OnProgress(total);
                    }

                    // downgrade bitrate
                    int actualSize = (int)size * 2 / CompressionRate;
                    for (int i = 1; i < actualSize; i++)
                    {
                        buffer [i] = buffer [i * CompressionRate / 2 + (CompressionRate / 2) - 1];
                    }
                    if (size > 0)
                    {
                        audio.Flush();
                        audio.Write(buffer, 0, actualSize);
                    }
                    // loop back to LOOPSTART
                    if (total >= loop_end)
                    {
                        player.vorbis_buffer.SeekPcm(loop_start / 4);  // also faked
                        player.OnLoop(loop_start);
                        total = loop_start;
                    }
                }
                audio.Flush();
                audio.Stop();
                player.OnComplete();
                Status = PlayerStatus.Stopped;
                return(null);
            }