コード例 #1
0
        /// <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);
        }
コード例 #2
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();

                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);
            }
        }
コード例 #3
0
 internal void RaiseMediaChangingEvent(MediaOptions options, MediaInfo mediaInfo) =>
 MediaChanging?.Invoke(this, new MediaOpeningEventArgs(options, mediaInfo));
コード例 #4
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);
            }
        }
コード例 #5
0
        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);
        }