我目前正在编写代码,以从用户的计算机中提取mp3文件并将其上传到使用C#在Visual Studio UWP中创建的音乐库应用程序中。它必须能够提取艺术家,标题和专辑的ID3标签,因为所有这些都必须在实际的库页面上引用,在此处音乐将进行相应的排序。
以下是我到目前为止所拥有的代码,我目前仍在继续写些什么,只是将文件上传到带有ID3标签的应用程序的音乐库部分:
//Uploading Music File Button
private async void UploadButton_Click(object sender, RoutedEventArgs e)
{
//Opening User's personal Music Library to select files
var picker = new Windows.Storage.Pickers.FileOpenPicker
{
ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail,
SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.MusicLibrary
};
//Accepted file type = mp3 (only mp3 files display for user selection)
picker.FileTypeFilter.Add(".mp3");
StorageFile file = await picker.PickSingleFileAsync();
if (file != null)
{
// Application now has read/write access to the picked file
//Storing File for future use
Windows.Storage.AccessCache.StorageApplicationPermissions.FutureAccessList.Add(file);
// Open a stream for the selected file.
// The 'using' block ensures the stream is disposed
// after the music is loaded.
IRandomAccessStream fileStream =
await file.OpenAsync(FileAccessMode.ReadWrite);
}
}
对于这一切我都是新手,所以我可能在这段代码中缺少一些非常明显的东西。我已经查看了各种教程和示例,但是都没有提供我正在寻找的完全适合的或已经完成一半的示例。感谢您抽出宝贵的时间阅读我的代码并提供任何建议/建议。
答案 0 :(得分:1)
如何使用C#和Visual Studio将带有ID3标签的音频文件上传到应用程序中
要访问音频元数据,可以使用GetMusicPropertiesAsync
方法来获取音频文件的专辑艺术家标题MusicProperties
属性。
try
{
StorageFile file = rootPage.sampleFile;
if (file != null)
{
StringBuilder outputText = new StringBuilder();
// Get music properties
MusicProperties musicProperties = await file.Properties.GetMusicPropertiesAsync();
outputText.AppendLine("Album: " + musicProperties.Album);
outputText.AppendLine("Rating: " + musicProperties.Rating);
}
}
// Handle errors with catch blocks
catch (FileNotFoundException)
{
// For example, handle a file not found error
}
要将音频文件上传到服务器,可以使用BackgroundTransfer
API。这就是您可以参考的code sample。您也可以使用HttpClient API来发布文件流。