protected void Init(MusicStream Wave) { _mode = (AudioTrackMode)Wave.Mode; if (_mode == AudioTrackMode.Static) { _player = new AudioTrack( // Stream type (Android.Media.Stream)Wave.Type, // Frequency Wave.SampleRate, // Mono or stereo (ChannelOut)Wave.Config, // Audio encoding (Encoding)Wave.Format, // Length of the audio clip. (int)Wave.SizeInBytes, // Mode. Stream or static. AudioTrackMode.Static); } else { _player = new AudioTrack( // Stream type (Android.Media.Stream)Wave.Type, // Frequency Wave.SampleRate, // Mono or stereo (ChannelOut)Wave.Config, // Audio encoding (Encoding)Wave.Format, // Length of the audio clip. _buffersize = AudioTrack.GetMinBufferSize(Wave.SampleRate, (ChannelOut)Wave.Config, (Encoding)Wave.Format), // Mode. Stream or static. AudioTrackMode.Stream); } _duration = Wave.Duration; _loop = Wave.Loop; _frames = Wave.Samples; _player.SetVolume(_volume = Wave.Volume); _player.SetNotificationMarkerPosition(_frames * 31 / 32); if (_mode == AudioTrackMode.Static) { _player.Write(Wave.Content.ReadFully(true), 0, (int)Wave.Content.Length); } else { Set((sender, e) => { if (_loop) { _player.Release(); Init(_Wave); Write(); } ; }); _content = Wave.Content.ReadFully(true); } _prepared = true; }
protected void Init(MusicStream Wave) {// To get preferred buffer size and sampling rate. AudioManager audioManager = (AudioManager) Forms.Context.GetSystemService(Android.Content.Context.AudioService); Rate = int.Parse(audioManager.GetProperty(AudioManager.PropertyOutputSampleRate)); //string Size = audioManager.GetProperty(AudioManager.PropertyOutputFramesPerBuffer); SampleRate = Wave.SampleRate; _content = Wave.Content; int SizeInBytes = Wave.SizeInBytes - 44; _player = new AudioTrack( // Stream type (Android.Media.Stream)Wave.Type, // Frequency Rate, // Mono or stereo (ChannelOut)Wave.Config, // Audio encoding (Encoding)Wave.Format, // Length of the audio clip. SizeInBytes, // Mode. Stream or static. (AudioTrackMode)Wave.Mode); _loop = Wave.Loop; _volume = Wave.Volume; _player.SetVolume(_volume = Wave.Volume); #if true //int ch = Wave.Channels; //_start = 0;// (int)Wave.Content.Length / ch; //_stop = (int)Wave.Content.Length;// / ch / 2 / 2 + 16000 #elif false _player.SetNotificationMarkerPosition(SizeInBytes / 2); _player.MarkerReached += (object sender, AudioTrack.MarkerReachedEventArgs e) => { if (_loop) { e.Track.SetPlaybackHeadPosition(0); } }; #elif false Device.StartTimer(Wave.Duration, () => { if (_loop) { _player.SetPlaybackHeadPosition(0); } return(_loop); }); #endif _prepared = true; }
protected void Init(Stream Content, bool Loop, float Volume) { _content = Content; _player = new AudioTrack( // Stream type Android.Media.Stream.Music, // Frequency 11025, // Mono or stereo ChannelOut.Mono, // Audio encoding Encoding.Pcm16bit, // Length of the audio clip. (int)Content.Length, // Mode. Stream or static. AudioTrackMode.Stream); _loop = Loop; _volume = Volume; _player.SetVolume(_volume = Volume); _player.SetNotificationMarkerPosition((int)Content.Length / 2); _prepared = true; }