How do I figure out if Windows is currently playing any sounds? - c#

How can I figure out if Windows is currently playing any sounds through the primary audio device? I need to know, so that I can make my program automatically adjust its volume.

You could use CSCore.
Download it right here -> http://cscore.codeplex.com/
Paste these lines on a console project.
using System;
using CSCore.CoreAudioAPI;
namespace AudioDetector
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(IsAudioPlaying(GetDefaultRenderDevice()));
Console.ReadLine();
}
public static MMDevice GetDefaultRenderDevice()
{
using (var enumerator = new MMDeviceEnumerator())
{
return enumerator.GetDefaultAudioEndpoint(DataFlow.Render, Role.Console);
}
}
public static bool IsAudioPlaying(MMDevice device)
{
using (var meter = AudioMeterInformation.FromDevice(device))
{
return meter.PeakValue > 0;
}
}
}
}
Play a music be it on YouTube, Music Player, etc...
Run the program.
It automatically notifies(true/false) if there is an audio currently being played or not.

You may need to mess around with mixer controls.
Mixer Control
These may help ya out too.
Measure speaker volume by recording playing sound with microphone
Using p/invoke and win-api to monitor audio line-in (C#)

Related

Using C# to control media en application volumes

I'm trying to build my own mixer panel and got the hardware and software communication side working now all i need is to control the media on my computer using this. How do i control the media en stuff like volume on a window's computer the program i made is written en C# and communicates via serial to my mixer board based on Arduino. I searched around but i can't find a lot about controlling media using C# or volume control there seems to be nothing in the standard C# libraries about this.
I want it to be able to skip songs or go back and do the standard media control like this. I also got different sliders for volume and want to control the volume of different applications using this.
For controlling media you can use virtual keyboard inputs by hooking into the user32 DLL to virtual press the media control keys. This way you can do things like mute and skip to next track, but the Volume Control is limited to just controlling the main device volume just like a normal keyboard. Below is a simple code snipped of how to do this.
public class VirtualMediaController
{
internal enum VirtualKeyCodes
: uint
{
VOLUME_MUTE = 0xAD,
VOLUME_DOWN = 0xAE,
VOLUME_UP = 0xAF,
NEXT_TRACK = 0xB0,
PREVIOUS_TRACK = 0xB1,
STOP = 0xB2,
PLAY_PAUSE = 0xB3,
LAUNCH_MEDIA = 0xB5,
}
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
internal static extern void keybd_event(uint bVk, uint bScan, uint dwFlags, uint dwExtraInfo);
internal static void SendKey(VirtualKeyCodes virtualKeyCode)
{
keybd_event((uint)virtualKeyCode, 0, 0, 0);
}
public static void Mute()
{
SendKey(VirtualKeyCodes.VOLUME_MUTE);
}
public static void VolumeDown()
{
SendKey(VirtualKeyCodes.VOLUME_DOWN);
}
public static void VolumeUp()
{
SendKey(VirtualKeyCodes.VOLUME_UP);
}
public static void NextTrack()
{
SendKey(VirtualKeyCodes.NEXT_TRACK);
}
public static void PreviousTrack()
{
SendKey(VirtualKeyCodes.PREVIOUS_TRACK);
}
public static void Stop()
{
SendKey(VirtualKeyCodes.STOP);
}
public static void PlayPause()
{
SendKey(VirtualKeyCodes.PLAY_PAUSE);
}
public static void LaunchMedia()
{
SendKey(VirtualKeyCodes.LAUNCH_MEDIA);
}
}
For more accurate Volume Control you will have to use a library or build one that will hook into the windows mixer API. CoreAudio Github is an example of such a library and can be used as a jumping of point. It can be used to control audio output devices and gives you more fin grain control over the audio then just using a visual keyboard for the media keys (volume up, volume down and mute). This way you can also get information like current volume and mute status. But this type of library wont give you the play/pause and stop functionality so a combination of both options is best.

SoundPlayer adjustable volume

I have a class Sounds.cs that manages playing sounds in my form, and I want to be able to adjust the volume to decimal value. Is there a way to change the volume of a sound being played with a SoundPlayer object? Or is there perhaps a different way to play sound that makes this possible?
Unfortunately SoundPlayer doesn't provide an API for changing the volume. You could use the MediaPlayer class:
using System.Windows.Media;
public class Sound
{
private MediaPlayer m_mediaPlayer;
public void Play(string filename)
{
m_mediaPlayer = new MediaPlayer();
m_mediaPlayer.Open(new Uri(filename));
m_mediaPlayer.Play();
}
// `volume` is assumed to be between 0 and 100.
public void SetVolume(int volume)
{
// MediaPlayer volume is a float value between 0 and 1.
m_mediaPlayer.Volume = volume / 100.0f;
}
}
You'll also need to add references to the PresentationCore and WindowsBase assemblies.

How can i add Pause and Play funtion in one button in windows media player using c#?

I am currently making windows media player and facing the problem with pause and play function in one button.
Here is my Code its not working except pausing the video. how can i resume the video again by same (pause) button ?
In Class :
public void play(AxWMPLib.AxWindowsMediaPlayer p1)
{
p1.Ctlcontrols.play();
}
public void pause(AxWMPLib.AxWindowsMediaPlayer p1)
{
p1.Ctlcontrols.pause();
}
myClass1 mc1 = new myClass1();
mc1.play(axWindowsMediaPlayer1);
mc1.pause(axWindowsMediaPlayer1);
You can check the Media Players playStateproperty and then Play or Pause it.
To reuse the code you already have, you can add the following criteria:
myClass1 mc1 = new myClass1();
if (axWindowsMediaPlayer1.playState == WMPLib.WMPPlayState.wmppsPlaying)
{
mc1.pause(axWindowsMediaPlayer1);
}
else
{
mc1.pause(axWindowsMediaPlayer1);
}
You can read the documentation here
Windows media player has a useful property Ctlcontrols that gives you many options:
To pause:
axWindowsMediaPlayer1.Ctlcontrols.pause();
And another options:
axWindowsMediaPlayer1.Ctlcontrols.stop();
axWindowsMediaPlayer1.Ctlcontrols.currentPosition = // a double value...
axWindowsMediaPlayer1.Ctlcontrols.play();
.....

Detect tap in Samsung Gear VR

how can I detect tap in Gear Vr to make an action
I use unity 5 with C# programming language
my tries
I read answers in untiy3d forums
none of them work to me
http://forum.unity3d.com/threads/samsung-gear-vr-detect-tap-swipe.298346/
any suggestions
You have to implement the tap, (or in reality a click, as the touchpad works as a mouse) yourself. A tap is a touch/mouse down, and then a touch/mouse up in a relatively same place.
Here's some untested code that should work (call if it doesn't):
using UnityEngine;
public class ClickDetector:MonoBehaviour {
public int button=0;
public float clickSize=50; // this might be too small
void ClickHappened() {
Debug.Log("CLICK!");
}
Vector3 pos;
void Update() {
if(Input.GetMouseButtonDown(button))
pos=Input.mousePosition;
if(Input.GetMouseButtonUp(button)) {
var delta=Input.mousePosition-pos;
if(delta.sqrMagnitude < clickSize*clickSize)
ClickHappened();
}
}
}
Thanks to #chanibal I find answer
Input.GetMouseButtonDown(0)
but I face another problem , application crush
is there any custom configuration to Gear VR

