public void PlaySound(int samplingRate, byte[] pcmData)
        {
            if (previousAudioTrack != null)
            {
                previousAudioTrack.Stop();
                previousAudioTrack.Release();
            }

            AudioTrack audioTrack = new AudioTrack(Stream.Music,
                                                   samplingRate,
                                                   ChannelOut.Mono,
                                                   Android.Media.Encoding.Pcm16bit,
                                                   pcmData.Length * sizeof(short),
                                                   AudioTrackMode.Static);

            audioTrack.Write(pcmData, 0, pcmData.Length);
            audioTrack.Play();

            previousAudioTrack = audioTrack;
        }
        protected void PlayAudioTrack()
        {
            audioTrack = new AudioTrack (
                // Stream type
                Android.Media.Stream.Music,
                // Frequency
                11025,
                // Mono or stereo
                ChannelConfiguration.Mono,
                // Audio encoding
                Android.Media.Encoding.Pcm16bit,
                // Length of the audio clip.
                buffer.Length,
                // Mode. Stream or static.
                AudioTrackMode.Stream);

            audioTrack.Play ();

            audioTrack.Write (buffer, 0, buffer.Length);
        }
예제 #3
0
        protected Task PlayAudioTrackAsync()
        {
            return new Task(() => {
            audioTrack = new AudioTrack (
                // Stream type
                Android.Media.Stream.Music,
                // Frequency
                44100,
                // Mono or stereo
                ChannelConfiguration.Mono,
                // Audio encoding
                Android.Media.Encoding.Pcm16bit,
                // Length of the audio clip.
                RecordAudio.fullAudioBuffer.Count,
                // Mode. Stream or static.
                AudioTrackMode.Stream);

            audioTrack.Play ();

                audioTrack.Write(RecordAudio.fullAudioBuffer.ToArray(), 0, RecordAudio.fullAudioBuffer.Count);
            });
        }
        private void PlayMetronome()
        {
            const int amp = 10000;
            double twopi = 8*Math.Atan(1.0);
            const double fr = 440.0;
            double ph = 0.0;

            int lastBpm = metronCurrentBpm;

            Animation anim = new AlphaAnimation(0.5f, 1.0f);
            anim.Duration = (60000/metronCurrentBpm)/2;
            anim.StartOffset = 0;
            anim.RepeatMode = RepeatMode.Reverse;
            anim.RepeatCount = Animation.Infinite;
            RunOnUiThread(() => { metronBpmText.StartAnimation(anim); });

            metronAudioTrack = new AudioTrack(Android.Media.Stream.Music, 44100, ChannelOut.Mono,
                Encoding.Pcm16bit, metronBuffSize, AudioTrackMode.Stream);

            metronAudioTrack.Play();

            while (reading)
            {
                Thread.Sleep(60000/metronCurrentBpm);

                if (lastBpm != metronCurrentBpm)
                {
                    // The BPM has changed - change the animation speed!
                    lastBpm = metronCurrentBpm;
                    anim.Duration = (60000/metronCurrentBpm)/2;

                    RunOnUiThread(() =>
                    {
                        metronBpmText.ClearAnimation();
                        metronBpmText.StartAnimation(anim);
                    });
                }

                for (int i = 0; i < metronAudioBuffer.Length; i++)
                {
                    metronAudioBuffer[i] = (short) (amp*Math.Sin(ph));
                    ph += twopi*fr/44100;
                }

                metronAudioTrack.Write(metronAudioBuffer, 0, metronAudioBuffer.Length);
            }

            metronAudioTrack.Stop();
            metronAudioTrack.Release();

            RunOnUiThread(() => { metronBpmText.ClearAnimation(); });
        }
예제 #5
0
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate (bundle);

            // Set our view from the "main" layout resource
            SetContentView (Resource.Layout.Main);

            // Get our button from the layout resource,
            // and attach an event to it
            Button buttonRec = FindViewById<Button> (Resource.Id.myButton);
            Button buttonPlay = FindViewById<Button> (Resource.Id.btnPlay);

            ar = findAudioRecord ();
            audioBuffer = new Int16[bufferSize];
            //ar.Release ();

            buttonRec.Click += delegate {

                ar.StartRecording();
                while (true) {
                    try
                    {
                        // Keep reading the buffer
                        //while there is audio input.
                        ar.Read(
                            audioBuffer,
                            0,
                            audioBuffer.Length);

                        if(count++ > audioBuffer.Length)
                        {
                            ar.Stop();
                            break;
                        }
                        // Write out the audio file.
                    }
                    catch (Exception ex)
                    {
                        Console.Out.WriteLine(ex.Message);
                        break;
                    }
                }
            };

            buttonPlay.Click += (sender, e) =>
            {
                int minimumBufferSize = AudioTrack.GetMinBufferSize(ar.SampleRate, ChannelOut.Mono, Android.Media.Encoding.Pcm16bit);

                 audioTrack = new AudioTrack(
                    // Stream type
                    Android.Media.Stream.Music,
                    // Frequency
                    ar.SampleRate,
                    // Mono or stereo
                    ChannelConfiguration.Mono,
                    // Audio encoding
                    Android.Media.Encoding.Pcm16bit,
                    // Length of the audio clip.
                    (minimumBufferSize < audioBuffer.Length ? audioBuffer.Length : minimumBufferSize),
                    // Mode. Stream or static.
                    AudioTrackMode.Static);

                audioTrack.Play();
                audioTrack.Write(audioBuffer, 0, audioBuffer.Length);
            };
        }
 void WriteValue(AudioTrack at, byte[] val, int times = 1)
 {
     for (int i = 0; i < times; i++)
         at.Write(val, 0, val.Length);
 }
예제 #7
0
        private void Click_Play(object sender, EventArgs e)
        {
            if (_isPlaying)
            {          
                _audioTrack.Stop();
                _isPlaying = false;
                _play.SetImageResource(Resource.Drawable.play);
                return;
            }

            _play.SetImageResource(Resource.Drawable.stop);

            var byteArr = _audioData.ToArray();

            int sampleRate = 0;
            Android.Media.Encoding audioFormat = Android.Media.Encoding.Pcm16bit;
            ChannelOut channelConfig = ChannelOut.Stereo;
            int bufferLength = 0;
            _audioTrack = AudioHelper.FindAudioTrack(ref sampleRate, ref audioFormat, ref channelConfig, ref bufferLength);

            _isPlaying = true;

            (new TaskFactory()).StartNew(() =>
                {
                    _audioTrack.Play();
                    _audioTrack.Write(byteArr, 0, byteArr.Length);
                    _activity.RunOnUiThread(() =>
                        {
                            _isPlaying = false;
                            _play.SetImageResource(Resource.Drawable.play);
                        });
                });
        }