Sound Recorder Windows phone 7 - windows

I work in Microsoft Visual Studio 2010, Windows phone.
Can someone help me with audio recorder. When I play it I can heard it twice but not always. After that can someone tell me how to save it to isolated storage and play it from there.
Thanks.
private Microphone microphone = Microphone.Default;
private byte[] buffer;
private MemoryStream stream = new MemoryStream();
private SoundEffect sound;
int broj=0;
void timer_Tick(object sender, EventArgs e)
{
if (microphone.State == MicrophoneState.Started)
{
microphone.Stop();
timer.Stop();
}
}
void microphone_BufferReady(object sender, EventArgs e)
{
microphone.GetData(buffer);
stream.Write(buffer, 0, buffer.Length);
}
System.Windows.Threading.DispatcherTimer timer;
public MainPage()
{
InitializeComponent();
timer = new System.Windows.Threading.DispatcherTimer();
timer.Interval = TimeSpan.FromMilliseconds(2000);
timer.Tick += new EventHandler(timer_Tick);
DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += new EventHandler(dt_Tick);
dt.Start();
microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);
}
void dt_Tick(object sender, EventArgs e)
{
try { FrameworkDispatcher.Update(); }
catch { }
}
private void button1_Click(object sender, RoutedEventArgs e)
{
++broj;
if ((broj + 1) % 2 == 0)
{
//FrameworkDispatcher.Update();
microphone.BufferDuration = TimeSpan.FromMilliseconds(500);
buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];
stream.SetLength(0);
microphone.Start();
timer.Start();
}
else
{
sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
sound.Play();
}
}

Related

Xamarin Stopwatch is very slow if smartphone is in locked mode

So i have a small xamarin project. Just a little stopwatch:
public partial class Stopwatch : ContentPage
{
Stopwatch stopwatch;
public Stopwatch()
{
InitializeComponent();
stopwatch = new Stopwatch();
labeltimer.Text = "00:00:00.00";
}
private void btnTimerReset(object sender, EventArgs e)
{
btnStart.Text = "Start";
stopwatch.Reset();
}
private void btnTimerstop(object sender, EventArgs e)
{
if (labeltimer.Text != "00:00:00.00")
btnStart.Text = "Resume";
stopwatch.Stop();
}
private void btnTimerStart(object sender, EventArgs e)
{
Timerstart();
}
private void Timerstart()
{
stopwatch.Start();
Device.StartTimer(TimeSpan.FromMilliseconds(1), () =>
{
labeltimer.Text = stopwatch.Elapsed.ToString("hh\\:mm\\:ss\\.ff");
return true; // return true to repeat counting, false to stop timer
});
}
}
}
My problem is now, that everything works perfectly, until i start the stopwatch and lock my phone. The stopwatch then works very slow.
Any idea, how to fix that?

photo chooser task windows phone 7.1

i need to take a picture from the directory of Windows phone in my application so i add a child Windows when i click on the text in this child Windows i add a button and i called
public partial class AnnotationControl : ChildWindow
{
public ObservableCollection<string> cercle { get; set; }
public AnnotationControl()
{
InitializeComponent();
}
private void ChildWindow_Closing_1(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void btnsave_Click_1(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void btnCancel_Click_1(object sender, RoutedEventArgs e)
{
this.DialogResult = true;
}
private void browse_Click(object sender, RoutedEventArgs e)
{
PhotoChooserTask objPhotoChooser = new PhotoChooserTask();
objPhotoChooser.Completed += new EventHandler<PhotoResult>(PhotoChooseCall);
objPhotoChooser.Show();
}
but when i clicked on the button to choose a picture the application crashed
"Application_UnhandledException"
some one have any idea please
Use this may be helpful
private void changeImage(object sender, RoutedEventArgs e)
{
PhotoChooserTask photoChooserTask = new PhotoChooserTask();
photoChooserTask.Completed += new EventHandler<PhotoResult>(photoChooserTask_Completed);
photoChooserTask.Show();
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
System.Windows.Media.Imaging.BitmapImage bmp = new System.Windows.Media.Imaging.BitmapImage();
bmp.SetSource(e.ChosenPhoto);
imageTapped.Source = bmp;
}
}

Need slideshow of images when my application is launched in windows phone 7

How to get the slideshow of images with time interval of 2 seconds. I had referred the below code from stackoverflow but while running iam nt getting any images displayed.. Please tel where iam went wrong...
Xaml:
<image Name=myImg Source={Binding bi}/>
Code:
private DispatcherTimer tmr = new DispatcherTimer();
private List<string> images = new List<string>();
private int imageIndex = 0;
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
}
void MainPage_Loaded(object sender, RoutedEventArgs e)
{
tmr.Interval = TimeSpan.FromSeconds(5);
tmr.Tick += new EventHandler(tmr_Tick);
LoadImages();
ShowNextImage();
}
private void LoadImages()
{
// list the files (includede in the XAP file) here
images.Add("/images/filename1.jpg");
images.Add("/images/filename2.jpg");
images.Add("/images/filename3.jpg");
images.Add("/images/filename4.jpg");
}
private void ShowNextImage()
{
Imagesource bi = new BitmapImage(new Uri(images[imageIndex], UriKind.Relative));
myImg.Source = bi;
imageIndex = (imageIndex + 1) % images.Count;
}
void tmr_Tick(object sender, EventArgs e)
{
ShowNextImage();
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
if (!tmr.IsEnabled)
{
tmr.Start();
}
base.OnNavigatedTo(e);
}
protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
tmr.Stop();
base.OnNavigatedFrom(e);
}
Thanks in Advance
Change Name=myImg to x:Name="myImg" and remove the Source attribute entirely. Otherwise it looks like it should work.

