/* //play short tone on cennection * private void websocketClient_Opened(object sender, EventArgs e) * { * * websocketClient.Send("2000"); * Thread.Sleep(333); * websocketClient.Send("2000"); * * } */ private void websocketClient_MessageReceived(object sender, MessageReceivedEventArgs e) { if (!isPlaying) { byte[] GeneratedSnd = CreateSound(int.Parse(e.Message)); track = new AudioTrack(Stream.Music, 8000, ChannelOut.Mono, Encoding.Pcm16bit, 80000, AudioTrackMode.Static); prevTone = int.Parse(e.Message); isPlaying = !isPlaying; track.Write(GeneratedSnd, 0, 80000); try { track.Play(); } catch (Java.Lang.IllegalStateException) { track.Flush(); track.Release(); } } else if (isPlaying && prevTone != int.Parse(e.Message)) { isPlaying = !isPlaying; track.Stop(); track.Flush(); track.Release(); byte[] GeneratedSnd = CreateSound(int.Parse(e.Message)); track = new AudioTrack(Stream.Music, 8000, ChannelOut.Mono, Encoding.Pcm16bit, 80000, AudioTrackMode.Static); prevTone = int.Parse(e.Message); isPlaying = !isPlaying; track.Write(GeneratedSnd, 0, 80000); try { track.Play(); } catch (Java.Lang.IllegalStateException) { track.Flush(); track.Release(); } } else { isPlaying = !isPlaying; track.Stop(); track.Flush(); track.Release(); } }
public async Task PlayOnce(System.IO.Stream stream) { await Task.Run(() => { try { int sampleRate = 16000; var channel = ChannelOut.Mono; var encoding = Android.Media.Encoding.Pcm16bit; var buffSize = AudioTrack.GetMinBufferSize(sampleRate, channel, encoding); if (_playOnceAudioTrack == null) { _playOnceAudioTrack = new AudioTrack(Stream.Music, sampleRate, channel, encoding, buffSize, AudioTrackMode.Stream); } _playOnceAudioTrack.Stop(); _playOnceAudioTrack.Flush(); var buffer = new byte[stream.Length]; stream.Read(buffer, 0, buffer.Length); _playOnceAudioTrack.Play(); _playOnceAudioTrack.Write(buffer, 0, buffer.Length); } catch (Exception ex) { } }); }
protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle); // Set our view from the "main" layout resource SetContentView(Resource.Layout.Main); Button button = FindViewById <Button>(Resource.Id.button1); button.Text = ("Morse"); //Client connectiong to the server Client client = new Client(); client.Setup("ws://192.168.0.14:8001", "basic", WebSocketVersion.Rfc6455); client.Start(); Random rnd = new Random(); int freq = rnd.Next(100, 2600); byte[] GeneratedSnd = Client.CreateSound(freq); button.Click += delegate { //if handshaking does not happend, trying if (!client.isConncted()) { client.Setup("ws://192.168.0.14:8001", "basic", WebSocketVersion.Rfc6455); client.Start(); } client.SendTone(freq.ToString()); //playing short tone try { AudioTrack track = new AudioTrack(Stream.Music, 8000, ChannelOut.Mono, Encoding.Pcm16bit, 8000, AudioTrackMode.Static); track.Write(GeneratedSnd, 0, (8000 / 3)); track.Play(); Thread.Sleep(333); client.SendTone(freq.ToString()); track.Flush(); track.Release(); //withou flush and realse only 32 track play possible } catch (Java.Lang.IllegalStateException) { client.SendTone(freq.ToString()); } }; button.LongClick += delegate { if (!client.isConncted()) { client.Setup("ws://192.168.0.14:8001", "basic", WebSocketVersion.Rfc6455); client.Start(); } client.SendTone(freq.ToString()); //playing long tone try { AudioTrack track = new AudioTrack(Stream.Music, 8000, ChannelOut.Mono, Encoding.Pcm16bit, 8000, AudioTrackMode.Static); track.Write(GeneratedSnd, 0, 8000); track.Play(); Thread.Sleep(1000); client.SendTone(freq.ToString()); track.Flush(); track.Release(); } catch (Java.Lang.IllegalStateException) { client.SendTone(freq.ToString()); } }; }
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); }