LibVLCSharp how to get list of webcams - c#

VLC GUI shows the list of available webcams, like v4l2:///dev/video0 and v4l2:///dev/video1, I am wondering is there a way to get a list of available webcams? what about their default resolution?
I tried this but md.MediaList is empty.
var mds = libVlc.MediaDiscoverers(MediaDiscovererCategory.Devices);
if (mds.Any(x => x.LongName == "Video capture"))
{
var devices = mds.First(x => x.LongName == "Video capture");
var md = new MediaDiscoverer(libVlc, devices.Name);
foreach (var media1 in md.MediaList)
{
// Nothing ...
}
}

Your MediaDiscoverer is empty because you never call md.Start().
For more info, I found this really helpful: /LibVLCSharp/MediaDiscoverer.cs
That being said, I had no success using MediaDiscoverer to look for webcams myself.
If you don't insist on using LibVLC, you can list all camera devices without any third party software: How can I get a list of camera devices from my PC C#
from Francesco Bonizzi:
public static List<string> GetAllConnectedCameras()
{
var cameraNames = new List<string>();
using (var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_PnPEntity WHERE (PNPClass = 'Image' OR PNPClass = 'Camera')"))
{
foreach (var device in searcher.Get())
{
cameraNames.Add(device["Caption"].ToString());
}
}
return cameraNames;
}

Related

Why are the print capabilities always empty?

I'm getting a print queue for various printers installed locally by doing:
var queueName = "Myprinterqueue";
using (var ps = new PrintServer())
using (var pq = ps.GetPrintQueue(queueName))
{
pq.Refresh();
var status = pq.QueueStatus;
var jobs = pq.NumberOfJobs;
var averagePagesPerMinute = pq.AveragePagesPerMinute;
}
Status and NumberOfJobs are retrieved correctly. But many other print capabilities, eg. AveragePagesPerMinute, are always empty or 0.
Why is this?

C# - Get COM Ports with their friendly name using WMI

I'm developing a C# solution and I want to get the COM Ports, the description and the friendlyName (if they are bluetooth).
After investigating a bit, I've found that I can get the COM Ports using WMI/CIMV2/Win32_PnPEntity by searching the Name and Description values.
To find the friendly name I need to search on Win32_PnPSignedDriver and take the value of FriendlyName
Is there a way to match them to get a list like this?
COM56 - Bluetooth device - MyBTHDeviceName1
COM76 - Bluetooth device - MyBTHDeviceName2
COM5 - Serial device -
I attach the code that I have right now to get the first two fields.
// Method to retrieve the list of all COM ports.
public static List<PortInfo> FindComPorts()
{
List<PortInfo> portList = new List<PortInfo>();
ConnectionOptions options = PrepareOptions();
ManagementScope scope = PrepareScope(Environment.MachineName, options, #"\root\CIMV2");
// Prepare the query and searcher objects.
ObjectQuery objectQuery = new ObjectQuery("SELECT * FROM Win32_PnPEntity WHERE ConfigManagerErrorCode = 0");
ManagementObjectSearcher portSearcher = new ManagementObjectSearcher(scope, objectQuery);
using (portSearcher)
{
string caption = null;
// Invoke the searcher and search through each management object for a COM port.
foreach (ManagementObject currentObject in portSearcher.Get())
{
if (currentObject != null)
{
object currentObjectCaption = currentObject["Caption"];
if (currentObjectCaption != null)
{
caption = currentObjectCaption.ToString();
if (caption.Contains("(COM"))
{
PortInfo portInfo = new PortInfo();
portInfo.Name = caption.Substring(caption.LastIndexOf("(COM")).Replace("(", string.Empty).Replace(")", string.Empty);
portInfo.Description = caption;
portList.Add(portInfo);
}
}
}
}
}
return portList;
}
Thanks by advance.
The "friendly name" you are looking for is only suitable when COM Ports are virtual (as I assume they are on your example). I think that you can just get the information you need looking for the name property on Win32_PnPEntity class. There is no need to search for aditional information on COM ports as you will get all information already on Win32_PnPEntity class.
You can also try using ORMi and using strong typed objects for that.

Microphone being used in .NET

Is there a way to find out whether a microphone/recording device is currently being used by an application in Windows?
I am aware that by using NAudio one can easily get a list of all recording devices as follows:
public void getAudioDevices()
{
DeviceNameByID = new Dictionary<string, string>();
var enumerator = new NAudio.CoreAudioApi.MMDeviceEnumerator();
// Allows you to enumerate rendering devices in certain states
var endpoints = enumerator.EnumerateAudioEndPoints(
DataFlow.Capture,
DeviceState.Active);
foreach (var endpoint in endpoints)
{
float volumeLevel = endpoint.AudioEndpointVolume.MasterVolumeLevelScalar;
Debug.WriteLine($"Volume: {volumeLevel}");
DeviceNameByID[endpoint.ID] = endpoint.DeviceFriendlyName;
Debug.WriteLine($"{endpoint.ID}: {endpoint.DeviceFriendlyName} - {endpoint.State}");
}
NotificationClient nClient = new NotificationClient();
// Aswell as hook to the actual event
enumerator.RegisterEndpointNotificationCallback(nClient);
}
but I am not sure as to how/if one can get any knowledge on their current usage.

C# get portable devices connected to PC

I am trying to get all usb devices(including portable devices) on Windows 7
now I searched all over and didnt find a good answer.
I tried this code:
static void Main(string[] args)
{
//
// Get an instance of the device manager
//
PortableDeviceApiLib.PortableDeviceManagerClass devMgr
= new PortableDeviceApiLib.PortableDeviceManagerClass();
//
// Probe for number of devices
//
uint cDevices = 1;
devMgr.GetDevices(null, ref cDevices);
//
// Re-allocate if needed
//
if (cDevices > 0)
{
string[] deviceIDs = new string[cDevices];
devMgr.GetDevices(deviceIDs, ref cDevices);
for (int ndxDevices = 0; ndxDevices < cDevices; ndxDevices++)
{
Console.WriteLine("Device[{0}]: {1}",
ndxDevices + 1, deviceIDs[ndxDevices]);
}
}
else
{
Console.WriteLine("No WPD devices are present!");
}
}
but i get this error:
interop type 'portabledeviceapilib.portabledevicemanagerclass' Cannot
be embedded
Now im pretty stuck.
If you could help me with this code/ give me an idea what should i try, ill be happy
all I need is to get which type of USB got connected,
if a phone is connected, or a mouse. i want to know what is connected.
Thanx Ahead
I am using the NuGet package PortableDevices (which is based on the tutorial by Christophe Geers).
Derived from part one of the tutorial:
public void ListDevices()
{
var devices = new PortableDeviceCollection();
devices.Refresh();
foreach (var device in devices)
{
device.Connect();
Console.WriteLine(#"DeviceId: {0}, FriendlyName: {1}", device.DeviceId, device.FriendlyName);
device.Disconnect();
}
}
To expand on #CodeFox's answer, and in order to make his code ListDevices() work:
Download the NuGet package PortableDevices
Add references to these 4 COM libraries:
PortableDeviceClassExtension
PortableDeviceConnectApi
PortableDeviceTypes
PortableDeviceApi
Take the dll's under obj\Debug and put them into bin\Debug:
Interop.PortableDeviceClassExtension.dll
Interop.PortableDeviceConnectApiLib.dll
Interop.PortableDeviceTypesLib.dll
Interop.PortableDeviceApiLib.dll
Now you can use this function, although FriendlyName does not seem to be working (it returns an empty string):
private IDictionary<string, string> GetDeviceIds()
{
var deviceIds = new Dictionary<string, string>();
var devices = new PortableDeviceCollection();
devices.Refresh();
foreach (var device in devices)
{
device.Connect();
deviceIds.Add(device.FriendlyName, device.DeviceId);
Console.WriteLine(#"DeviceId: {0}, FriendlyName: {1}", device.DeviceId, device.FriendlyName);
device.Disconnect();
}
return deviceIds;
}
The next step for me is getting the contents from the device, which is done like so:
var contents = device.GetContents();

WMPLib: player.mediaCollection.getAll().count is always 0

I am attempting to write code that reads each item from the user's Windows Media Player library. This code works for the majority of users, but for some users, getAll() will return an empty list when they clearly have hundreds or thousands of items in their Windows Media Player library.
var player = new WindowsMediaPlayer();
var collection = player.mediaCollection;
var list = collection.getAll();
int total = list.count;
I am referencing the WMPLib namespace by adding a COM reference to wmp.dll. My application ships with Interop.WMPLib.dll. How would some users' machines be configured in such a way that they run Windows Media Player with many songs in their library, but WMPLib fails to function correctly? Furthermore, what workarounds exist to reliably read the user's Windows Media Player library in all cases?
Try this snippet and see if it works for you.
public List<MusicEntry> GetMusicLibrary()
{
List<MusicEntry> entries;
IWMPPlaylist mediaList = null;
IWMPMedia mediaItem;
try
{
// get the full audio media list
mediaList = media.getByAttribute("MediaType", "Audio");
entries = new List<MusicEntry>(mediaList.count);
for (int i = 0; i < mediaList.count; i++)
{
mediaItem = mediaList.get_Item(i);
// create the new entry and populate its properties
entry = new MusicEntry()
{
Title = GetTitle(mediaItem),
Album = GetAlbum(mediaItem),
Artist = GetArtist(mediaItem),
TrackNumber = GetTrackNumber(mediaItem),
Rating = GetRating(mediaItem),
FileType = GetFileType(mediaItem)
};
entries.Add(entry);
}
}
finally
{
// make sure we clean up as this is COM
if (mediaList != null)
{
mediaList.clear();
}
}
return entries;
}
For more information refer to this excellent article on Code Project.
http://www.codeproject.com/Articles/36338/Export-Windows-Media-Player-Music-Metadata-to-XML

Categories