5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

iTunesの歌詞に歌詞を追加

Posted at

参照設定からiTunes Libraryを追加してください。

myiTunes.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using iTunesLib;

namespace mp3Lyric {
    public class MusicInfo {
        public string Artist { get; set; }
        public string Album { get; set; }
        public string Title { get; set; }
        public string Lyric { get; set; }
    }

    /// <summary>
    /// iTunesをどうにかするクラス
    /// </summary>
    public class MyiTunes {
        #region プロパティ

        /// <summary>
        /// 曲情報
        /// </summary>
        public MusicInfo Info {
            get;
            private set;
        }

        /// <summary>
        /// iTunesAppのインスタンス
        /// </summary>
        private iTunesApp _iTunes = null;

        /// <summary>
        /// アルバムの検索結果
        /// </summary>
        private IITTrackCollection _searchRsltAlbum;

        #endregion

        #region コンストラクタ

        public MyiTunes() {
            _iTunes = new iTunesApp();
            Info = new MusicInfo();
            Info.Artist = Info.Album = Info.Lyric = Info.Title = null;
        }

        #endregion

        #region メソッド

        /// <summary>
        /// 曲情報を入力します。
        /// </summary>
        /// <param name="info">曲情報</param>
        public void SetInfo(MusicInfo info) {
            Info = info;
        }

        /// <summary>
        /// 歌詞を曲に追加します。
        /// </summary>
        /// <returns>成功したかどうか</returns>
        public bool SetLyric() {

            if (!isSet()) {
                Console.Error.WriteLine("! SetInfo()を使って曲情報を入力してください。");
                return false;
            }

            // その曲が収録されてるアルバムを検索
            _searchRsltAlbum = _iTunes.LibraryPlaylist.Search(
                Info.Album, ITPlaylistSearchField.ITPlaylistSearchFieldAlbums);
            if (_searchRsltAlbum == null) {
                Console.Error.WriteLine("! 指定したアルバムが存在しません。");
                return false;
            }

            var song = _searchRsltAlbum.get_ItemByName(Info.Title);
            if (song == null) {
                Console.Error.WriteLine("! 指定した曲が存在しません。");
                return false;
            }
            if (((IITFileOrCDTrack)song).Lyrics != null) {
                Console.Error.WriteLine("! すでに歌詞が付けられています。");
                return false;
            }
            ((IITFileOrCDTrack)song).Lyrics = Info.Lyric;

            return true;
        }

        /// <summary>
        /// 曲情報がすでに入力されているかどうか
        /// </summary>
        /// <returns></returns>
        private bool isSet() {

            return Info.Artist != null;
        }

        #endregion
    }
}

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?