Play audio/video from base64 in Xamarin

Neeraj Goel 21 Reputation points
2020-12-16T11:50:20.577+00:00

I have stored video/audio as base64 code in DB on server now i need to play video/audio from base64 which i will get through web-api. Please help me to play audio/video through base64 code.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,296 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,661 Reputation points Microsoft Vendor
    2020-12-16T13:19:46.087+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    You can decode your base64 string to Byte[], then play it.

    For example, In android, you can use MediaPlayer to play it. I used depdence service to make an test. If I want to play the audio, I do not have DB to store the base64 string, So I put the base64 string to assets folder called AboutAssets.txt, read base64 string to content

       public class PlayService: IMyPlay  
           {  
         
               public void PlayLoadService()  
               {  
       //I do not have DB to store the base64 string, So I put the base64 string to assets folder called `AboutAssets.txt`, read base64 string to `content`  
                   string content;  
                   AssetManager assets = Android.App.Application.Context.Assets;  
                   using (StreamReader sr = new StreamReader(assets.Open("AboutAssets.txt")))  
                   {  
                       content = sr.ReadToEnd();  
                   }  
         
                   byte[] decodedString = Base64.Decode(content,Base64Flags.Default);  
         
       //play it  
                   Play(decodedString);  
                  
               }  
         
               MediaPlayer currentPlayer;  
               public void Play(byte[] AudioFile)  
               {  
                   Stop();  
                   currentPlayer = new MediaPlayer();  
                   currentPlayer.Prepared += (sender, e) =>  
                   {  
                       currentPlayer.Start();  
                   };  
                   currentPlayer.SetDataSource(new StreamMediaDataSource(new System.IO.MemoryStream(AudioFile)));  
                   currentPlayer.Prepare();  
               }  
         
               void Stop()  
               {  
                   if (currentPlayer == null)  
                       return;  
         
                   currentPlayer.Stop();  
                   currentPlayer.Dispose();  
                   currentPlayer = null;  
               }  
           }  
         
           public class StreamMediaDataSource : MediaDataSource  
           {  
               System.IO.Stream data;  
         
               public StreamMediaDataSource(System.IO.Stream Data)  
               {  
                   data = Data;  
               }  
         
               public override long Size  
               {  
                   get  
                   {  
                       return data.Length;  
                   }  
               }  
         
               public override int ReadAt(long position, byte[] buffer, int offset, int size)  
               {  
                   data.Seek(position, System.IO.SeekOrigin.Begin);  
                   return data.Read(buffer, offset, size);  
               }  
         
               public override void Close()  
               {  
                   if (data != null)  
                   {  
                       data.Dispose();  
                       data = null;  
                   }  
               }  
         
               protected override void Dispose(bool disposing)  
               {  
                   base.Dispose(disposing);  
         
                   if (data != null)  
                   {  
                       data.Dispose();  
                       data = null;  
                   }  
               }  
           }  
    

    For IOS, you can refer to this thread:

    https://forums.xamarin.com/discussion/42640/how-to-record-and-play-audio-in-xamarin-ios-using-bytes-array

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful