参照設定から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
}
}