How to control SoundPlayer WhilePlaying and AfterPlay Events? - c#

I have Play and Stop buttons and I want to combine them into one button which works in two functions. However, I couldn't find any WhilePlaying() or AfterPlay() events. I googled some and I've found that using Thread may control events that I want.
Here is what I want. When User clicks the Play Button, Play icon will change to Stop icon and user will be able stop playing immediately or if sound finishes playing Stop icon will change to Play icon.
Here are the codes that I used in my program:
private void btPlayFile_Click(object sender, EventArgs e)
{
try
{
wavPlayer = new SoundPlayer();
string promptPath = System.Configuration.ConfigurationSettings.AppSettings["PromptStorage"];
wavPlayer.SoundLocation = promptPath + tePromptFile.Text;
wavPlayer.LoadCompleted += new AsyncCompletedEventHandler(wavPlayer_LoadCompleted);
wavPlayer.LoadAsync();
}
catch (Exception ex)
{
XtraMessageBox.Show("PlayFile Hata:" + ex.Message);
}
}
void wavPlayer_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
try
{
((System.Media.SoundPlayer)sender).Play();
}
catch (Exception ex)
{
XtraMessageBox.Show("PlayFile Hata:" + ex.Message);
}
}
private void btStopFile_Click(object sender, EventArgs e)
{
try
{
wavPlayer.Stop();
}
catch (Exception ex)
{
XtraMessageBox.Show("PlayFile Hata:" + ex.Message);
}
}

Related

when to use await key word? do I need it in this case?

i have a onClick method on a buttom. when this button is pressed, i get a displayalert message.
getTokenQ has a value. my question why its going inside catch block?
private async void Button_Clicked(object sender, EventArgs e)
{
try
{
var getTokenQ = await SecureStorage.GetAsync("Save_Security_Question");
if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
{
}
else
{
}
}
catch (Exception ex)
{
await DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
}
}
no you don't need to do that, you can use .Result if you don't want to execute your code asynchronously , this code is perfectly fine :
private void Button_Clicked(object sender, EventArgs e)
{
try
{
var getTokenQ = SecureStorage.GetAsync("Save_Security_Question").Result;
if ((String.IsNullOrWhiteSpace(getTokenQ) == false))
{
}
else
{
}
}
catch (Exception ex)
{
DisplayAlert("Message", "Some thing went wrong. Please try different method", "ok");
}
}
Why in catch ! Because it's a message to display only if an exception is thrown.

How to programmatically enable Location on Windows

Is there a way to programmatically enable Location on Windows 10 using c#.NET winforms. I am able to get coordinates using the following code unless the user's Location is off.
GeoCoordinateWatcher watcher = new GeoCoordinateWatcher();
watcher.StatusChanged += Watcher_StatusChanged;
watcher.Start(false);
private void Watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
{
try
{
if (e.Status == GeoPositionStatus.Ready)
{
if (!watcher.Position.Location.IsUnknown)
{
latitude = watcher.Position.Location.Latitude;
longitude = watcher.Position.Location.Longitude;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Location");
}
}

Media element "mediaPlayer_MediaEnded" not firing end of the voice

I just try to play vocie file using wpf mediaPlayer. here is sample code that i used. but mediaPlayer.MediaEnded += mediaPlayer_MediaEnded event not firing end of the voice.
private void PlayVoice(string voiceFileURL)
{
try
{
mediaPlayer = new MediaPlayer();
mediaPlayer.MediaEnded += mediaPlayer_MediaEnded;
mediaPlayer.Open(new Uri(voiceFileURL.Replace("https://","http://")));
for (int i = 0; i < 3; i++)
{
try
{
mediaPlayer.Play();
break;
}
catch (Exception)
{
System.Threading.Thread.Sleep(1000);
}
}
}
catch (Exception Ex)
{
}
finally
{
}
}
void mediaPlayer_MediaEnded(object sender, EventArgs e)
{
mediaPlayer.Stop();
}
I would suggest you to try
LoadedBehavior = Manual also UnloadedBehavior = MediaState.Stop
As MSDN link states
LoadedBehavior must be set to Manual in order to interactively control media with the Play, Pause, and Stop methods

Unable to Call Events using Zkemkeeper SDK

I am facing trouble in calling events in Zkemkeeper.dll. I have succesfully established the connection however on putting the finger on sensor no event is fired. Infact no realtime event is being triggered.
Any help would be appreciated following is my code;
private void button2_Click(object sender, EventArgs e)
{
string s = "";
int Val = 0;
bool bIsConnected = false;
try {
//zkemkeeper.CZKEMClass axczkem1 = new zkemkeeper.CZKEMClass();
// bIsConnected = axczkem1.Connect_USB(1);
bIsConnected = axczkem1.Connect_Com(6,1,115200);
if(bIsConnected==true){
Cursor = Cursors.Default;
bool asa= axczkem1.EnableDevice(1, true);
if (axczkem1.RegEvent(1, 65535))
{
axczkem1.OnFinger += new zkemkeeper._IZKEMEvents_OnFingerEventHandler(axczkem1_OnFinger);
axczkem1.OnKeyPress += new zkemkeeper._IZKEMEvents_OnKeyPressEventHandler(axczkem1_OnKeyPress);
axczkem1.OnConnected += new _IZKEMEvents_OnConnectedEventHandler(axCZKEM1_OnConnected);
axczkem1.OnVerify += new zkemkeeper._IZKEMEvents_OnVerifyEventHandler(axCZKEM1_OnVerify);
}
MessageBox.Show("Connection established!!!");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
And following are the event methods:
private void axCZKEM1_OnVerify(int UserID)
{
label2.Text = "Verify";
}
private void axCZKEM1_OnConnected()
{
label1.Text = "Connected";
}
private void axczkem1_OnKeyPress(int Key)
{
MessageBox.Show(Key.ToString());
}
private void axczkem1_OnFinger()
{
MessageBox.Show("Connection");
}
if this is a windows form application . If program have long running process event doesn't work . For example loop (while,for) . And also Thread.sleep() .
If you want to trigger work your program do nothing .
If this is not a windows form see this link enter link description here

Softphone on C# don't work incomming call

Here i have how i accept the call but when someon is calling to me it is rejecting here is what i have tried but still don't works outgoing calls are working but incoming not can you help me? there is 1 point that it is alawys showing me call state busy
static void call_CallStateChanged(object sender, CallStateChangedArgs e)
{
Console.WriteLine("Call state: {0}.", e.State);
if (e.State == CallState.Answered)
SetupDevices();
}
static void RegisterAccount(SIPAccount account)
{
try
{
phoneLine = softphone.CreatePhoneLine(account);
phoneLine.RegistrationStateChanged += line_RegStateChanged;
softphone.IncomingCall += softphone_IncomingCall;
softphone.RegisterPhoneLine(phoneLine);
}
catch (Exception ex)
{
MessageBox.Show("Error during SIP registration: " + ex);
}
What I have tried:
static void softphone_IncomingCall(object sender, VoIPEventArgs e)
{
call = e.Item;
call.CallStateChanged += call_CallStateChanged;
call.Answer();
}

Categories

Resources