Wrong Time and Position
Environment:
Platform: Xamarin Forms
LibVLCSharp version: 3.6.7
LibVLCSharp.Forms version: 3.6.7
I am trying to run audio from firebase but I am getting an issue that the Time in TimeChanged Callback stays 250 and never changes. The audio file is working properly on device music player when downloaded and its seekbar is also working. Issue:
Following is the code for creating audio file on Android: Code:
string GetAppFolder()
{
try
{
// var path = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath;
var path = System.IO.Path.Combine(AndroidApp.Context.GetExternalFilesDir(null).AbsolutePath, "App/Audio");
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
return path;
}
catch (Exception ex)
{
return null;
}
}
public void RecordAudio()
{
try
{
if (!String.IsNullOrEmpty(GetAppFolder()))
{
string folder = GetAppFolder();
Path = System.IO.Path.Combine(folder, DateTime.Now.ToString("yyyyMMddHHmmssffffff") + ".m4a");
recorder = new MediaRecorder();
using (File.Create(Path)) { }
recorder.SetAudioSource(Android.Media.AudioSource.Mic);
recorder.SetOutputFormat(OutputFormat.Mpeg4);
recorder.SetAudioEncoder(AudioEncoder.HeAac);
recorder.SetAudioEncodingBitRate(256000);
recorder.SetAudioSamplingRate(44100);
recorder.SetOutputFile(Path);
recorder.Prepare();
recorder.Start();
IsRecording = true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
And For IOS:
public void RecordAudio()
{
try
{
if (!IsRecording)
{
aVAudioSession = AVAudioSession.SharedInstance();
var err = aVAudioSession.SetCategory(AVAudioSessionCategory.PlayAndRecord);
string fileName = string.Format("Audio{0}.m4a", DateTime.Now.ToString("yyyyMMddHHmmssffffff"));
Path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), fileName);
using (File.Create(Path)) { }
url = NSUrl.FromFilename(Path);
NSObject[] values = new NSObject[]
{
NSNumber.FromFloat(44100f),//(12000f),//Sampling rate
NSNumber.FromInt32 ((int)AudioToolbox.AudioFormatType.MPEG4AAC),//AVFormat
NSNumber.FromInt32 (1), //Channels
NSNumber.FromInt32 (16), //PCMBitDepth
NSNumber.FromBoolean (false), //IsBigEndianKey
NSNumber.FromBoolean (false), //IsFloatKey
NSNumber.FromInt64(256000),//Bit Rate
};
NSObject[] keys = new NSObject[]
{
AVAudioSettings.AVSampleRateKey,
AVAudioSettings.AVFormatIDKey,
AVAudioSettings.AVNumberOfChannelsKey,
AVAudioSettings.AVLinearPCMBitDepthKey,
AVAudioSettings.AVLinearPCMIsBigEndianKey,
AVAudioSettings.AVLinearPCMIsFloatKey,
AVAudioSettings.AVEncoderBitRateKey
};
settings = NSDictionary.FromObjectsAndKeys(values, keys);
recorder = AVAudioRecorder.Create(url, new AudioSettings(settings), out error);
recorder.PrepareToRecord();
recorder.Record();
IsRecording = true;
}
}
catch (Exception ex)
{
Console.WriteLine(ex);
throw;
}
}
Following is the code I used to upload to Firebase:
using (var fileStream = new FileStream(_filePath, FileMode.Open, FileAccess.Read))
{
var uploadTask = new FirebaseStorage("app.appspot.com", new FirebaseStorageOptions()
{
HttpClientTimeout = new TimeSpan(1, 0, 0)
})
.Child("Test")
.Child(TimelineType.Audio.ToString())
.Child(Guid.NewGuid().ToString() + "." + extension)
.PutAsync(fileStream);
}
And Following is The code where I am setting up the VLC Player:
void setAudio(string url)
{
try
{
_videoView = new VideoView { HorizontalOptions = LayoutOptions.FillAndExpand, VerticalOptions = LayoutOptions.FillAndExpand };
MainGrid.Children.Add(_videoView);
var _frameView = new Frame { BackgroundColor = Color.White, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, Padding = 20, CornerRadius = 70};
_imageview = new Image { IsVisible = true, HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center, HeightRequest=100, WidthRequest=100 };
_imageview.Source = ImageSource.FromFile("audio_placeholder_1.png");
_frameView.Content= _imageview;
MainGrid.Children.Add(_frameView);
_indicatorview = new ActivityIndicator { HorizontalOptions = LayoutOptions.Center, VerticalOptions = LayoutOptions.Center };
MainGrid.Children.Add(_indicatorview);
_videoView.MediaPlayerChanged += MediaPlayerChanged;
using var media = new Media(_libVLC, new Uri(url));
{
_mediaPlayer.Media = media;
}
_videoView.MediaPlayer = _mediaPlayer;
_videoView.MediaPlayer.PositionChanged += MediaPlayer_PositionChanged;
_videoView.MediaPlayer.EncounteredError += MediaPlayer_EncounteredError;
_videoView.MediaPlayer.EndReached += MediaPlayer_EndReached;
_videoView.MediaPlayer.Buffering += MediaPlayer_Buffering; ;
}
catch (System.Exception)
{
}
}
Expected Behaviour:
The player should return correct time and position for the audio.