Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.IO;
- using System.Threading;
- using Android.App;
- using Android.Content;
- using Android.OS;
- using Android.Runtime;
- using Android.Views;
- using Android.Widget;
- using Android.Media;
- using System.Threading.Tasks;
- namespace Main
- {
- /// <summary>
- /// Le file paths:
- /// http://167.88.113.131:8000/;stream.mp3
- /// http://167.88.113.131:8000/listen.pls?sid=1
- /// </summary>
- [Activity (Label = "Main", MainLauncher = true, Icon = "@drawable/icon")]
- public class MainActivity : Activity
- {
- protected override void OnCreate (Bundle bundle)
- {
- LowLevelPlayAudio instance = new LowLevelPlayAudio(); //Instantiate your LowLevelPlayAudio here
- 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 button = FindViewById<Button> (Resource.Id.myButton);
- if (button.Enabled == true) {
- instance.StartAsync(); //Call on your method here and pass any parameters if they exist
- }
- }
- //Class from xamarin fourms to play with
- class LowLevelPlayAudio
- {
- static string filePath = "http://167.88.113.131:8000/;stream.mp3";
- byte[] buffer = null;
- AudioTrack audioTrack = null;
- public async Task PlaybackAsync ()
- {
- FileStream fileStream = new FileStream (filePath, FileMode.Open, FileAccess.Read);
- BinaryReader binaryReader = new BinaryReader (fileStream);
- long totalBytes = new System.IO.FileInfo (filePath).Length;
- buffer = binaryReader.ReadBytes ((Int32)totalBytes);
- fileStream.Close ();
- fileStream.Dispose ();
- binaryReader.Close ();
- await PlayAudioTrackAsync ();
- }
- protected async Task PlayAudioTrackAsync ()
- {
- audioTrack = new AudioTrack (
- // Stream type
- Android.Media.Stream.Music,
- // Frequency
- 128000,
- // Mono or stereo
- ChannelConfiguration.Stereo,
- // Audio encoding
- Android.Media.Encoding.Pcm16bit,
- // Length of the audio clip.
- buffer.Length,
- // Mode. Stream or static.
- AudioTrackMode.Stream);
- audioTrack.Play ();
- await audioTrack.WriteAsync (buffer, 0, buffer.Length);
- }
- public async Task StartAsync ()
- {
- await PlaybackAsync ();
- }
- public void Stop ()
- {
- if (audioTrack != null) {
- audioTrack.Stop ();
- audioTrack.Release ();
- audioTrack = null;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement