Advertisement
Guest User

domacikolaci code

a guest
Dec 27th, 2014
746
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Threading;
  7. using Android.App;
  8. using Android.Content;
  9. using Android.OS;
  10. using Android.Runtime;
  11. using Android.Views;
  12. using Android.Widget;
  13. using Android.Media;
  14. using System.Threading.Tasks;
  15. namespace Main
  16. {
  17.  
  18.     /// <summary>
  19.     /// Le file paths:
  20.     /// http://167.88.113.131:8000/;stream.mp3
  21.     /// http://167.88.113.131:8000/listen.pls?sid=1
  22.     /// </summary>
  23.  
  24.     [Activity (Label = "Main", MainLauncher = true, Icon = "@drawable/icon")]
  25.     public class MainActivity : Activity
  26.     {
  27.  
  28.         protected override void OnCreate (Bundle bundle)
  29.         {
  30.             LowLevelPlayAudio instance = new LowLevelPlayAudio(); //Instantiate your LowLevelPlayAudio here
  31.  
  32.             base.OnCreate (bundle);
  33.  
  34.             // Set our view from the "main" layout resource
  35.             SetContentView (Resource.Layout.Main);
  36.  
  37.             // Get our button from the layout resource,
  38.             // and attach an event to it
  39.             Button button = FindViewById<Button> (Resource.Id.myButton);
  40.  
  41.             if (button.Enabled == true) {
  42.                 instance.StartAsync(); //Call on your method here and pass any parameters if they exist
  43.             }
  44.            
  45.                     }
  46.  
  47.  
  48.         //Class from xamarin fourms to play with
  49.         class LowLevelPlayAudio
  50.         {
  51.             static string filePath = "http://167.88.113.131:8000/;stream.mp3";
  52.             byte[] buffer = null;
  53.             AudioTrack audioTrack = null;
  54.  
  55.             public async Task PlaybackAsync ()
  56.             {
  57.                 FileStream fileStream = new FileStream (filePath, FileMode.Open, FileAccess.Read);
  58.                 BinaryReader binaryReader = new BinaryReader (fileStream);
  59.                 long totalBytes = new System.IO.FileInfo (filePath).Length;
  60.                 buffer = binaryReader.ReadBytes ((Int32)totalBytes);
  61.                 fileStream.Close ();
  62.                 fileStream.Dispose ();
  63.                 binaryReader.Close ();
  64.                 await PlayAudioTrackAsync ();
  65.             }
  66.  
  67.  
  68.             protected async Task PlayAudioTrackAsync ()
  69.             {
  70.                 audioTrack = new AudioTrack (
  71.                     // Stream type
  72.                     Android.Media.Stream.Music,
  73.                     // Frequency
  74.                     128000,
  75.                     // Mono or stereo
  76.                     ChannelConfiguration.Stereo,
  77.                     // Audio encoding
  78.                     Android.Media.Encoding.Pcm16bit,
  79.                     // Length of the audio clip.
  80.                     buffer.Length,
  81.                     // Mode. Stream or static.
  82.                     AudioTrackMode.Stream);
  83.  
  84.                 audioTrack.Play ();
  85.  
  86.                 await audioTrack.WriteAsync (buffer, 0, buffer.Length);
  87.             }
  88.  
  89.             public async Task StartAsync ()
  90.             {
  91.                 await PlaybackAsync ();
  92.             }
  93.  
  94.             public void Stop ()
  95.             {
  96.                 if (audioTrack != null) {
  97.                     audioTrack.Stop ();
  98.                     audioTrack.Release ();
  99.                     audioTrack = null;
  100.                 }
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement