问题
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.
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.
回答1:
How to upload an audio file with ID3 tags into an application using C# and visual studio
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
}
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.
来源:https://stackoverflow.com/questions/52196477/how-to-upload-an-audio-file-with-id3-tags-into-an-application-using-c-sharp-and