Visual C# Select which Artist to play music from

This forum is for questions / discussions regarding development of addons / tweaks for MediaMonkey.

Moderator: Gurus

Flaming-Ninja
Posts: 8
Joined: Fri May 10, 2013 5:58 am

Visual C# Select which Artist to play music from

Post by Flaming-Ninja » Fri May 10, 2013 6:12 am

Hello,

I'm quite new to C# programming, but I'm starting to get a hang of it all now.
Since I work a lot with Mediamonkey, I started making an own little program to control my Mediamonkey player.
After some searching on the internet I know the basics.
I can already play/pause music or play the next song etc. with the following code:

Code: Select all

SongsDB.SDBApplication SDB = new SongsDB.SDBApplication();
SDB.ShutdownAfterDisconnect = false;
and

Code: Select all

SDB.Player.Play();
SDB.Player.Next();
But I was wondering if I can select which Artist to play music from in my own program.
So is it possible to, for example, start playing music of Linkin Park after I entered 'Linkin Park' in a textbox?

Thanks in advance for the trouble to be taken.
Last edited by Lowlander on Fri May 10, 2013 9:57 am, edited 1 time in total.
Reason: Moved to correct forum

Peke
Posts: 12697
Joined: Tue Jun 10, 2003 7:21 pm
Location: Serbia
Contact:

Re: Visual C# Select which Artist to play music from

Post by Peke » Fri May 10, 2013 8:04 pm

Best regards,
Pavle
MediaMonkey Team lead QA/Tech Support guru
Admin of Free MediaMonkey addon Site HappyMonkeying
Image
Image
How to add SCREENSHOTS to forum

Flaming-Ninja
Posts: 8
Joined: Fri May 10, 2013 5:58 am

Re: Visual C# Select which Artist to play music from

Post by Flaming-Ninja » Sat May 11, 2013 12:23 pm

At first thank you for the answer.
But I cannot really figure out how to use that function.
I made the following code, where txt_Song is a textbox:

Code: Select all

string songs;
songs = txt_Song.Text.ToString();
SDB.Database.QuerySongs(songs);
But this doesn't work.
I suppose I have to do something with the SDBSongIterator?
But how and where do I call that function?
Thanks!

Scottes
Posts: 139
Joined: Sat Mar 21, 2009 6:51 am

Re: Visual C# Select which Artist to play music from

Post by Scottes » Sat May 11, 2013 1:18 pm

I'm also working in C#. I could not figure out how to do an Iterator in C#, so I query Songs, and check whether SongTitle is Null, indicating that there are no more songs to iterate over.

I don't use QuerySongs, but OpenSQL in my program, but maybe the snippet below will help you. Also do a search here for "QuerySongs" and you'll find some VB code showing examples, and the VB example will translate to C# quite easily.

Code: Select all

SongsDB.SDBApplication SDB = new SongsDB.SDBApplication();
SDB.ShutdownAfterDisconnect = false;

Query = "SELECT SongTitle FROM Songs WHERE Artist LIKE '%Link%' AND Album LIKE '%Hybrid%'";

var QueryData = SDB.Database.OpenSQL(Query);
while (true)
{
    if (QueryData.StringByName("SongTitle") == null)
        break;

    if (QueryData.StringByName("SongTitle") != null)
    {
        label_Artist.Text = QueryData.StringByName("Artist");
        label_Album.Text = QueryData.StringByName("Album");
        label_Song.Text = QueryData.StringByName("SongTitle");
        # Do stuff with the current song...
    }
    QueryData.Next();
}
SDB = null;
MonkeyMatch - Find and fix similar/incorrect spellings
MonkeyMatch Thread

Scottes
Posts: 139
Joined: Sat Mar 21, 2009 6:51 am

Re: Visual C# Select which Artist to play music from

Post by Scottes » Sat May 11, 2013 1:29 pm

I found an old version of my program when I used QuerySongs. Here's the syntax:

var QueryData = SDB.Database.QuerySongs("Artist LIKE 'pink floyd'");

You don't have to do the SELECT and such with QuerySongs
MonkeyMatch - Find and fix similar/incorrect spellings
MonkeyMatch Thread

Flaming-Ninja
Posts: 8
Joined: Fri May 10, 2013 5:58 am

Re: Visual C# Select which Artist to play music from

