public AudioPlayerNative() { player = new Android.Media.MediaPlayer(); player.Prepared += (s, e) => { prepare = true; }; player.SetScreenOnWhilePlaying(true); timer = new System.Timers.Timer(1000) { AutoReset = true }; timer.Elapsed += (s, e) => { if (SeekPositionChanged != null) { SeekPositionChanged.Invoke(this, player.CurrentPosition); } var timeDiff = (player.Duration - player.CurrentPosition); if (timeDiff < 1000 && player.Duration != 1886146040) { if (Completed != null) { timer.Stop(); Completed.Invoke(this, null); } } }; }
public void LoadAsync(string source) { AVAsset asset; if (string.IsNullOrWhiteSpace(source)) { return; } _prepared = false; if (audioPlayer != null) { audioPlayer.Dispose(); audioPlayer = null; } _prepared = false; _timer = new System.Timers.Timer(1000) { AutoReset = true }; if (source.StartsWith("http://", StringComparison.CurrentCultureIgnoreCase) || source.StartsWith("https://", StringComparison.CurrentCultureIgnoreCase)) { asset = AVAsset.FromUrl(NSUrl.FromString(source)); } else { asset = AVAsset.FromUrl(NSUrl.FromFilename(source)); } var playerItem = new AVPlayerItem(asset); audioPlayer = new AVPlayer(playerItem); NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.DidPlayToEndTimeNotification, DidVideoFinishPlaying, playerItem); //NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.ItemFailedToPlayToEndTimeNotification, DidVideoErrorOcurred, playerItem); //NSNotificationCenter.DefaultCenter.AddObserver(AVPlayerItem.NewErrorLogEntryNotification, DidVideoErrorOcurred, playerItem); _timer.Elapsed += (s, e) => { if (SeekPositionChanged != null) { SeekPositionChanged.Invoke(this, CurrentPosition); } }; _prepared = true; SetTotalDuration(); }