C# Windows Form App - choose output audio device - c#

I'm trying to do some basic app, with System.Speech.Synthesis in c#
It's just a basic form with a text input and after pressing enter it reads the text using ss.SpeakAsync();
The goal is to play that sound on a exact audio output device on Windows but I can't achieve that.
I've tried do it by windows settings, App volume and device preferences, but it doesn't work, not sure why.
I'm setting the correct device, but it plays on the "default" anyways, no matter what I do. It works with any other application correctly (like browser for example) bit with mine it doesn't. It works when I change all system audio to that device, but the main goal is that i want only this app to use "cable input".
I've tried to use some 3rd party software to do this (https://github.com/a-sync/audio-router) but it doesn't work either. I set everything, and when I click "play" in my app, then my app crashes with an exception: „[17132] TrayReader.exe” exited with code -1073741819 (0xc0000005) 'Access violation'.
So maybe I'm able to set in the code the correct audio output device? Or how do I fix those previous errors? maybe I need to add some privileges or something? I'm mainly web developer, this is my first time with windows app.
//edit -> it works, when i change all my system audio to cable input, play some sound, and then go back to my main device. It seems like a windows bug?

you may use a 3rd lib Naudio; the example below shows how to return to current position in case the player was playing a song. The key code is player.DeviceNumber = deviceNum; in which deviceNum is 0, 1, 2... depending how many speakers are available in your PC.
A disadvantage of Naudio is that it loads completely the song before playing, and quite slow.
var playback = player.PlaybackState;
if (playback == PlaybackState.Stopped) // change and exit
{
player.DeviceNumber = deviceNum;
return;
}
var currentTime = reader.CurrentTime;
player.Stop(); // must stop to change output
player.DeviceNumber = deviceNum;
player.Init(reader);
reader.CurrentTime = currentTime;
if (playback == PlaybackState.Playing)
player.Play();

Related

Play sound when iPhone sound switch is on silent mode in xamarin.iOS

I want to play video with full sound, even i have the sound switch(positioned above volume buttons) set to silent mode. What i did is working on iOS 7.x and 8.1.x but doesn't work on 8.2. It's quite weird, not really sure. How to go about it. Any sample code will be appreciated.
I don't want to change to AVPlayer,at this stage, as i am almost ready for release
Here's what i have already tried and works in 7.x and 8.1.x
//I declare a class level variable
MPMusicPlayerController VolumeRocker;
//initialize it
VolumeRocker = MPMusicPlayerController.ApplicationMusicPlayer;
//Use it. I set this inside the click even of button
//which i use to start playback of video
VolumeRocker.Volume = 1f;
Am i missing something ? This works in following scenarios in 7.x and 8.1.x,(but doesn't work on iOS 8.2, not sure abt 8.3)
1) I switch to silent mode and start playing the video. I get to see the volume popup and i can hear the sound
2) I set the volume to zero with sound switch set to ringer mode, i play the video , i get the volume popup. I can hear the sound, then i switch the sound mode to silent using the switch, i can still hear the sound. Even if i keep toggling the modes using the switch, it doesn't affect the volume of the video(which is what i need).
You need to configure your audio session by setting the Category property:
http://iosapi.xamarin.com/?link=P%3aAudioToolbox.AudioSession.Category
An explanation of the possible values and how to control them are available here:
http://iosapi.xamarin.com/?link=T%3aAudioToolbox.AudioSessionCategory

C# Windows Phone 8 - Catastrophic Failure E_Unexpected when playing media

