正如我在 twitter 上提到的,在 Windows Phone 8 中,您可以使用 MediaLibraryExtensions 从设备的音乐库中添加或删除歌曲。新功能在 MSDN here中提到。但是,我找不到任何 API 文档,所以这里是新的 Microsoft.Xna.Framework.MediaLibraryExtensions.dll 的 API 打印输出:
//Microsoft.Xna.Framework.MediaLibraryExtensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553
namespace Microsoft.Xna.Framework.Media.PhoneExtensions {
public static class MediaLibraryExtensions {
public static void Delete(MediaLibrary library, Song song);
public static String GetPath(Picture picture);
public static String GetPathFromToken(MediaLibrary library, String token);
public static Stream GetPreviewImage(Picture picture);
public static Song SaveSong(MediaLibrary library, Uri filename, SongMetadata songMetadata, SaveSongOperation operation);
}
public enum SaveSongOperation {
CopyToLibrary,
MoveToLibrary
}
public sealed class SongMetadata {
public SongMetadata();
public Uri AlbumArtistBackgroundUri { get; set; }
public String AlbumArtistName { get; set; }
public Uri AlbumArtUri { get; set; }
public String AlbumName { get; set; }
public DateTime AlbumReleaseDate { get; set; }
public Uri ArtistBackgroundUri { get; set; }
public String ArtistName { get; set; }
public TimeSpan Duration { get; set; }
public String GenreName { get; set; }
public String Name { get; set; }
public Int32 TrackNumber { get; set; }
}
}
您可以通过使用本地 URI 调用 SaveSong 并通过包含自定义 SongMetadata 潜在地覆盖 ID3 元数据来使用此新 API。此 API 仅允许您存储新歌曲,但我想您可以将您的播客归类为一个派系艺术家。关于此 API 的快速说明是确保添加新的 DLL 引用 MediaLibraryExtensions DLL。您还可以将 SongMetadata 保留为 null 并让 WP8 操作系统推断 ID3 元数据。
这是一个简单的代码片段:
private async void MainPage_Loaded(object sender, RoutedEventArgs e)
{
var sourceFile = await Package.Current.InstalledLocation.GetFileAsync("ChargeOfTheLightBridge.mp3");
CopyFileIntoIsoStore(sourceFile);
var library = new MediaLibrary();
library.SaveSong(new Uri(sourceFile.Name, UriKind.RelativeOrAbsolute),
new SongMetadata()
{
ArtistName = "My Custom Artist",
AlbumArtistName = "My Custom Artist",
Name = "My Custom Track Name",
AlbumName = "clubbing baby seals in the face",
Duration = TimeSpan.FromSeconds(29),
TrackNumber = 1,
AlbumReleaseDate = DateTime.Now,
GenreName = "Podcasts"
},
SaveSongOperation.CopyToLibrary);
}
private async void CopyFileIntoIsoStore(StorageFile sourceFile)
{
using (var s = await sourceFile.OpenReadAsync())
using (var dr = new DataReader(s))
using (var isoStore = IsolatedStorageFile.GetUserStoreForApplication())
using (var targetFile = isoStore.CreateFile(sourceFile.Name))
{
var data = new byte[s.Size];
await dr.LoadAsync((uint) s.Size);
dr.ReadBytes(data);
targetFile.Write(data, 0, data.Length);
}
}
请注意,我们必须在 IsoStore 中保存一个文件才能使用此 API。另请注意,Uri 的格式不正确,也不是标准的 IsoStore Uri。这只是文件名。
当我们运行这个代码片段时,我们可以看到以下内容: