question

neerajgoel avatar image
0 Votes"
neerajgoel asked LeonLu-MSFT commented

Play audio/video from base64 in Xamarin

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.

dotnet-xamarinformsdotnet-androiddotnet-ios
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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.


· 2
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thanks @LeonLu-MSFT for your reply and it will work fine for Audio.
Plz help me to provide solution for playing video by base64 format as well

0 Votes 0 ·

@neerajgoel For Video, I do not advice you to convert video to base64, it will cost lots of menory when you load it. Normally, we store video's url(You can update your video to your server) in the DB, when we want to play it, we can use XamarinMediaManager to load it.https://github.com/Baseflow/XamarinMediaManager, If you still want to use base64, I found the only way is convert video to stream, then write your video to the local storeage. Use XamarinMediaManager to play the video like this thread:https://stackoverflow.com/questions/59723841/xamarin-forms-mediamanager-play-video-from-local-storage

byte[] decodedString = Base64.Decode(content,Base64Flags.Default);
 Stream stream = new MemoryStream(decodedString);

0 Votes 0 ·