Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
AccessViolationException when call PlayerLoad()
24-02-2015, 12:37 AM (This post was last modified: 24-02-2015 12:53 AM by feanaro100.)
Post: #1
AccessViolationException when call PlayerLoad()
Hi,

It's that same German again, this time with another problem.
I managed to get all the events up and working. After that I created the buffering and playback mechanics.

This method starts the playback of a specific song. I've placed it on it's own thread because of the AutoResetEvent that blocks the thread until the Track has been loaded. As you can see I tried to mitigate this problem by unloading the track before re-creating the buffer. This didn't help at all.

Code:
public void playTrack(string track)
        {
            new Thread(new ThreadStart(() =>
            {
                var playable = Link.CreateFromString(string.Format("spotify:track:{0}", track)).AsTrack();

                if (playing)
                {
                    player.Pause();
                    App.Logic.spotify.sp_session.PlayerUnload();
                }

                buffer = new BufferedWaveProvider(new WaveFormat())
                {
                    BufferDuration = TimeSpan.FromSeconds(120),
                    DiscardOnBufferOverflow = false
                };

                var waitEvent = new AutoResetEvent(false);
                while (!playable.IsLoaded())
                {
                    waitEvent.WaitOne(30);
                }

                App.Logic.spotify.sp_session.PlayerLoad(playable);
                App.Logic.spotify.sp_session.PlayerPlay(true);

                player = new WaveOut();
                player.Init(buffer);
                player.Play();
                playing = true;
            })).Start();
        }

The AccessViolationException occurs on line 4 of this piece of code within GeneratedCode.cs. This only happens after I try to play a second song. I also tried waiting until the first song was done playing, that didn't change anything though. It sometimes happens that I can play the second track but then on the third track it throws the AccessViolationException. Some other times it throws the exception whilst loading the first track. Very weird.

Code:
public void PlayerLoad(Track @track)
        {
            SpotifyError errorValue;
            errorValue = NativeMethods.sp_session_player_load(this._handle, track._handle);
            SpotifyMarshalling.CheckError(errorValue);
        }

I thought it couldn't hurt to put in the SpotifySessionListener callbacks, it might help you come to a more complete picture of the situation.

Code:
public override void GetAudioBufferStats(SpotifySession session, out AudioBufferStats stats)
        {
            stats = new AudioBufferStats()
            {
                samples = App.Logic.spotify.player.buffer.BufferedBytes / 2,
                stutter = 0
            };
        }

        public override int MusicDelivery(SpotifySession session, AudioFormat format, IntPtr frames, int num_frames) {
            int incoming_size = num_frames * format.channels * 2;
            try
            {
                if (incoming_size > sample_buffer.Length)
                {
                    short rendered_frames = Convert.ToInt16(Math.Floor((sample_buffer.Length / format.channels / 2d)));
                    short rendered_size = Convert.ToInt16(rendered_frames * format.channels * 2);
                    Marshal.Copy(frames, sample_buffer, 0, rendered_size);
                    App.Logic.spotify.player.buffer.AddSamples(sample_buffer, 0, rendered_size);
                    return rendered_frames;
                }
                else
                {
                    Marshal.Copy(frames, sample_buffer, 0, incoming_size);
                    App.Logic.spotify.player.buffer.AddSamples(sample_buffer, 0, incoming_size);
                    return num_frames;
                }
            }
            catch (Exception e)
            {
                return 0;
            }
        }

If you need any more information or explanation, I'd be happy to supply it!
Find all posts by this user
Quote this message in a reply
26-02-2015, 01:59 PM
Post: #2
RE: AccessViolationException when call PlayerLoad()
On StackOverflow Andreww has helped me solve this issue. You might want to take a look there for a possible answer.
Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump: