Play sound file on Timer end - windows-phone-8

I have a countdown timer and I want an alarm to go off when it reaches the end ... lots for Android and iOS, but nothing that I could find for WP8.
I'm using the DispatcherTimer for counting down.
I'm looking for some information or an example on how to call a sound when the timer finishes counting down. I have the counter start on a button click and reset on a long press. It all works good, just want a sound effect when it stops after counting down.
private DateTime EndTime { get; set; }
private DispatcherTimer _dispatcherTimer;
private void BtnCounter_Click(object sender, RoutedEventArgs e)
{
if (this._dispatcherTimer == null)
{
this._dispatcherTimer = new DispatcherTimer();
this._dispatcherTimer.Interval = TimeSpan.FromMilliseconds(100);
this._dispatcherTimer.Tick += new EventHandler(dispatcherTimer_Tick);
}
if (this.EndTime == DateTime.MinValue)
{
this.EndTime = DateTime.Now + (TimeSpan)this.tsPicker.Value;
}
this._dispatcherTimer.Start();
}
void dispatcherTimer_Tick(object sender, EventArgs e)
{
var remaining = this.EndTime - DateTime.Now;
int remainingSeconds = (int)remaining.TotalSeconds;
this.tsPicker.Value = TimeSpan.FromSeconds(remainingSeconds);
if (remaining.TotalSeconds <= 0)
{
this._dispatcherTimer.Stop();
// Sound code should go here, or a method call to it.
}
}
private void BtnCounter_Hold(object sender, System.Windows.Input.GestureEventArgs e)
{
cnt = 0;
BtnCounter.Content = cnt;
this._dispatcherTimer.Stop();
this.EndTime = DateTime.MinValue;
this.tsPicker.Value = TimeSpan.FromSeconds(0);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
this.EndTime = DateTime.MinValue;
base.OnNavigatedFrom(e);
}

Have you tried using a MediaElement? There seems to be a good example using it, titled Windows Phone 8: Playing Sounds

Related

For unknown reason, a button click closes my window. How can I fix this?

