简体   繁体   English

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

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

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#. 我目前正在编写代码,以从用户的计算机中提取mp3文件并将其上传到使用C#在Visual Studio UWP中创建的音乐库应用程序中。 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: 到目前为止,下面的代码是我所拥有的,而我目前还停留在编写其他东西上,只是要将文件上传到带有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);
        }
}

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. 感谢您抽出宝贵的时间阅读我的代码并提供任何建议/建议。

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

For accessing audio metadata, you could use GetMusicPropertiesAsync method to get audio file's album artist title MusicProperties property. 要访问音频元数据,可以使用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
}

For uploading the audio file to server, you could use BackgroundTransfer api. 要将音频文件上传到服务器,可以使用BackgroundTransfer api。 and this is code sample you could refer to. 这是您可以参考的代码示例 And you could also you HttpClient API to post your file stream. 您也可以使用HttpClient API来发布文件流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM