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:
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.
@Neeraj Goel 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