This is driving me bonkers! When trying to play a sound or any media element in a windows phone 8 app I get "Catastrophic failure (Exception from HRESULT: 0x8000FFFF (E_UNEXPECTED))"
And that is all it says. If I look at the stack trace it shows: at Windows.UI.Xaml.Controls.MediaElement.Play()
Example code is:
XAML:
MediaElement x:Name="sndClick" Source="Assets/Sounds/Click.wav" Volume="10" AutoPlay="False"
Code:
sndClick.play();
I am trying to port a Windows 8 app to Windows Phone 8. The Windows 8 app works perfectly with the exact same code. This must be some issue with the Windows Phone. Everything else works, it just crashes when the phone trys to play media.
Not all the elements that play sounds crash the program, unless you click them a few times. Some elements always crash on the first click. No sounds ever play, even if the program doesn't crash with the exception it never plays any sounds.
ONE time (and only one time) it played the first couple frames of one of the animation files (.wmv) and then crashed.
It is a weird problem. All the code is copied from the working Windows 8 program and everything works except for the media. If I comment out the sound.play()'s and I disable the media player the program works fine.
At first I thought it might be that the resources weren't copying so I set the "Copy to Output Directory" to "Always Copy". No effect.
I tried the simulators and I tried it on physical hardware, it is the same on everything.
It isn't a resource issue, the simulators and the hardware have more than enough system resources, the whole app is only 22mb's.
It's not a codec issue, the animation is a .wmv file and the sounds are all .wav. Both should be natively supported on any windows device.
I have searched around and seen others with similar problems but I haven't seen any solutions. Does anybody know what is causing this issue and how to fix it? I would very much appreciate it.
I am pulling my hair out on this.
Thanks,
-RW
OK I fix it.
Windows Phone 8.1 simply hates it when there is more than one MediaElement in the program... Even just playing one at a time throws the error and wont play any sounds.
It is annoying. It is further annoying that it doesn't seem that XNA works in 8.1 (at least I couldn't get it to work)
The solution was to use SharpDX.
Special Thanks to this post: Multiple audio stream in Universal App(Runtime API), XNA SoundEffect replacement
I removed all but one MediaElement and added installed SharpDX and SharpDX Xaudio2 from the package installer thing. Then:
using SharpDX;
using SharpDX.XAudio2;
using SharpDX.IO;
using SharpDX.Multimedia;
private void PlaySound(string strPath)
{
XAudio2 xAudio = new XAudio2();
var masteringVoice = new MasteringVoice(xAudio);
var nativeFileStream = new NativeFileStream(strPath, NativeFileMode.Open, NativeFileAccess.Read, NativeFileShare.Read);
SoundStream stream = new SoundStream(nativeFileStream);
var waveFormat = stream.Format;
AudioBuffer buffer = new AudioBuffer
{
Stream = stream.ToDataStream(),
AudioBytes = (int)stream.Length,
Flags = BufferFlags.EndOfStream
};
var sourceVoice = new SourceVoice(xAudio, waveFormat, true);
sourceVoice.SubmitSourceBuffer(buffer, stream.DecodedPacketsInfo);
sourceVoice.Start();
}
And then just:
PlaySound("Assets/Sounds/Click.wav");
It works quite well! And the one remaining MediaElement that I need for an animation works now. My hair is even starting to grow back (though it is growing back grey).
If anyone else is having this issue, this is the fix. I hope it helps someone.

Windows Phone 8 - 2 Background Audios Clash and both of the App terminates

around a week ago, I submitted an online Background Radio Streaming app for the Windows Phone store. The app was quite good (as I used the Emulator to test it, it was good on all the possible sectors) but when I submitted it for certification, it failed.
According the the error log, if someone is already playing a Music from Music + Video hub and then tries to open this app, both of the apps Crash and stop unexpectedly.
So far I understood, it is because the Music of Music + Video hub is also Background Music and for playing 2 Background Musics at the same time, the apps are Crashing. It can be some other reason but the described one seemed more logical to me.
So, is there anyone who can tell me how to change the state of the app of Music + Video hub? I want to pause or stop the app of Music + Video hub for the time being so that both of the states of the app are not same. In that way, the apps won't clash with each other in the background.
Can anyone help me in this regard?
Use gameHasControl to check for other BAP using music:
bool gameHasControl = Microsoft.Xna.Framework.Media.MediaPlayer.GameHasControl;
if (!gameHasControl)
{
MessageBox.Show("stopping other player"); // normally you should ask if to do that
BackgroundAudioPlayer.Instance.Stop();
}
Once it is Stopped, when you start your BAP, then old instance invokes Shutdown(), and your BAP will be new Instance, which you can normally use. The same is when your BAP is in memory and you start to play from Music+Video Hub.
Only watch out, because when you use XNA, you sometimes need to do:
FrameworkDispatcher.Update();
Otherwise your App will sometimes crash. Good luck.
EDIT - after comment
To make it work you need to add a reference to Microsoft.Phone.BackgroundAudio or use like this:
Microsoft.Phone.BackgroundAudio.BackgroundAudioPlayer.Instance.Stop();
BackgroundAudioPlayer is a Singleton which you are allowed to use - in this case - Stop it playing (of course give the choice to the User).

Change barcode scan beep volume for motorola devices

