コード例 #1
0
ファイル: Audio.cs プロジェクト: s1080170/android
        /*********************************************************************************
        *
        *
        *********************************************************************************/
        public void ButtonPlay_Click(object sender, EventArgs e)
        {
            //String musicFolder = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryMusic).Path;
            String filePath = mRecFolder + "/sample_mono_8k8bit.wav";

            //String filePath = musicFolder + "/sample_stereo_44k16bit.wav";

            System.Diagnostics.Debug.WriteLine(filePath);

            File            file        = new File(filePath);
            FileInputStream inputStream = new FileInputStream(file);

            // Streamモードで再生を行うので、リングバッファサイズを取得
            Int32 bufferSize = AudioTrack.GetMinBufferSize(mSamplingRate, ChannelOut.Mono, mFormat);

            System.Diagnostics.Debug.WriteLine("AudioTrack : GetMinBufferSize={0}", bufferSize);

            // Frame size
            TrackBuffer.Instance.Frames = mFrameSize;

            // AudioTrackを生成する
            mAudioTrack = new AudioTrack(
                Stream.Music,
                //Stream.VoiceCall,
                mSamplingRate,
                ChannelOut.Mono,
                mFormat,
                bufferSize,
                AudioTrackMode.Stream);

            // コールバックを指定
            mAudioTrack.SetPlaybackPositionUpdateListener(new OnPlaybackPositionUpdateListener());

            //通知の発生するフレーム数を指定
            mAudioTrack.SetPositionNotificationPeriod(TrackBuffer.Instance.Frames);

            TrackBuffer.Instance.Clear();

            Task.Run(() => {
                while (true)
                {
                    if (TrackBuffer.Instance.Count > 5)
                    {
                        break;
                    }
                }

                System.Diagnostics.Debug.WriteLine("AudioTrack play streaming data");
                mAudioTrack.Play();

                Byte[] wav = null;
                wav        = TrackBuffer.Instance.Dequeue(); mAudioTrack.Write(wav, 0, wav.Length);
                wav        = TrackBuffer.Instance.Dequeue(); mAudioTrack.Write(wav, 0, wav.Length);
                wav        = TrackBuffer.Instance.Dequeue(); mAudioTrack.Write(wav, 0, wav.Length);
                wav        = TrackBuffer.Instance.Dequeue(); mAudioTrack.Write(wav, 0, wav.Length);
            });
        }