I am trying to replicate a simple addition of 2 entries with a button in Monodevelop (shown how to make it step-by-step) but somehow the window closes 2 seconds after pressing the button without actually changing anything.
The code for the button:
using Gtk;
using Frontend.ChatService;
public partial class MainWindow : Gtk.Window
{
public MainWindow() : base(Gtk.WindowType.Toplevel)
{
Build();
}
protected void OnDeleteEvent(object sender, DeleteEventArgs a)
{
Application.Quit();
a.RetVal = true;
}
protected void OnButton1Clicked(object sender, EventArgs e)
{
ChatService client = new ChatService();
int x = Int32.Parse(entry1.Text);
int y = Int32.Parse(entry2.Text);
int sum = client.Add(x, y);
entry1.Text = sum.ToString();
}
}
And the sum (which I tested and think works):
using System;
using System.Web;
using System.Web.Services;
namespace Backend
{
public class ChatService : System.Web.Services.WebService
{
[WebMethod]
public int Add(int x, int y)
{
return x + y;
}
}
}
I left the main file program.cs as generated and is:
using System;
using Gtk;
namespace Frontend
{
class MainClass
{
public static void Main(string[] args)
{
Application.Init();
MainWindow win = new MainWindow();
win.Show();
Application.Run();
}
}
}
The window does pop up as it should and shows no problem until the button is pressed.
Edit:
I forgot to run the backend / server part, which is why the function was not found... (beginners mistake I guess)
Works now
The problem is probably that your code throws an exception you are not aware of. The problem is in the code that handles the button being clicked.
protected void OnButton1Clicked(object sender, EventArgs e)
{
ChatService client = new ChatService();
int x = Int32.Parse(entry1.Text);
int y = Int32.Parse(entry2.Text);
int sum = client.Add(x, y);
entry1.Text = sum.ToString();
}
Let's go line by line:
ChatService client = new ChatService();
Here you are creating a new instance of what it seems to be a system service or maybe a web services. This could throw if the service is not known (in the former case), or if the connection is interrupted or does not reach a destination, etc., in the latter case.
These lines are also delicate:
int x = Int32.Parse(entry1.Text);
int y = Int32.Parse(entry2.Text);
They will throw in case the field entry1 or entry2 are empty, or contain a letter...
In order to manage these cases you need to add try... catch blocks in the appropriate places. Alternately, you can use Int32.TryParse.
For example, assuming the service is in the web:
protected void OnButton1Clicked(object sender, EventArgs e)
{
ChatService client;
int x;
int y;
try {
client = new ChatService();
} catch(HttpRequestException exc) {
client = null;
var dlg = new Gtk.MessageDialog(
this,
Gtk.DialogFlags.Modal,
Gtk.MessageType.Error,
Gtk.ButtonsType.Ok,
"Connection error"
);
dlg.Text = exc.Message;
dlg.Run();
dlg.Destroy();
}
if ( client != null ) {
if ( !int.TryParse( entry1.Text, out x) {
entry1.Text = "0";
x = 0;
}
if ( !int.TryParse( entry2.Text, out y) {
entry2.Text = "0";
y = 0;
}
int sum = client.Add(x, y);
entry1.Text = sum.ToString();
}
}
Getting code which correctly handles errors is always harder, of course.
Hope this helps.

How can I erase specyfic pixels of image in windows phone by finger touching the screen

I'm new to Windows Phone programming, I'm having problems, and I've been looking over the internet for the past two days and found nothing.
I'm writing and app where I've got two images one over the other, and I need to "erase" the top one by finger, a bit like using a rubber, so I can see the picture underneath. I've done drawing in general but it's not what I need right now. I found an example of something similar but it doesn't work on the phone probably because of library differences. Can anyone please help me find something that I can use? Or maybe show anything?
I found something like this, and it would be awesome if I could do something like this in windows phone 8
Note, I'm using Microsoft Visual Studio Express 2012
After few days of work, i managed to get the erasable layer.
//Created by Kamil SokoĊ‚owski on 01.11.14
//Windows Phone 7.1 Build on Microsoft Visual Studio Express 2012 for Windows Phone
public partial class MainPage : PhoneApplicationPage
{
public BitmapImage mainImage;
private WriteableBitmap writeableBitmap;
private bool isRubbing;
private SolidColorBrush brush;
private Point currentPoint;
private Point oldPoint;
private bool touchedFirst;
// Constructor
public MainPage()
{
InitializeComponent();
Canvas mainCanwas = new Canvas();
drawBackgroundLayer(new Uri("main.png", UriKind.RelativeOrAbsolute));
drawImageLayer(new Uri("para.png", UriKind.RelativeOrAbsolute));
touchedFirst = true;
}
public void drawBackgroundLayer(Uri uri)
{
BitmapImage bgi = new BitmapImage();
bgi.UriSource = uri;
BackGroundImage.Source = bgi;
}
public void drawImageLayer(Uri uri)
{
mainImage = new BitmapImage();
mainImage.UriSource = uri;
MyImage.Source = mainImage;
}
private void Tap_leftButtonDown(object sender, System.Windows.Input.MouseEventArgs e)
{
if (touchedFirst)
{
WriteableBitmap wb = new WriteableBitmap(mainImage);
Color color = new Color();
color.A = 0;
color.R = 33;
color.G = 34;
color.B = 255;
brush = new SolidColorBrush();
brush.Color = color;
touchedFirst = false;
writeableBitmap = wb;
}
isRubbing = true;
oldPoint = currentPoint;
currentPoint = e.GetPosition(MyImage);
}
private void Mouse_MouseMove(object sender, System.Windows.Input.MouseEventArgs e)
{
if (isRubbing)
{
oldPoint = currentPoint;
currentPoint = e.GetPosition(MyImage);
for (int i = 0; i < 40; i++)
{
for (int a = 0; a < 40; a++)
{
writeableBitmap.DrawLine((int)currentPoint.X + i, (int)currentPoint.Y + a, (int)oldPoint.X + i, (int)oldPoint.Y + a, brush.Color);
}
}
MyImage.Source = writeableBitmap;
}
}
private void ButtonLetGo(object sender, MouseButtonEventArgs e)
{
isRubbing = false;
MyImage.Source = writeableBitmap;
}
}
}
// To get this to work You need to download the WiritableBitmapEx form NuGet

Recording sound in temporary file on windows phone 8

This is my code for recording sound in temporary file. when i record sound and then listen to playback, everything goes well, but when i click again on playback button, i get this error:
How can i solve this problem?
Code:
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Windows;
using System.Windows.Controls;
using Coding4Fun.Toolkit.Audio;
using Coding4Fun.Toolkit.Audio.Helpers;
namespace AudioRecorder.UserControls
{
public partial class SoundRecorderPanel : UserControl
{
private MicrophoneRecorder _recorder = new MicrophoneRecorder();
private List<IsolatedStorageFileStream> _audioList = new List<IsolatedStorageFileStream>();
private int _counter;
public SoundRecorderPanel()
{
InitializeComponent();
}
private void ButtonRecord_OnChecked(object sender, RoutedEventArgs e)
{
_recorder.Start();
}
private void ButtonRecord_OnUnchecked(object sender, RoutedEventArgs e)
{
_recorder.Stop();
SaveTempAudio(_recorder.Buffer);
}
private void SaveTempAudio(MemoryStream buffer)
{
if (_counter==2)
return;
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
var bytes = buffer.GetWavAsByteArray(_recorder.SampleRate);
var tempFileName = "tempwaveFile_"+_counter;
IsolatedStorageFileStream audioStream = isoStore.CreateFile(tempFileName);
audioStream.Write(bytes,0,bytes.Length);
_audioList.Add(audioStream);
_counter++;
}
}
private void ButtonPlayBack_OnClick(object sender, RoutedEventArgs e)
{
var index = int.Parse(((Button) sender).Tag.ToString());
var audioPlayer = new MediaElement {AutoPlay = true};
if (index < _audioList.Count)
{
audioPlayer.SetSource(_audioList[index]);
LayoutRoot.Children.Add(audioPlayer);
audioPlayer.Play();
}
}
}
}
You can 100% use a using block. Issue was how you were attempting to access the stream in the separate event. Reopen it rather than attempt to save a reference in an index to the stream.
using (var stream = new IsolatedStorageFileStream(_fileName, FileMode.Open, storageFolder))
{
playBack.SetSource(stream);
playBack.Play();
}
Use the sample code:
https://coding4fun.codeplex.com/SourceControl/latest#source/Coding4Fun.Toolkit.Test.WindowsPhone.Common/Samples/Audio.xaml.cs
I solved my problem , it's weird but it seems using(){} does not work ! and i disposed IsolatedStorageFile and IsolatedStorageFileStream manually . and also i changed the code under ButtonPlayBack click event . this is my new code for someone who has a same problem .
private void SaveTempAudio(MemoryStream buffer)
{
if (_counter == 2)
return;
var isoStore = IsolatedStorageFile.GetUserStoreForApplication();
var bytes = buffer.GetWavAsByteArray(_recorder.SampleRate);
var tempFileName = "tempwave_" + _counter;
var audioStream = isoStore.CreateFile(tempFileName);
audioStream.Write(bytes, 0, bytes.Length);
_audioList.Add(audioStream);
_counter++;
isoStore.Dispose();
audioStream.Close();
audioStream.Dispose();
}
private void ButtonPlayBack_OnClick(object sender, RoutedEventArgs e)
{
var index = int.Parse(((Button) sender).Tag.ToString());
var fileName = "tempwave_" + ((Button) sender).Tag;
if (index >= _audioList.Count)
return;
var isoStorage = IsolatedStorageFile.GetUserStoreForApplication();
var fileStream = isoStorage.OpenFile(fileName, FileMode.Open, FileAccess.Read);
SoundPlayer.SetSource(fileStream);
SoundPlayer.Play();
isoStorage.Dispose();
fileStream.Close();
fileStream.Dispose();
}

