/// <summary>
        /// Detects if an audio CD/DVD/BD is contained in the given <paramref name="drive"/>.
        /// </summary>
        /// <param name="drive">The drive to be examined.</param>
        /// <param name="tracks">Returns a collection of audio tracks for the audio CD in the given <paramref name="drive"/>.</param>
        /// <param name="extractedMIATypeIDs">IDs of the media item aspect types which were extracted from the returned <paramref name="tracks"/>.</param>
        /// <returns><c>true</c>, if an audio CD was identified, else <c>false</c>.</returns>
        public static bool DetectAudioCD(string drive, out ICollection <MediaItem> tracks, out ICollection <Guid> extractedMIATypeIDs)
        {
            tracks = null;
            extractedMIATypeIDs = null;
            if (string.IsNullOrEmpty(drive) || drive.Length < 2)
            {
                return(false);
            }
            drive = drive.Substring(0, 2); // Clip potential '\\' at the end

            try
            {
                IList <BassUtils.AudioTrack> audioTracks = BassUtils.GetAudioTracks(drive);
                // BassUtils can report wrong audio tracks for some devices, we filter out "Duration = -1" here
                audioTracks = audioTracks?.Where(t => t.Duration > 0).ToList();
                if (audioTracks == null || audioTracks.Count == 0)
                {
                    return(false);
                }
                ISystemResolver systemResolver = ServiceRegistration.Get <ISystemResolver>();
                string          systemId       = systemResolver.LocalSystemId;
                tracks = new List <MediaItem>(audioTracks.Count);
                char driveChar = drive[0];
                foreach (BassUtils.AudioTrack track in audioTracks)
                {
                    tracks.Add(CreateMediaItem(track, driveChar, audioTracks.Count, systemId));
                }
                extractedMIATypeIDs = new List <Guid>
                {
                    ProviderResourceAspect.ASPECT_ID,
                    MediaAspect.ASPECT_ID,
                    AudioAspect.ASPECT_ID,
                };
            }
            catch (IOException)
            {
                ServiceRegistration.Get <ILogger>().Warn("Error enumerating tracks of audio CD in drive {0}", drive);
                tracks = null;
                return(false);
            }
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Detects if an audio CD/DVD/BD is contained in the given <paramref name="drive"/>.
        /// </summary>
        /// <param name="drive">The drive to be examined.</param>
        /// <param name="tracks">Returns a collection of audio tracks for the audio CD in the given <paramref name="drive"/>.</param>
        /// <param name="extractedMIATypeIDs">IDs of the media item aspect types which were extracted from the returned <paramref name="tracks"/>.</param>
        /// <returns><c>true</c>, if an audio CD was identified, else <c>false</c>.</returns>
        public static bool DetectAudioCD(DriveInfo driveInfo, out ICollection <MediaItem> tracks, out ICollection <Guid> extractedMIATypeIDs)
        {
            tracks = null;
            extractedMIATypeIDs = null;
            string drive = driveInfo.Name;

            if (string.IsNullOrEmpty(drive) || drive.Length < 2)
            {
                return(false);
            }
            drive = drive.Substring(0, 2); // Clip potential '\\' at the end

            try
            {
                IList <BassUtils.AudioTrack> audioTracks = BassUtils.GetAudioTracks(drive);
                // BassUtils can report wrong audio tracks for some devices, we filter out "Duration = -1" here
                audioTracks = audioTracks?.Where(t => t.Duration > 0).ToList();
                if (audioTracks == null || audioTracks.Count == 0)
                {
                    return(false);
                }
                ISystemResolver systemResolver = ServiceRegistration.Get <ISystemResolver>();
                string          systemId       = systemResolver.LocalSystemId;
                tracks = new List <MediaItem>(audioTracks.Count);
                char driveChar = drive[0];
                int  driveId   = BassUtils.Drive2BassID(driveChar);
                if (driveId > -1)
                {
                    BASS_CD_INFO info = BassCd.BASS_CD_GetInfo(driveId);
                    if (info.cdtext)
                    {
                        string[] tags        = BassCd.BASS_CD_GetIDText(driveId);
                        string   album       = GetCDText(tags, "TITLE");
                        string   albumArtist = GetCDText(tags, "PERFORMER");
                        foreach (BassUtils.AudioTrack track in audioTracks)
                        {
                            tracks.Add(CreateMediaItem(track, driveChar, audioTracks.Count, systemId, album, albumArtist,
                                                       album, albumArtist, irsc: BassCd.BASS_CD_GetISRC(driveId, track.TrackNo - 1)));
                        }
                    }
                    else
                    {
                        foreach (BassUtils.AudioTrack track in audioTracks)
                        {
                            tracks.Add(CreateMediaItem(track, driveChar, audioTracks.Count, systemId,
                                                       irsc: BassCd.BASS_CD_GetISRC(driveId, track.TrackNo - 1)));
                        }
                    }
                    BassCd.BASS_CD_Release(driveId);
                }
                else
                {
                    foreach (BassUtils.AudioTrack track in audioTracks)
                    {
                        tracks.Add(CreateMediaItem(track, driveChar, audioTracks.Count, systemId));
                    }
                }
                extractedMIATypeIDs = new List <Guid>
                {
                    ProviderResourceAspect.ASPECT_ID,
                    MediaAspect.ASPECT_ID,
                    AudioAspect.ASPECT_ID,
                    ExternalIdentifierAspect.ASPECT_ID,
                };
            }
            catch (IOException)
            {
                ServiceRegistration.Get <ILogger>().Warn("Error enumerating tracks of audio CD in drive {0}", drive);
                tracks = null;
                return(false);
            }
            return(true);
        }