Post by Flaming-Ninja » Sat May 11, 2013 2:10 pm

So for as far as I understand, you basically have the data of the songs to be played in the variable QueryData with the following code, am I right?

Code: Select all

Query = "Artist LIKE '" + txt_Song.Text.ToString() + "'";
var QueryData = SDB.Database.QuerySongs(Query);
But then how do I send that data to Mediamonkey so it will start playing that specific artist or whatever?
Thank you!

Scottes
Posts: 139
Joined: Sat Mar 21, 2009 6:51 am

Re: Visual C# Select which Artist to play music from

Post by Scottes » Sat May 11, 2013 3:00 pm

When searching for a partial name, use LIKE with % as wildcards:

txt_Song.Text = "zeppel"
Query = "Artist LIKE '%" + txt_Song.Text.ToString() + %''";
So Query would be: Artist LIKE '%zeppel%'

For an exact match, use =, no %

txt_Song.Text = "Led Zeppelin"
Query = "Artist ='" + txt_Song.Text.ToString() + ''";
So Query would be: Artist='Led Zeppelin'


Also, this post might prove helpful:
http://www.mediamonkey.com/forum/viewto ... 19&t=63177

This is kind of out of my realm. I'm kinda struggling to figure out how to use MM with C#, too. The best advice I can give you is to search this Addons Developer Forum for whatever you're trying to do - I searched for "play song" to get that post above. Then you need to translate any VB code you find into C# code. Usually it's pretty straight forward.


My *guess* would be to look at http://www.mediamonkey.com/wiki/index.php/SDBPlayer which will let you manipulate the NowPlaying playlist.

Do an SDB.Player.PlaylistClear
Loop through your query, and on every song you find, do an SDB.Player.PlaylistAddTrack
When done with the loop, do an SDB.Player.Play


And when you figure it out, post the code!
MonkeyMatch - Find and fix similar/incorrect spellings
MonkeyMatch Thread

Flaming-Ninja
Posts: 8
Joined: Fri May 10, 2013 5:58 am

Re: Visual C# Select which Artist to play music from

Post by Flaming-Ninja » Sat May 11, 2013 7:57 pm

After alot of thinking I made the following code:

Code: Select all

var SongList = new SDBSongList();
SongList = SDB.PlaylistByTitle[txt_Song.Text.ToString()].Tracks;
SDB.Player.PlaylistClear();
SDB.Player.PlaylistAddTracks(SongList);
SDB.Player.CurrentSongIndex = 0;
What this Basically does is that it selects a specific playlist based on what you type in the textbox and sets the songs in this playlist in a songlist.
Then it clears the current playlist en loads the new songlist into the empty playlist.
And at last but not least it starts playing ;-)

So this does what I want it to do, if I have a playlist for every artist in my library.
But since I don't have that and it is alot of work to create all those playlists and add the right tracks, I thought that has to be possible to as a batch process in Visual C#.
But I couldn't really figure out how to batch create a playlist for every artist in my library.
I can call the names of all artists in strings in my program, so I was thinking of creating one playlist with all other playlists as childplaylists in it...
Has anyone an idea of how to do that?

Scottes
Posts: 139
Joined: Sat Mar 21, 2009 6:51 am

Re: Visual C# Select which Artist to play music from

Post by Scottes » Sat May 11, 2013 8:33 pm

Well, you can create playlists in MediaMonkey, and specify which playlist you want to play. I'm pretty sure there is a way to select which MediaMonkey playlist to play, rather than building a new playlist every time.


"But I couldn't really figure out how to batch create a playlist for every artist in my library."
Why not build a list of all the artists you have, using code something like:

Code: Select all

