I am in Blazor server and after I click on on my audio tag, it does not play any sound. However after I click on it on Android or on Home windows PC, it really works properly.
That is my code:
@if (Query.Parcours.Kind == Constants.TypeLanguage)
{
@if (FileAudio != null)
{
@if (ErrorMessage != null)
{
@ErrorMessage
}
}
@code {
[Parameter] public QuestionDto Query { get; set; }
[Parameter] public bool Managing { get; set; } = true;
[Inject] IJSRuntime JS { get; set; }
non-public byte[] audioBytes;
non-public bool recording = false;
non-public string? FileAudio { get; set; }
non-public bool DisableStopRecoredingBtn { get; set; } = false;
non-public string AudioDirectory = Path.Mix("Fichiers", "AudiosLangue");
non-public string? ErrorMessage { get; set; } = null;
non-public string MessagePermissionDenied = "Vous devez permettre à l'software d'accéder à votre micro!";
protected override async Job OnInitializedAsync()
{
if (Query?.AudioFilePath != null)
{
var audioStream = GetAudioFileStream(Query.AudioFilePath);
if (audioStream != null)
FileAudio = await GetBase64Audio(audioStream);
}
}
non-public async Job StartRecording()
{
strive
{
ErrorMessage = null;
await JS.InvokeVoidAsync("startRecording");
recording = true;
StateHasChanged();
await Job.Delay(60000);
if (recording)
{
await StopRecording();
}
}
catch (Exception ex)
{
ErrorMessage = MessagePermissionDenied;
}
}
non-public async Job StopRecording()
{
strive
{
DisableStopRecoredingBtn = true;
audioBytes = await JS.InvokeAsync("stopRecording");
const int maxFileSize = 5 * 1024 * 1024;
if (audioBytes.Size > maxFileSize)
{
ErrorMessage = "Le fichier est trop gros, le most est de 5 MB.";
return;
}
if (!Listing.Exists(AudioDirectory))
{
Listing.CreateDirectory(AudioDirectory);
}
var randomFileName = $"P{Query.ParcoursId}-Q{Query.Id}-{Path.GetRandomFileName()}.mp3";
var filePath = Path.Mix(AudioDirectory, randomFileName);
if (File.Exists(filePath))
{
File.Delete(filePath);
}
await File.WriteAllBytesAsync(filePath, audioBytes);
Query.AudioFilePath = filePath;
var fileStream = GetAudioFileStream(filePath);
FileAudio = await GetBase64Audio(fileStream);
recording = false;
DisableStopRecoredingBtn = false;
StateHasChanged();
}
catch (Exception ex)
{
ErrorMessage = MessagePermissionDenied;
}
}
non-public Stream GetAudioFileStream(string filePath)
{
if (File.Exists(filePath))
{
return new FileStream(filePath, FileMode.Open, FileAccess.Learn);
}
return null;
}
non-public async Job GetBase64Audio(Stream stream)
{
utilizing (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
byte[] byteArray = memoryStream.ToArray();
string base64String = Convert.ToBase64String(byteArray);
return $"knowledge:audio/mp3;base64,{base64String}";
}
}
}
So for taking part in the sound with the audio tag it does not work. Are you able to assist me? Surprisingly, after I document on IOS I can click on the audio and it performs the sound properly. Plainly the get file does not work? Once I register I can heard the sound. But when I come again and the audio file shouldn’t be right here and have to be loaded, this does not work on ios. Do I do wrongly my convertion for Ios?