/// <summary> /// Loads the specified file into the player. /// </summary> /// <param name="fileName">Path to the music file.</param> /// <returns>Boolean</returns> public async Task <bool> Load(Mediafile mediaFile) { if (mediaFile != null && mediaFile.Length != "00:00") { try { string path = mediaFile.Path; await InitializeCore.Dispatcher.RunAsync(() => { MediaChanging?.Invoke(this, new EventArgs()); }); await Stop(); await Task.Run(() => { _handle = Bass.CreateStream(path, 0, 0, BassFlags.AutoFree | BassFlags.Float); PlayerState = PlayerState.Stopped; Length = 0; Length = Bass.ChannelBytes2Seconds(_handle, Bass.ChannelGetLength(_handle)); Bass.FloatingPointDSP = true; Bass.ChannelSetDevice(_handle, 1); Bass.ChannelSetSync(_handle, SyncFlags.End | SyncFlags.Mixtime, 0, _sync); Bass.ChannelSetSync(_handle, SyncFlags.Position, Bass.ChannelSeconds2Bytes(_handle, Length - 5), _posSync); Bass.ChannelSetSync(_handle, SyncFlags.Position, Bass.ChannelSeconds2Bytes(_handle, Length - 15), _posSync); CurrentlyPlayingFile = mediaFile; }); if (Equalizer == null) { Equalizer = new BassEqualizer(_handle); } else { (Equalizer as BassEqualizer).ReInit(_handle); } MediaStateChanged?.Invoke(this, new MediaStateChangedEventArgs(PlayerState.Stopped)); return(true); } catch (Exception ex) { await InitializeCore.NotificationManager.ShowMessageAsync(ex.Message + "||" + mediaFile.OrginalFilename); } } else { string error = "The file " + mediaFile?.OrginalFilename + " is either corrupt, incomplete or unavailable. \r\n\r\n Exception details: No data available."; if (IgnoreErrors) { await InitializeCore.NotificationManager.ShowMessageAsync(error); } else { await InitializeCore.NotificationManager.ShowMessageBoxAsync(error, "File corrupt"); } } return(false); }
/// <summary> /// Loads the specified file into the player. /// </summary> /// <returns>Boolean</returns> private async Task <bool> LoadMusicAsync(Action LoadMusicAction) { try { await InitializeSwitch.Dispatcher.RunAsync(() => { MediaChanging?.Invoke(this, new EventArgs()); }); await Stop(); await Task.Run(() => { PlayerState = PlayerState.Stopped; LoadMusicAction(); //loads the respective stream if (Length <= 1) { Length = Bass.ChannelBytes2Seconds(_handle, Bass.ChannelGetLength(_handle)); } IsSeekable = Bass.ChannelGetLength(_handle) != -1; Bass.FloatingPointDSP = true; Bass.ChannelSetDevice(_handle, 1); Bass.ChannelSetSync(_handle, SyncFlags.End | SyncFlags.Mixtime, 0, _sync); Bass.ChannelSetSync(_handle, SyncFlags.Position, Bass.ChannelSeconds2Bytes(_handle, Length - 5), _posSync); Bass.ChannelSetSync(_handle, SyncFlags.Position, Bass.ChannelSeconds2Bytes(_handle, Length - 15), _posSync); }); if (InitializeSwitch.IsMobile) { await ChangeDevice(); } if (Equalizer == null) { Equalizer = new BassEqualizer(_handle); } else { (Equalizer as BassEqualizer).ReInit(_handle); } MediaStateChanged?.Invoke(this, new MediaStateChangedEventArgs(PlayerState.Stopped)); return(true); } catch (Exception ex) { BLogger.E("An error occured while loading music. Action {action}", ex, LoadMusicAction.ToString()); await InitializeSwitch.NotificationManager.ShowMessageAsync(ex.Message); return(false); } }