Play Sounds in a Visual Studio Application

I'm making a program in Visual Studio 2015 (C#) and I want to add sound effects to it. However, I have looked up countless tutorials but none of them seem to work, and gave me tons of errors. If anyone can give me a code to play a .wav file from resource files then I would be very grateful
How to: Play Sounds in an Application
Add the following method code under the button1_Click event hander :
System.Media.SoundPlayer player =
new System.Media.SoundPlayer();
player.SoundLocation = #"C:\Users\Public\Music\Sample Music\xxxx.wav";
player.Load();
player.Play();
If the file you want to play is wav files, try this.
var player = new System.Media.SoundPlayer("c:\\tes.wav");
player.Play();
For myself I wrote this SounceController, hope it help:
using System.Windows.Media; // add reference to system.windows.presentation.
using System;
using System.IO;
public class SoundController
{
private bool isPlaying;
private MediaPlayer player;
public SoundController()
{
player = new MediaPlayer();
}
~SoundController()
{
player = null;
}
public void Play(string path)
{
if (!File.Exists(path) || isPlaying)
return;
isPlaying = true;
player.Open(new Uri(path));
player.Play();
}
public void Stop()
{
if (isPlaying)
{
isPlaying = false;
player.Stop();
}
}
}
I recommend you use PInvoke To play sound using winmm.dll
first of all import System.Runtime.InteropServices namespace in to your project.
using System.Runtime.InteropServices;
Then in your class you will have
[DllImport("winmm.dll")]
static extern Int32 mciSendString(string command, StringBuilder buffer, int bufferSize, IntPtr hwndCallback);
public void Play(string path ,string name)
{
// Open
mciSendString($#"open {path} type waveaudio alias {name}", null, 0, IntPtr.Zero);
// Play
mciSendString($#"play {name}", null, 0, IntPtr.Zero);
}
You can play the sound sending correct path of wave file with name. . given name does not need to be the same name of wave file.for example:
Play(#"C:\soundeffect.wav", "soundEffect1");
Usually sound effects are played simultaneously. you can call this method several times to play several files simultaneously.
Play(#"C:\soundeffect1.wav", "soundEffect1");
Play(#"C:\soundeffect2.wav", "soundEffect2");
Play(#"C:\soundeffect3.wav", "soundEffect3");

Categories