Can't manage to get image

May 20, 2014 at 12:15 AM
Edited May 20, 2014 at 4:05 PM
Hi,

Function "sp_playlist_get_image" doesn't seem to work for me. It returns true every time, but image pointer (imagePtr) is always 0, despite sp_session and sp_playlist are ok.

Here's what I'm trying to do
                IntPtr playlistPtr = libspotify.sp_playlistcontainer_playlist(container, i);
                IntPtr imagePtr = new IntPtr();
                libspotify.sp_playlist_get_image(playlistPtr, image);
Edit.

So now I get imagePtr correctly, but then, when i try to get image data in imageloaded callback it throws memory exception.
        private void onImageLoaded(IntPtr imagePtr, IntPtr userDataPtr)
        {
            isImageLoaded =  libspotify.sp_image_is_loaded(image);

            int bytesNum = 0;

            buffer = libspotify.sp_image_data(image, out bytesNum);

            if (callback != IntPtr.Zero)
            {
                libspotify.sp_image_remove_load_callback(image, callback, IntPtr.Zero);
                callback = IntPtr.Zero;
            }
        }
Edit

So now I got it working. I've just forgotten to put imagePtr = libspotify.sp_image_create(IntPtr imageID) instead of imageID
Coordinator
May 20, 2014 at 8:55 PM
Great! Glad you got it working. Was looking into this for you but you beat me to it.
May 21, 2014 at 12:23 PM
Thanks for reply, but now I have another problem. I can't see whether image is loaded or not, even with using waitFor method. I even used the method from JamcastSpotifyPlugin, but i doesn't work too.
        public static byte[] GetAlbumArt(IntPtr coverPtr)
        {
            IntPtr imagePtr = libspotify.sp_image_create(session, coverPtr);
            try
            {
                using (Image img = Image.Load(imagePtr))
                {
                    WaitFor(delegate()
                    {
                        return img.IsLoaded;
                    }, 20);

                    var err = img.GetLoadError();   <----------- Here I always get IMAGE_IS_LOADING
                    if (err != libspotify.sp_error.OK)
                    {
                        throw new ApplicationException(String.Format("Image failed to load: {0}", err));
                    }

                    int bytes = 0;
                    IntPtr bufferPtr = libspotify.sp_image_data(img.ImagePtr, out bytes);
                    byte[] buffer = new byte[bytes];
                    Marshal.Copy(bufferPtr, buffer, 0, buffer.Length);

                    return buffer;
                }
            }
            finally
            {
            }
        }
Coordinator
May 21, 2014 at 12:45 PM
Do you have a message loop set up for libspotify to work with? You'll need this, with a call to sp_session_process_events() within, in order for libspotify to process asynchronous operations like loading album art, etc.

Spotify.cs in the Spotify Plugin for Jamcast project has a working example of this.
May 21, 2014 at 3:02 PM
Actually you're right. Thanks for help :) I made copy&paste and now it's working. BTW I don't understand these threading operations, as I'm quite newbie so far. In spite of all, thanks for your help.
Coordinator
May 21, 2014 at 3:40 PM
callaoone wrote:
Actually you're right. Thanks for help :) I made copy&paste and now it's working. BTW I don't understand these threading operations, as I'm quite newbie so far. In spite of all, thanks for your help.
Great. The libspotify API is very challenging to work with. This library is merely a simple wrapper around it. The complexities are in your implementation, the Jamcast plugin is a good resource but I encourage you to check out the Spotify docs and sample code as well.

Good luck!