Windows Phone 8 Page Navigation

I have a problem that I can't really figure out, and I am really desperate now - I've no idea why it's happening:(
So here is the problem: I am writing a kind of Guess that Tune app. The first page is a menu page, that a user can press "Play" button, and he will navigate to a GenreSelectPage where he selects a genre and navigates to a GamePage. I wanted to handle BackButtonPress on GamePage - when a user hits BackButton, he navigates to MainPage, not GenreSelectPage. Here is the code:
private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
{
base.OnBackKeyPress(e);
this.player.Pause();
var result = MessageBox.Show(AppResources.GamePageAlert, "Warning", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
//NavigationService.RemoveBackEntry();
//App.RootFrame.Navigate(new Uri(#"/MainPage.xaml", UriKind.Relative));
}
else
{
this.player.Play();
e.Cancel = true;
}
}
However, I encoountered a big problem here I can't really solve. When I move back to MainMEnu, than go again to GenreSelectPage and choose the same genre, everything is ok - the app navigates to GamePage where there is list of 4 answers. However, if I choose another genre, the listBox at GamePage is populated with 12 or 15 items. On the other hand, when I comment navigation to MainPage and normally go back, everything works alright.
Here is my GenrePage Code:
public GenresPage()
{
InitializeComponent();
this.DataContext = App.ViewModel.GenreHelper;
}
private async void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
this.genresListBox.SelectedIndex = -1;
this.progressBar.Visibility = System.Windows.Visibility.Visible;
this.genresListBox.ItemsSource = await App.ViewModel.GenreHelper.GetGenres();
this.progressBar.Visibility = System.Windows.Visibility.Collapsed;
ClearCollections();
}
private static void ClearCollections()
{
if (App.ViewModel.TracksCollection.Count != 0)
{
App.ViewModel.TracksCollection.Clear();
App.ViewModel.TrackCounter = 0;
}
if (App.ViewModel.AnswerCollection.Count > 0)
{
App.ViewModel.AnswerCollection.Clear();
}
}
private async void NavigateToPlay(object sender, RoutedEventArgs e)
{
if (this.genresListBox.SelectedIndex != -1)
{
this.progressBar.Visibility = System.Windows.Visibility.Visible;
await App.ViewModel.GetSongs();
await App.ViewModel.GetAnswers();
this.progressBar.Visibility = System.Windows.Visibility.Collapsed;
NavigationService.Navigate(new Uri(#"/Views/GamePage.xaml", UriKind.Relative));
}
}
UPDATE
On my GamePage I am only assigning DataContext and duration to MediaElement:
public partial class GamePage : PhoneApplicationPage
{
public GamePage()
{
InitializeComponent();
this.DataContext = App.ViewModel;
}
private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
var trackId = App.ViewModel.TracksCollection[App.ViewModel.TrackCounter].Id;
var sampleUri = App.ViewModel.GetSampleUri(trackId);
player.Source = new Uri(sampleUri.AbsoluteUri);
player.Play();
}
private void GetTrackDuration(object sender, RoutedEventArgs e)
{
var player = (MediaElement)sender;
if (player.CurrentState == System.Windows.Media.MediaElementState.Playing)
{
playerSeekBar.Maximum = player.NaturalDuration.TimeSpan.TotalSeconds;
}
}
private void PhoneApplicationPage_BackKeyPress(object sender, CancelEventArgs e)
{
base.OnBackKeyPress(e);
this.player.Pause();
var result = MessageBox.Show(AppResources.GamePageAlert, "Warning", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
//NavigationService.RemoveBackEntry();
//App.RootFrame.Navigate(new Uri(#"/MainPage.xaml", UriKind.Relative));
}
else
{
this.player.Play();
e.Cancel = true;
}
}
}
If anyone can point out what I am doing wrong, I would be really greatful - I am fighting it all day and I have no idea what's causing it.
Thank You very much in advance!!

Deregister the EventHandler in Windows Phone 8 application

I am using this piece of code to register the event and want to de-register Event after completing it's task but don't know how to do problem is that I am using local object for registering event..
code..
public void loadData()
{
//Here client is loacal object..
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);
}
void AccessTokenDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
}
If I understood you correctly, you want to remove your event handler after the download is completed. To remove an event handler, all you need to do is:
client.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);
Note the -= instead of +=.
Place this code where the download completes and you should be fine.
Maybe you can try this:
public void loadData()
{
//Here client is loacal object..
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);
}
void AccessTokenDownloadCompleted(object sender, DownloadStringCompletedEventArgs e)
{
Client client = sender as Client;
if(client != null)
client.DownloadStringCompleted -= new DownloadStringCompletedEventHandler(AccessTokenDownloadCompleted);
}