Jump to content

c# how do you get the artist of a song from a track?


Recommended Posts

mickle026
Posted

I get the albums like this

var AllAlbum = libraryManager.GetInternalItemIds(new InternalItemsQuery
            {
                IncludeItemTypes = new[] { "MusicAlbum" }
            });

Then iterate through the albums churning out the tracks

 foreach (var SingleAlbum in AllAlbum)
            {

                var mAlbum = libraryManager.GetItemById(SingleAlbum);
                var AlbumName = mAlbum.Name.Replace(" ,", " ").Trim();

                var Tracklist = libraryManager.GetItemIds(new InternalItemsQuery
                {
                    AlbumIds = new[] { SingleAlbum }
                });

foreach (var Entry in Tracklist)
                {
                    var trackid = libraryManager.GetItemById(Entry).InternalId;
                    var TrackName = libraryManager.GetItemById(Entry).Name;

but how to get the artist of a track ?

 

I can get all the artists on the album but not for the track by itself.

var TrackArtists = libraryManager.GetAllArtists(mAlbum);

or
  
var TrackArtists = libraryManager.GetArtists(mAlbum);

to avoid the album artist

However this array is not in the same order.

ie Tracklist[0] and TrackArtists[0] do not correspond with each other, they are in a different order

I have all the tracks and all the artists but they dont match up, there must be a simple way to do this, any ideas?

 

 

 

Posted

Hi.  The return from GetItemById will be an item with all the properties of that item (e.g. Artists).  Peek at it in the debugger and you can see all the properties.

Also, you can grab that once into a local and not have to run through the search/get/database operation multiple times on each track...

mickle026
Posted

Thank you for taking the time to reply,

I can see the properties but I dont see anything thats obvious for getting the artist for the track neither on GetItemById for the Album or GetItemById  for the track :(

I see GetImageInfo etc but nothing for Artist or ArtistInfo

 

Posted

You have to cast it to (Audio) and then you'll get more properties available.

mickle026
Posted (edited)
57 minutes ago, Luke said:

You have to cast it to (Audio) and then you'll get more properties available.

mmm, im not that good yet, I dont know how to do that :(, I'll give it a go and wonder if there is a much easier way than what i am doing - lol

 

Tried This and it looks like it should work but get InvalidCast Exception,  MusicAlbum to Audioo doesnt work (Amateur at play here :) )

 

var mArtist = (Audio)mAlbum;
var mArtistNames= mArtist.Artists;

foreach (var newname in mArtistNames)
{
       logger.Info($"Artist: {newname} ", null);
}    

OK so I think Ive got it.... Cast the track to Audio (TrackImage in my code)

foreach (var Entry in Tracklist)
                {
                    var trackid = libraryManager.GetItemById(Entry).InternalId;
                    var TrackName = libraryManager.GetItemById(Entry).Name;
                    var TrackImage = libraryManager.GetItemById(Entry);

                    var mArtist = (Audio)TrackImage;
                    string[] mArtistNames = mArtist.Artists;

foreach (var newname in mArtistNames)
                    {
                        logger.Info($"Artist: {newname} ", null);
                    }

 

Edited by mickle026
mickle026
Posted

I Just want to say Thank You!

This has now opened the Plugin coding up for me :)

  • Thanks 1
BillOatman
Posted (edited)

Instead of this

var trackid = libraryManager.GetItemById(Entry).InternalId;
var TrackName = libraryManager.GetItemById(Entry).Name;
var TrackImage = libraryManager.GetItemById(Entry);

Maybe try this, calling the GetItemById once instead of multiple times.

var TrackImage = libraryManager.GetItemById(Entry);
var trackid = TrackImage.InternalId;
var TrackName = TrackImage.Name;

                    

 

Edited by BillOatman
mickle026
Posted (edited)
10 hours ago, BillOatman said:

Instead of this

var trackid = libraryManager.GetItemById(Entry).InternalId;
var TrackName = libraryManager.GetItemById(Entry).Name;
var TrackImage = libraryManager.GetItemById(Entry);

Maybe try this, calling the GetItemById once instead of multiple times.

var TrackImage = libraryManager.GetItemById(Entry);
var trackid = TrackImage.InternalId;
var TrackName = TrackImage.Name;

                    

 

Thanks #BillOatman, makes sense.

I am self taught with c#, and only learned what bit I know because I wanted to write a plugin, im getting better but still have big gaps in my knowledge, helpful pointers and improvements are always welcome :)

Edited by mickle026
Posted
20 hours ago, BillOatman said:

Maybe try this, calling the GetItemById once instead of multiple times.

var TrackImage = libraryManager.GetItemById(Entry);
var trackid = TrackImage.InternalId;
var TrackName = TrackImage.Name;

Then modify that first one to:

Audio track = (Audio)libraryManager.GetItemById(Entry);

 

  • Like 1
BillOatman
Posted
1 hour ago, ebr said:

Then modify that first one to:

Audio track = (Audio)libraryManager.GetItemById(Entry);

 

True, I was assuming he might be using track id and name for something later :)

mickle026
Posted

I am using all the parts.  Well trying to.

 

However I am confused, not with this code above but why as a part of my other code I am having problems with provider keys

 

string mTrack_id = mTrack.GetProviderId(MetadataProviders.MusicBrainzTrack);
string mAlbum_id = mTrack.GetProviderId(MetadataProviders.AudioDbAlbum);

No matter how I add this within Emby when in the c# code MusicBrainzTrack is always Empty/Null, is there something wrong with that provider key? - The other seem to work fine so far in my tests.

 

Screenshot 2021-12-22 at 17-54-14 Emby.png

Posted

Are you able to get other provider id values?

mickle026
Posted
1 hour ago, Luke said:

Are you able to get other provider id values?

Yes without any problems, this one though is always empty.

Posted

I really don't know. Maybe that specific track doesn't actually have the value?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...