如何使用 C# 和 Visual Studio 将带有 ID3 标签的音频文件上传到应用程序 [英] How to upload an audio file with ID3 tags into an application using C# and visual studio

查看:28
本文介绍了如何使用 C# 和 Visual Studio 将带有 ID3 标签的音频文件上传到应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写代码以从用户的计算机中提取 mp3 文件并将其上传到我正在使用 C# 在 Visual Studio UWP 中创建的音乐库应用程序.它需要能够提取艺术家、标题和专辑的 ID3 标签,因为这些都必须在实际的库页面上引用,音乐将在其中进行相应的排序.

I am currently working on code to pull an mp3 file from a user's computer and upload it into a music library application that I am creating in visual studio UWP using C#. It needs to be able to pull the ID3 tags for artist, title, and album, as these will all have to be referenced on the actual library page, where the music will be sorted accordingly.

到目前为止,以下代码是我所拥有的,我目前只需要编写其他代码来将文件上传到带有 ID3 标签的应用程序的音乐库部分:

The following code is what I have so far, and I am currently stuck on what else to write just to upload the file into the music library portion of my app with the ID3 tags:

 //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);
        }
}

我对这一切都很陌生,所以我可能在这段代码中遗漏了一些非常明显的东西.我查看了各种教程和示例,但没有一个提供我正在寻找的完全合适的内容,或者已经完成了一半.感谢您花时间阅读我的代码并提供任何建议/建议.

I am very new to all this, so I may be missing some very obvious things in this code. I have checked into various tutorials and examples, but none of them provide the exact fit I'm looking for or are half done. Thank you for taking the time to read my code and offer any advice/suggestions.

推荐答案

如何使用 C# 和 Visual Studio 将带有 ID3 标签的音频文件上传到应用程序

How to upload an audio file with ID3 tags into an application using C# and visual studio

要访问音频元数据,您可以使用 GetMusicPropertiesAsync 获取音频文件的专辑艺术家标题 MusicProperties 属性的方法.

For accessing audio metadata, you could use GetMusicPropertiesAsync method to get audio file's album artist title MusicProperties property.

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.这是您可以参考的代码示例.您也可以使用 HttpClient API 来发布您的文件流.

For uploading the audio file to server, you could use BackgroundTransfer api. and this is code sample you could refer to. And you could also you HttpClient API to post your file stream.

这篇关于如何使用 C# 和 Visual Studio 将带有 ID3 标签的音频文件上传到应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