开发者

Windows Phone 7 Audio Recording Problem

开发者 https://www.devze.com 2023-01-28 23:14 出处:网络
I\'m hoping someone can help开发者_开发技巧 me with this. I have found the examples for recording audio using XNA in a Silverlight application. And it works, however, only the first time in. I have al

I'm hoping someone can help开发者_开发技巧 me with this. I have found the examples for recording audio using XNA in a Silverlight application. And it works, however, only the first time in. I have all the recording functionality on a seperate WP7 Page and with successive visits to the page it doesn't work. The best I can tell is the microphone.start is getting called but the micophone.status remains stopped. What is weird is the BufferReady keeps getting called and the code within that function is all running but without the microphone really starting nothing is really happening. When you exit the app and come back in again the first time visit to the page and everything works fine, but a revisit to the page and it doesn't.

    void microphone_BufferReady(object sender, EventArgs e)
            {
                this.Dispatcher.BeginInvoke(() =>
                {
                    microphone.GetData(buffer);

                    stream.Write(buffer, 0, buffer.Length);

                    TimeSpan tsTemp = timer.Elapsed;
                    TextBlockSeconds.Text = tsTemp.Hours.ToString().PadLeft(2, '0') + ":" + tsTemp.Minutes.ToString().PadLeft(2, '0') + ":" + tsTemp.Seconds.ToString().PadLeft(2, '0');

    if(timer.Elapsed.Seconds >5)
    DoStop();

                });
            }

            private void ButtonRecord_Click(object sender, RoutedEventArgs e)
            {
                DisableRecordButton();

                timer = new Stopwatch();
                timer.Start();

                stream = new MemoryStream();

                TextBlockSeconds.Text = "00:00:00";
                TextBlockStatus.Text = "Recording: "; 

                microphone.BufferDuration = TimeSpan.FromMilliseconds(500);
                buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)];

                microphone.BufferReady += new EventHandler<EventArgs>(microphone_BufferReady);

                microphone.Start();
            }

    private void DoStop()
            {

if (timer.IsRunning)
                timer.Stop();

            if (microphone.State == MicrophoneState.Started)
            {
                microphone.Stop();
                TextBlockStatus.Text = "Stopped: Ready to save";
            }
            else
            {
                TextBlockStatus.Text = "Ready: ";
            }

            TextBlockSeconds.Text = string.Empty;

            EnableRecordButton();
            }

Update... I found the problem but no solution. I was calling the microphone.stop via code on a timer (so I could limit the recorded audio to 5 seconds). Exact same code to execute when a manual stop button would be clicked. When clicking the manual stop button everything worked fine, could re-visit the page and all would be fine. When the stop was called in code from the timer, next visit to the page would not work. So I implemented it with only a manual stop button but really would have been nice to do it automatically (and to know what the real issue was).


actually when you are navigating away from the page you can add

 protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);
        this.MicroPhone.BufferReady -= this.Microphone_BufferReady;

    }

and when you are returning to page add

this.MicroPhone.BufferReady += this.Microphone_BufferReady;

You can add this statement either in a page loaded event or an OnNavigatedTo event


Added string name = System.Threading.Thread.CurrentThread.ManagedThreadId.ToString() to make sure that it was on the same thread (and it was).

But finally worked this out, the problem is the microphone.stop doesn't stop the microphone from continuing to fire the buffer ready event (like I was expecting). And it would seem the way the page is cached this causes some weird problems with that event still firing. So I added the code

microphone.BufferReady -= new EventHandler<EventArgs>(microphone_BufferReady);

to my code for stopping, and it all works now.


I can't see from your code how you're stopping the timer/microphone if you navigate away from the page and don't manually stop it.

If that's not it, are you ensuring that all your microphone operations are being executed on the same thread? (Just a thought.)

0

精彩评论

暂无评论...
验证码 换一张
取 消