I am developing some .net applications with c# for various Motorola devices running Windows Mobile and Windows CE. These include the MC9190 and the WT41N0. On these two models, it beeps very loudly when a barcode is scanned. Is there anyway using the Motorola emdk or by changing a registry setting to make the beep quieter without turning the beep off altogether.
I don't know if this works for all Motorola devices, but you could try including Symbol.Audio and:
using Symbol.Audio;
...
using (StandardAudio audio = new StandardAudio(Device.AvailableDevices[0]))
{
audio.BeeperVolume = 1;
}
You can inspect the audio.BeeperVolumeLevels property to see what the maximum volume level is.
In Symbol scanner terminology this is called "feedback", and by default it's the local feedback setting that gets in the way of properly managing the audio.
In your application, after you create the reader, the following settings control it:
_BarcodeReader.Parameters.Feedback.Success.BeepFrequency = 2000
_BarcodeReader.Parameters.Feedback.Success.BeepTime = 20
_BarcodeReader being the reader object. I set it to lower frequency for a shorter time above.
If you need to eliminate the beeping altogether:
_BarcodeReader.Parameters.Feedback.Success.BeepTime = 0
You may want to add your own wave file:
_BarcodeReader.Parameters.Feedback.Success.WaveFile = "decode.wav"
This is also the case with the DataWedge software that ships with MC9xxx and MC3xxx series. If you're using DataWedge, look for "local feedback" under the feedback menu, from Scanner, under Basic configuration.
The scan audio can only be managed from the feedback settings, volume control and other methods have no effect. It seems Zebra (previously Motorola) used high-volume default for loud industrial settings, wish they had considered a better way to configure though.
I put a piece of Scotch tape over the speaker on mine.
The tape dampens most of the noise while I am testing here at my desk, but it is simple to remove so they can hear it out on the floor.
Outside or on our production floor, they need the loud noise.
If that doesn't work for you, in the Settings on our Datalogic Falcons, there is a Decoding application. One of the inputs there is called Audio and enables someone to turn down the volume.
Since I don't have the SDK for the Datalogic Falcon, I can only post a low resolution cell phone clip. Hope this helps.
There is a configuration utility for motorola devices. It is a single exe file you can put on your device and then you can adjust several settings and also the beeper volume. I had a look on support.symbol.com but I didn't found it. I think you can get it from your vendor-support.
In our situation with a Motorola MC9590 device using telnet sessions, the volume control on the left-side of the device needs to be adjusted down prior to signing into the telnet session. The volume level remains at the established setting once the user signs into their telnet session.
The volume can be controlled by pressing the blue function key and then the H or M key. The display brightness can be controlled by pressing the blue function key and then the D or I key.
the way I made it silence was : go to control panel -> All control panel items -> Devices and Printers. find the Symbol bar code and right click and select Keyboard settings, on the speed box you will see a Repeat delay icon just drag that all the way to the left and the sound should go away.

Playing Stream Audio with MPMoviePlayerController in Background Modes

my first question here...
I have been searching how solve this issue i start a app using monotouch creating my MPMoviePlayerController like this:
this.mp = new MPMoviePlayerController(new NSURL("http://stream3.dyndns.org:1935/iphone/xeco.stream/playlist.m3u8"));
this.mp.useApplicationAudioSession=false //also try with true
this.mp.PrepareToPlay();
this.mp.Play();
Everything works great, i hear the audio
Notes:
I already search how fix this and on my plist file add UIBackgroundModes and string audio as many members advice. (Using the plist editor provided in monotouch). Also add this code to set the category of my audio session:
AudioSession.Initialize();
AudioSession.Interrupted += delegate{
Console.Writeline("interrupted");
}
AudioSession.Category = AudioSessionCategory.MediaPlayback; //also try ambientsound and others
The problem is:
How can i keep the audio playing when the app goes background, when the device blocks or the home button is pushed?
The stream always seems get muted when press home or block the device, when i return to the app the audio starts very quickly wich make me think that the app is streaming all the time but only doesnt hear.
I am testing only on the Iphone Simulator, i am starting to think that maybe this is caused only on simulator. any advice?
thanks in advance.
UPDATE:
I receive my licenses of monotouch and my register on the iOS Dev Program, so i test my app on the device. It works, the important part is on the AudioSession part:
AudioSessionCategory.MediaPlayback;
and when use the player on:
this.mp.useApplicationAudioSession=false
I receive my licenses of monotouch and my register on the iOS Dev Program, so i test my app on the device. It works, the important part is on the AudioSession part:
AudioSessionCategory.MediaPlayback;
and when use the player on:
this.mp.useApplicationAudioSession=false
The app works great on the device. Monotouch rules!

Categories