如何在Windows Forms App中获取MP3文件的BPM属性 [英] How can I get the BPM property of an MP3 file in a Windows Forms App

查看:98
本文介绍了如何在Windows Forms App中获取MP3文件的BPM属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从MP3文件中获取BPM属性:

我可以根据以下问题在Windows Store应用中查看如何执行此操作:

如何在Windows应用商店C#中读取mp3文件的每分钟节拍标记?

,但看不到如何在Windows Forms应用程序中使用Windows.Storage. (如果我理解正确,那是因为Windows.Storage是特定于UWP的.)

如何在Forms应用程序中阅读此内容?如果没有本地语言,很高兴使用一个(希望免费的)库.

解决方案

您可以使用Windows的 ShellFolderItem.ExtendedProperty 方法

您要使用的属性是名为的Windows官方属性. System.Music.BeatsPerMinute

因此,这是您可以使用它的方式(由于COM对象具有很酷的dynamic C#语法,因此您无需引用任何内容):

static void Main(string[] args)
{
    string path = @"C:\path\kilroy_was_here.mp3";

    // instantiate the Application object
    dynamic shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));

    // get the folder and the child
    var folder = shell.NameSpace(Path.GetDirectoryName(path));
    var item = folder.ParseName(Path.GetFileName(path));

    // get the item's property by it's canonical name. doc says it's a string
    string bpm = item.ExtendedProperty("System.Music.BeatsPerMinute");
    Console.WriteLine(bpm);
}

I am trying to get the BPM property from an MP3 file:

I can see how to do this in a Windows Store App as per this question:

How to read Beats-per-minute tag of mp3 file in windows store apps C#?

but can't see how to use Windows.Storage in a Windows Forms app. (If I understand it correctly it's because Windows.Storage is specific to UWP.)

How can I read this in a Forms app? Happy to use a (hopefully free) library if there is nothing native.

解决方案

You can use Windows' Scriptable Shell Objects for that. The item object has an ShellFolderItem.ExtendedProperty method

The property you're after is an official Windows property named System.Music.BeatsPerMinute

So, here is how you can use it (you don't need to reference anything, thanks to the cool dynamic C# syntax for COM objects):

static void Main(string[] args)
{
    string path = @"C:\path\kilroy_was_here.mp3";

    // instantiate the Application object
    dynamic shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"));

    // get the folder and the child
    var folder = shell.NameSpace(Path.GetDirectoryName(path));
    var item = folder.ParseName(Path.GetFileName(path));

    // get the item's property by it's canonical name. doc says it's a string
    string bpm = item.ExtendedProperty("System.Music.BeatsPerMinute");
    Console.WriteLine(bpm);
}

这篇关于如何在Windows Forms App中获取MP3文件的BPM属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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