[SOLVED] How to create a song object?

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

Moderators: Gurus, Addon Administrators

marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

[SOLVED] How to create a song object?

Post by marceros »

Hello,

I have to import a song to a playlist. The song may or may not be in the database (it's very likely that it IS in the database) but I have the path of the MP3 file in the disk.

I assume I want to use the SDBSongData object but how to create a new object when I only have the path string?

Thank you in advance,
Marcelo
Peke
Posts: 17446
Joined: Tue Jun 10, 2003 7:21 pm
Location: Earth
Contact:

Re: How to create a song object?

Post by Peke »

Try this

Code: Select all

Dim NewSong
Set NewSong = CreateObject("SDB.SDBSongData")
NewSong.Path = "C:\Temp\Test.mp3"
NewSong.ReadTags
Set NewSong = Nothing
You could also want to first query for path in Library

Code: Select all

Dim NewSong, TrackID.
Set TrackID = -1
Set NewSong = CreateObject("SDB.SDBSongData")

Set Iter = SDB.Database.QuerySongs("Path = 'C:\Temp\Test.mp3'")
While Not Iter.EOF
  TrackID = Iter.Item.SongID
  Iter.Next
WEnd
Set Iter = Nothing ' Avoid locking errors
Now you can either Manipulate Track ID and Directly Insert https://www.mediamonkey.com/wiki/index. ... st_members track to playlist or use https://www.mediamonkey.com/wiki/index. ... DBSongList to fill info and then https://www.mediamonkey.com/wiki/index. ... :AddTracks to fill MM playlist.

I hope I helped with this brief explanation it would be much simpler if https://www.mediamonkey.com/wiki/index. ... DBSongList supported AddByID like SDBPlaylist but it is missing :(
Best regards,
Peke
MediaMonkey Team lead QA/Tech Support guru
Admin of Free MediaMonkey addon Site HappyMonkeying
Image
Image
Image
How to attach PICTURE/SCREENSHOTS to forum posts
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: How to create a song object?

Post by marceros »

Thank you very much, Pavle!

I write in C# but after your response I realized that I was doing a mistake in my coding and it seems to work now. I'll finish writing the whole procedure and I'll check again.

Thanks,
Marcelo
Peke
Posts: 17446
Joined: Tue Jun 10, 2003 7:21 pm
Location: Earth
Contact:

Re: How to create a song object?

Post by Peke »

Hi,
My pleasure always. I also noted that AddByID is missing in SDBSongList for some future MM versions ;)
Best regards,
Peke
MediaMonkey Team lead QA/Tech Support guru
Admin of Free MediaMonkey addon Site HappyMonkeying
Image
Image
Image
How to attach PICTURE/SCREENSHOTS to forum posts
marceros
Posts: 36
Joined: Sun Apr 21, 2013 1:54 am

Re: [SOLVED] How to create a song object?

Post by marceros »

I managed to create the SongData object but the program behaves strange. I'm attaching the code that does the following:
1. reads lines (from a M3U file) with paths to MP3 music files
2. creates a new song object
3. connects the song object with the path
4. runs the ReadRags method
5. adds the song object to a playlist (created previously in the code)

The first round of the loop works good but in the second round, the song object gets stuck with the same first music so I end up with only one music in the playlist.

Code: Select all

System.IO.StreamReader reader = new System.IO.StreamReader(playlistDisk_path);
while (!reader.EndOfStream)
{
    string line = reader.ReadLine();
    if (line[0] != '#')
    {
         SongsDB.SDBSongData song = new SongsDB.SDBSongData();
         song.Path=line;
         song.ReadTags();
         playlistDB_actualLevel.AddTrack(song);
    }
}
Peke
Posts: 17446
Joined: Tue Jun 10, 2003 7:21 pm
Location: Earth
Contact:

Re: [SOLVED] How to create a song object?

Post by Peke »

I would use this approach instead of your example as it will not keep playlist file open

Code: Select all

Set obj = CreateObject(“Scripting.FileSystemObject”)    ‘Creating a File Object
Const ForReading = 1                      ‘Defining Constant Value to read from a file
Set obj1 = obj.OpenTextFile(“C:\app.txt”, ForReading) ‘Opening a text file and reading text from it
Dim str,str1
str=obj1.ReadAll                                              ‘All text from the file is read using ReadAll
Dim strLines() As String = Split(Replace(str,Chr(13),""),Chr(10)) ‘Split lines in Array and it works in linux based M3Us
Msgbox str                                ‘Contents of a file will be displayed through message box
Do while obj1.AtEndofStream                  ‘Reading text line wise using Do Loop and ReadLine
str1=obj1.ReadLine
Msgbox str1
Loop
obj1.Close                                         ‘Closing a File
Set obj=Nothing                              ‘Releasing File object
Best regards,
Peke
MediaMonkey Team lead QA/Tech Support guru
Admin of Free MediaMonkey addon Site HappyMonkeying
Image
Image
Image
How to attach PICTURE/SCREENSHOTS to forum posts
Post Reply