Query = "SELECT Artist FROM Songs GROUP BY Artist ORDER BY Artist";
var QueryData = SDB.Database.OpenSQL(Query);
while (true)
{
    if (QueryData.StringByName("SongTitle") == null)
        break;

    if (QueryData.StringByName("SongTitle") != null)
        ListBox1.Add(QueryData.StringByName("Artist");

    QueryData.Next();
}
SDB = null;
This will populate a ListBox with every Artist in your library.

"GROUP BY" makes the list unique, so even if you have 100 songs by Led Zeppelin your listbox will only contain Led Zeppelin once.
"ORDER BY" sorts the results alphabetically, so the listbox will be sorted alphabetically.

Next, click on one of the artist names in the listbox, and click your Play button. And that btnPlay_Click() method would run another SDB query, with "Artist LIKE" whatever artist name you clicked on. Loop through those results, adding each song to a PlayList, then do the SDB.Player.Play for that PlayList.
MonkeyMatch - Find and fix similar/incorrect spellings
MonkeyMatch Thread

Flaming-Ninja
Posts: 8
Joined: Fri May 10, 2013 5:58 am

Re: Visual C# Select which Artist to play music from

Post by Flaming-Ninja » Sun May 12, 2013 4:58 am

I already figured out how to build a list of all my artists with the following code:

Code: Select all

for (i = 0; i < SDB.PlaylistByTitle["All Music"].Tracks.Artists.Count; i++)
{
    var Artist = SDB.PlaylistByTitle["All Music"].Tracks.Artists.Item[i].Name.ToString();
    listBox1.Items.Add(Artist);
    listBox1.TopIndex = listBox1.Items.Count - 1;
}
But to be honest, I don't really understand how the whole 'Query', 'QueryData' and 'OpenSQL' thingy works.
Somehow Visual Studio won't give me any previews of what functions to use with that.
Also I keep getting errors while trying your code
What am I doing wrong?

Flaming-Ninja
Posts: 8
Joined: Fri May 10, 2013 5:58 am

Re: Visual C# Select which Artist to play music from

Post by Flaming-Ninja » Sun May 12, 2013 7:31 am

Okay, this is basically how I did it:

First, I made a Playlist called "All Music" and added all my music to it.
Note that this playlist must be sorted by artist.
The next thing to do is to play that playlist so it is loaded into the 'CurrentSongList'.
After that I ran the following code:

Code: Select all

        {

            int i;
            int j = 0;

            var Playlist = SDB.PlaylistByTitle["All Music"];
            Playlist.CreateChildPlaylist(SDB.PlaylistByTitle["All Music"].Tracks.Artists.Item[0].Name.ToString());

            for (i = 0; i < (SDB.Player.CurrentSongList.Count - 1); i++)
            {
                var Song = SDB.Player.CurrentSongList.Item[i];
                var Artist = SDB.Player.CurrentSongList.Item[i].Artist.Name;
                var NextArtist = SDB.Player.CurrentSongList.Item[i + 1].Artist.Name;

                if (Artist.Contains("The "))
                {
                    Artist = Artist.Replace("The ", "");
                }

                if (NextArtist.Contains("The "))
                {
                    NextArtist = NextArtist.Replace("The ", "");
                }

                if (Song != null)
                {
                    Playlist.ChildPlaylists.Item[j].AddTrack(Song);
                }

                if (!(Artist.Equals(NextArtist)))
                {
                    Playlist.CreateChildPlaylist(NextArtist);
                    j++;
                }
            }

            Playlist.ChildPlaylists.Item[j].AddTrack(SDB.Player.CurrentSongList.Item[SDB.Player.CurrentSongList.Count]);

        }
What this basically does is that it makes a Childplaylist for every Artist in the Playlist "All Music" and puts all songs of that artist in it.
This process may take some time, depending on the number of songs you have.

Now I can just run the next code:

Code: Select all

        {
            var SongList = new SDBSongList();
            SongList = SDB.PlaylistByTitle[txt_Artist.Text.ToString()].Tracks;
            SDB.Player.PlaylistClear();
            SDB.Player.PlaylistAddTracks(SongList);
            SDB.Player.CurrentSongIndex = 0;
        }
So you can just type the name of the artist in the txt_Artist TextBox and the player will start playing the playlist with the name of that artist.
There probably are lots of other ways of doing this, but for me this works good enough.

Notes:
- Don't forget to add the MediaMonkey Library COM Reference to your project.

- Don't forget to insert "using SongsDB;" to the top of your .cs file.

- Don't forget to insert

Code: Select all

SongsDB.SDBApplication SDB = new SongsDB.SDBApplication();
SDB.ShutdownAfterDisconnect = false;
in your .cs file.

Scottes
Posts: 139
Joined: Sat Mar 21, 2009 6:51 am

Re: Visual C# Select which Artist to play music from

Post by Scottes » Sun May 12, 2013 9:29 am

Flaming-Ninja wrote:But to be honest, I don't really understand how the whole 'Query', 'QueryData' and 'OpenSQL' thingy works.
MediaMonkey has (at least) two options for searching for data in its database. QuerySongs and OpenSQL. I like OpenSQL because it's more powerful and I am somewhat used to SQL commands. (SQL Commands are Structured Query Language, commands used to search databases. QuerySongs is basically a wrapper around SQL commands. It does much of the work for you, makes searching the database easier, but you lose some functionality. Unless you know that you need to functionality, just use QuerySongs.

Running a QuerySongs query will return a set of songs that match the query.

var QueryData = SDB.Database.QuerySongs("Artist='pink floyd'");

QueryData contains all the song by Pink Floyd. You then loop through that list grabbing whatever information you want - like getting a SongTitle - or adding the songs to a playlist.
Flaming-Ninja wrote:Somehow Visual Studio won't give me any previews of what functions to use with that.
Yeah, that does kinda suck. You have to go through the Wiki and see what you can type.
http://www.mediamonkey.com/wiki/index.php/SDBSongData
And there's a big list on the right, too. It is certainly daunting at first, but you will get used to it. You are already using some of the functionality, and it will come easier over time.
Flaming-Ninja wrote:Also I keep getting errors while trying your code
What am I doing wrong?
It's almost certainly MY mistake, not yours. I was writing much of the code off the top of my head, and it might take minor tweaking to get it to work. But if you have some code that just doesn't work, post it, and I will try to help. Keep in mind that I'm learning this stuff, too...
MonkeyMatch - Find and fix similar/incorrect spellings
MonkeyMatch Thread

Scottes
Posts: 139
Joined: Sat Mar 21, 2009 6:51 am

Re: Visual C# Select which Artist to play music from

Post by Scottes » Sun May 12, 2013 9:32 am

Flaming-Ninja wrote:...but for me this works good enough.
That counts for a lot! It works.

Congrats, and thanks for posting the code. I may very well steal some of it, and I'm sure that it will help someone figure out how to program MediaMonkey with C#.
MonkeyMatch - Find and fix similar/incorrect spellings
MonkeyMatch Thread

Scottes
Posts: 139
Joined: Sat Mar 21, 2009 6:51 am

Re: Visual C# Select which Artist to play music from

Post by Scottes » Sun May 12, 2013 7:21 pm

I don't know if this will help you out, but I just finished a little bit of code in my program and it's similar to what you're doing. So I figured that I'd post it, hoping it can help you (or the next person that stumbles upon this thread).

My program is made to find incorrect spellings of Artists, Albums and Song titles. Then it will correct them. This portion finds all songs with an incorrect spelling and changes them to the correct spelling, then saves the info to the database - and updates the tags if the user chose to do so.


Basically this bit of code will find all songs by an artist and add them to a playlist - one you remove the lines:
NewSongList.Item[NewSongList.Count -1].ArtistName = NewValue;
and
NewSongList.UpdateAll();

Code: Select all

PrimaryFieldName = "Artist";
OriginalValue = "Led Zepelin";  // Incorrect spelling
NewValue = "Led Zeppelin";      // Correct spelling

// Query will be "Artist='LedZepelin'"
Query = string.Format("{0}='{1}'", PrimaryFieldName, OriginalValue);

SongsDB.SDBApplication SDB = new SongsDB.SDBApplication();
SDB.ShutdownAfterDisconnect = false;

// Get all songs by the artist "Led Zepelin"
var QueryData = SDB.Database.QuerySongs(Query);

// Create a playlist to add the songs to
var NewSongList = SDB.NewSongList;

while (true)
{
    // If EOF, we've seen all the songs by 'Led Zepelin', so break out of the loop
    if (QueryData.EOF)
    {
        break;
    }
    else
    {
        // Add this song to the SongList
        NewSongList.Add(QueryData.Item);
        // Change the incorrect spelling 'Led Zepelin' to the correct 'Led Zeppelin'
        NewSongList.Item[NewSongList.Count -1].ArtistName = NewValue;
    }
    QueryData.Next();
}
// Save the info and tags per the MediaMonkey configuration
// Tools > Options > Library > Tags & Playlists > "Update tags when editing properties"
NewSongList.UpdateAll();

// Close the connection to the database
SDB = null;
MonkeyMatch - Find and fix similar/incorrect spellings
MonkeyMatch Thread

Post Reply