Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Play audio/video from base 64

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.

Answers

  • LeonLuLeonLu Member, Xamarin Team Xamurai

    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


    Xamarin forums are migrating to a new home on Microsoft Q&A!
    We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A!
    For more information, please refer to this sticky post.

Sign In or Register to comment.