MediaElement repeatedly play Exception

I'm new in WP7, and I am trying to play sounds with a MediaElement when I press a button.
Its works, but unfortunately I get "Operation not permitted on IsolatedStorageFileStream" Exception when I press the button repeatedly, before the sound begins. How can I avoid that?
The Play method:
public void Play(string filename)
{
try
{
mediaElement.Stop();
mediaElement.ClearValue(MediaElement.SourceProperty);
using (var isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fileStream = isf.OpenFile(#"shared\transfers\" + filename, FileMode.Open))
{
mediaElement.SetSource(fileStream);
mediaElement.IsMuted = false;
mediaElement.Volume = 1.0;
}
}
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
Event handlers:
void me_MediaOpened(object sender, RoutedEventArgs e)
{
mediaElement.Play();
}
void me_MediaEnded(object sender, RoutedEventArgs e)
{
mediaElement.ClearValue(MediaElement.SourceProperty);
}
If you'd like for the user to be allowed to click the play button only once. One way would be.
public bool IsPlaying = false;
void me_MediaOpened(object sender, RoutedEventArgs e)
{
if(!IsPlaying){
mediaElement.Play();
IsPlaying = true;
}
}
void me_MediaEnded(object sender, RoutedEventArgs e)
{
if(IsPlaying){
mediaElement.ClearValue(MediaElement.SourceProperty);
IsPlaying = false;
}
}
Loading the filestream to a memorystream, before playing should make it more flexible, if you're dealing with low size streams.

Toolbox items grayed out in VS 2010

I have tried numerous attempts to fix this problem or bug, firstly by deleting the .tbd files from C:\Users\\AppData\Local\Microsoft\VisualStudio\x.0
I have also tried this:
Visual Studio "Tools" menu
"Options" submenu
"Windows Form Designer" tab
"General" tab
Set "AutoToolboxPopulate" to "True"
The ToolBox list is still not populating correctly and the "BackgroundWorker" component I need is grayed out. Any ideas?
At least a workaround: declare the BackgroundWorker in code, but don't forget to dispose it properly:
public class MyForm : Form
{
private BackgroundWorker bgWorker = null;
public MyForm()
{
InitializeComponent();
this.bgWorker = new BackgroundWorker; //TODO: set properties and event handlers
}
public override void Dispose(bool disposing)
{
//TODO: copy from MyForm.Designer.cs and add:
Backgroundworker bgw = this.bgWorker;
this.bgWorker = null;
if (disposing && bgw != null)
{
try
{
//TODO: release event handlers
bgw.Dispose();
}
catch(Exception)
{
/* consumed disposal error */
}
}
}
}
I have found a solution to my problem, using the BackgroundWorker class in C# without using the component from the toolbox. In this case, I needed two seperate backgroundWorkers:
using System.Threading;
public partial class MainWindow : Window
{
private BackgroundWorker bw1 = new BackgroundWorker();
private BackgroundWorker bw2 = new BackgroundWorker();
public MainWindow()
{
InitializeComponent();
bw1.WorkerReportsProgress = true;
bw1.DoWork += new DoWorkEventHandler(bw1_DoWork);
bw1.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw1_RunWorkerCompleted);
bw1.ProgressChanged += new ProgressChangedEventHandler(bw1_ProgressChanged);
bw2.WorkerReportsProgress = true;
bw2.DoWork += new DoWorkEventHandler(bw2_DoWork2);
bw2.RunWorkerCompleted += new RunWorkerCompletedEventHandler(bw2_RunWorkerCompleted);
bw2.ProgressChanged += new ProgressChangedEventHandler(bw1_ProgressChanged);
}
private void bw1_DoWork(object sender, DoWorkEventArgs e)
{
StatsProcessor proc = new StatsProcessor();
proc.CompareStats(listText1, listText2);
}
private void bw2_DoWork2(object sender, DoWorkEventArgs e)
{
StatsParser parser = new StatsParser();
}
private void bw1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar2.IsIndeterminate = false;
progressBar2.Value = 100;
btnCompareStats.IsEnabled = true;
}
private void bw2_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
progressBar1.IsIndeterminate = false;
progressBar1.Value = 100;
btnFetchStats.IsEnabled = true;
}
private void bw1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar2.Value = e.ProgressPercentage;
}
private void bw2_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
this.progressBar1.Value = e.ProgressPercentage;
}
private void btnCompare_Click(object sender, EventArgs e)
{
btnCompareStats.IsEnabled = false;
StatsProcessor proc = new StatsProcessor();
if (bw1.IsBusy != true)
{
progressBar2.IsIndeterminate = true;
// Start the asynchronous operation.
bw1.RunWorkerAsync();
}
}
private void btnFetchStats_Click(object sender, RoutedEventArgs e)
{
btnFetchStats.IsEnabled = false;
if (bw2.IsBusy != true)
{
progressBar1.IsIndeterminate = true;
// Start the asynchronous operation.
bw2.RunWorkerAsync();
}
}
}
I would try resetting the toolbox items. Then use the Add Item dialog to put back something you need.

Resources