Author Topic: BASSMIX can't play again if stop pressed  (Read 187 times)

noelquipit

  • Posts: 11
BASSMIX can't play again if stop pressed
« on: 15 Aug '19 - 01:25 »
Hi Admin,
Can you help me i copied this code from chere on BASSMIX. at first it will load the and play the music. then you can add more music on the list then continue sequencing all music.

But when everything are done or even pressed the stop. the player cant play anymore music, the loading work but it wont play.  see the code below.

private void buttonAddFile_Click(object sender, EventArgs e)
        {
            if (DialogResult.OK == openFileDialog.ShowDialog(this))
            {
                if (File.Exists(openFileDialog.FileName))
                {
                    lock (listBoxPlaylist)
                    {
                        Track track = new Track(openFileDialog.FileName, 0);
                        listBoxPlaylist.Items.Add(track);

                        // in the demo we already add each new track to the mixer
                        // this is in real life not the best place to do so (especially with larger playlists)
                        // but for the demo it is okay ;-)

                        // add the new track to the mixer (in PAUSED mode!)
                        BassMix.BASS_Mixer_StreamAddChannel(_mixer, track.Channel, BASSFlag.BASS_MIXER_PAUSE | BASSFlag.BASS_MIXER_DOWNMIX | BASSFlag.BASS_STREAM_AUTOFREE);

                        // an BASS_SYNC_END is used to trigger the next track in the playlist (if no POS sync was set)
                        track.TrackSync = new SYNCPROC(OnTrackSync);
                        BassMix.BASS_Mixer_ChannelSetSync(track.Channel, BASSSync.BASS_SYNC_END, 0L, track.TrackSync, new IntPtr(0));
                    }

                    if (_currentTrack == null)
                        PlayNextTrack();
                }
            }
        }

        private void PlayNextTrack()
        {
            lock (listBoxPlaylist)
            {
                // get the next track to play
                if (listBoxPlaylist.Items.Count > 0)
                {
                    _previousTrack = _currentTrack;
                    _currentTrack = listBoxPlaylist.Items[0] as Track;

                    listBoxPlaylist.Items.RemoveAt(0);

                    // the channel was already added
                    // so for instant playback, we just unpause the channel
                    _zoomStartBytes = (int)Bass.BASS_ChannelSeconds2Bytes(_currentTrack.Channel, _deviceLatencyMS / 1000f);
                    BassMix.BASS_Mixer_ChannelPlay(_currentTrack.Channel);

                    labelTitle.Text = _currentTrack.Tags.title;
                    labelArtist.Text = _currentTrack.Tags.artist;

                    // get the waveform for that track
                    GetWaveForm();
                }
            }
        }

private void cmdStop_Click(object sender, EventArgs e)
        {
            StopStream();
        }

        private void StopStream()
        {
            _currentTrack = null;
            timerUpdate.Stop();
            // kills rendering, if still in progress, e.g. if a large file was selected
            if (_WF != null && _WF.IsRenderingInProgress)
                _WF.RenderStop();
            DrawWavePosition(-1, -1);

            Bass.BASS_StreamFree(_mixer);
            _mixer = 0;
            //this.labelTime.Text = "Stopped";
            //this.button1.Text = "Select a file to play (e.g. MP3, OGG or WAV)...";
            //this.buttonStop.Enabled = false;
            //this.buttonPlay.Enabled = true;
        }

can anyone help me on this?

radio42

  • Posts: 4655
Re: BASSMIX can't play again if stop pressed
« Reply #1 on: 15 Aug '19 - 08:25 »
In your StopStream method you are freeing the entire _mixer, which (due to the BASS_STREAM_AUTOFREE flag at BASS_Mixer_StreamAddChannel also free's your source channel (_currentTrack.Channel).

noelquipit

  • Posts: 11
Re: BASSMIX can't play again if stop pressed
« Reply #2 on: 24 Aug '19 - 02:58 »
I did do this _currentTrack_P1 = null; but still not working.

I have something to ask if this is safe. i called the method that holding this code and it works.

 _damp_P1.Preset_Medium();

            Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_BUFFER, 200);
            Bass.BASS_SetConfig(BASSConfig.BASS_CONFIG_UPDATEPERIOD, 20);

            // already create a mixer
            _mixer_P1 = BassMix.BASS_Mixer_StreamCreate(44100, 2, BASSFlag.BASS_SAMPLE_FLOAT);
            Bass.BASS_ChannelSetDevice(_mixer_P1, newdevice_P1);
            if (_mixer_P1 == 0)
            {
                MessageBox.Show(this, "Could not create mixer!");
                Bass.BASS_Free();
                this.Close();
                return;
            }

            _mixerStallSync_P1 = new SYNCPROC(OnMixerStall_P1);
            Bass.BASS_ChannelSetSync(_mixer_P1, BASSSync.BASS_SYNC_STALL, 0L, _mixerStallSync_P1, IntPtr.Zero);

            _visModified_P1.MaxFFT = BASSData.BASS_DATA_FFT1024;
            _visModified_P1.MaxFrequencySpectrum = Utils.FFTFrequency2Index(16000, 1024, 44100);

            timerUpdate_P1.Start();
            Bass.BASS_ChannelPlay(_mixer_P1, false);

but i will check again if the error  A callback was made on a garbage collected delegate of type 'Bass.Net!Un4seen.Bass.SYNCPROC::Invoke'. will be trigger.

noelquipit

  • Posts: 11
Re: BASSMIX can't play again if stop pressed
« Reply #3 on: 24 Aug '19 - 06:55 »
i really encounter this error after number of play. "Additional information: A callback was made on a garbage collected delegate of type 'Bass.Net!Un4seen.Bass.SYNCPROC::Invoke'. This may cause application crashes, corruption and data loss. When passing delegates to unmanaged code, they must be kept alive by the managed application until it is guaranteed that they will never be called."

FYI i created two mix player in one form. check file attached. also i downloaded the file from here.
« Last Edit: 25 Aug '19 - 13:01 by noelquipit »

Ian @ un4seen

  • Administrator
  • Posts: 21979
Re: BASSMIX can't play again if stop pressed
« Reply #4 on: 26 Aug '19 - 13:17 »
You need to keep a reference to callback delegates to prevent them getting garbage collected by .Net. Please see the "Callbacks and Delegates" section of the "Interoperating with Unmanaged Code" page in the BASS.Net documentation for details.

To avoid mishaps, I would recommend creating a delegate for each callback function during initialization and keeping them in global variables for later use. For example, don't create a new delegate every time here:

                        // an BASS_SYNC_END is used to trigger the next track in the playlist (if no POS sync was set)
                        track.TrackSync = new SYNCPROC(OnTrackSync);
                        BassMix.BASS_Mixer_ChannelSetSync(track.Channel, BASSSync.BASS_SYNC_END, 0L, track.TrackSync, new IntPtr(0));