Beispiel #1
0
        /// <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();

                Result loadResult = Result.Ok;
                await Task.Run(() =>
                {
                    PlayerState = PlayerState.Stopped;
                    LoadMusicAction(); //loads the respective stream
                    //load the stream into the channel but don't play it yet.
                    loadResult = _fmodSys.PlaySound(_fmodSound, null, true, out _fmodChannel);
                });

                //get and update length of the track.
                Length = TimeSpan.FromMilliseconds(_fmodSound.LengthInMilliseconds).TotalSeconds;

                //set the channel callback for all the syncpoints
                loadResult = _fmodChannel.SetCallback(_channelEndCallback);

                //add all the sync points
                //1. when song ends
                loadResult = _fmodSound.AddSyncPoint(_fmodSound.LengthInMilliseconds, TimeUnit.Ms, "songended", out _endSyncPoint);

                //2. when song has reached the last 15 seconds
                loadResult = _fmodSound.AddSyncPoint(_fmodSound.LengthInMilliseconds - 15000, TimeUnit.Ms, "songabouttoended", out _last15SyncPoint);

                //3. when song has reached the last 5 seconds
                loadResult = _fmodSound.AddSyncPoint(_fmodSound.LengthInMilliseconds - 5000, TimeUnit.Ms, "fade", out _last5SyncPoint);

                //update the system once here so that
                //all the sync points and callbacks are saved and updated.
                loadResult = _fmodSys.Update();

                await InitializeSwitch.Dispatcher.RunAsync(() =>
                {
                    if (Equalizer == null)
                    {
                        Equalizer = new FmodEqualizer(_fmodSys, _fmodChannel);
                    }
                    else
                    {
                        (Equalizer as FmodEqualizer).ReInit(_fmodSys, _fmodChannel);
                    }
                    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);
            }
        }
        public async Task <bool> Load(Mediafile mediaFile)
        {
            if (mediaFile != null && mediaFile.Length != "00:00")
            {
                //tell all listeners that we are about to change media
                await InitializeCore.Dispatcher.RunAsync(() => { MediaChanging?.Invoke(this, new EventArgs()); });

                //stop currently playing track and free the channel
                await Stop();

                //create a stream of the new track
                Result loadResult = _fmodSys.CreateStream(mediaFile.Path, _isMobile ? Mode.LowMem & Mode.IgnoreTags : Mode.Default, out _fmodSound);

                //load the stream into the channel but don't play it yet.
                loadResult = _fmodSys.PlaySound(_fmodSound, null, true, out _fmodChannel);

                //this checks if looping is enabled and then sets the loop
                SetLoop();

                //START EXPERIMENT!
                //volume normalization code.
                //FMODSys.CreateDSPByType(Fmod.CoreDSP.DspType.NORMALIZE, out DSP dsp);

                //FMODChannel.addDSP(ChannelControlDspIndex.HEAD, dsp);

                //dsp.setParameterFloat((int)Fmod.CoreDSP.DspNormalize.THRESHHOLD, 1.0f);
                //dsp.setParameterFloat((int)Fmod.CoreDSP.DspNormalize.MAXAMP, 2.0f);

                //dsp.setActive(true);
                //END EXPERIMENT!

                //load equalizer
                if (Equalizer == null)
                {
                    Equalizer = new FmodEqualizer(_fmodSys, _fmodChannel);
                }
                else
                {
                    (Equalizer as FmodEqualizer).ReInit(_fmodSys, _fmodChannel);
                }

                //get and update length of the track.
                Length = TimeSpan.FromMilliseconds(_fmodSound.LengthInMilliseconds).TotalSeconds;

                //set the channel callback for all the syncpoints
                loadResult = _fmodChannel.SetCallback(_channelEndCallback);

                //add all the sync points
                //1. when song ends
                loadResult = _fmodSound.AddSyncPoint(_fmodSound.LengthInMilliseconds, TimeUnit.Ms, "songended", out _endSyncPoint);

                //2. when song has reached the last 15 seconds
                loadResult = _fmodSound.AddSyncPoint(_fmodSound.LengthInMilliseconds - 15000, TimeUnit.Ms, "songabouttoended", out _last15SyncPoint);

                //3. when song has reached the last 5 seconds
                loadResult = _fmodSound.AddSyncPoint(_fmodSound.LengthInMilliseconds - 5000, TimeUnit.Ms, "fade", out _last5SyncPoint);

                //update the system once here so that
                //all the sync points and callbacks are saved and updated.
                loadResult = _fmodSys.Update();

                PlayerState          = PlayerState.Stopped;
                CurrentlyPlayingFile = mediaFile;

                //check if all was successful
                return(loadResult == Result.Ok);
            }
            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);
        }