Windows Phone 8中的录音问题

时间:2014-02-24 11:02:09

标签: windows-phone-8 navigation recording

我的代码正常工作直到页面处于活动状态。但是当我再次浏览录制页面后再返回另一页时,它无效。

这是添加了页面的示例代码。

 public partial class MainPage : PhoneApplicationPage
{
    private Microphone microphone = Microphone.Default;
    private byte[] buffer;                             
    private MemoryStream stream = new MemoryStream();
    private SoundEffectInstance soundInstance;
    private bool soundIsPlaying = false;      

    // Status images
    private BitmapImage blankImage;
    private BitmapImage microphoneImage;
    private BitmapImage speakerImage;


    public MainPage()
    {
        InitializeComponent();

        // Timer to simulate the XNA Framework game loop (Microphone is 
        // from the XNA Framework). We also use this timer to monitor the 
        // state of audio playback so we can update the UI appropriately.
        DispatcherTimer dt = new DispatcherTimer();
        dt.Interval = TimeSpan.FromMilliseconds(33);
        dt.Tick += new EventHandler(dt_Tick);
        dt.Start();


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

        blankImage = new BitmapImage(new Uri("Images/blank.png", UriKind.RelativeOrAbsolute));
        microphoneImage = new BitmapImage(new Uri("Images/microphone.png", UriKind.RelativeOrAbsolute));
        speakerImage = new BitmapImage(new Uri("Images/speaker.png", UriKind.RelativeOrAbsolute));
    }


    void dt_Tick(object sender, EventArgs e)
    {
        try { FrameworkDispatcher.Update(); }
        catch { }

        if (true == soundIsPlaying)
        {
            if (soundInstance.State != SoundState.Playing)
            {
                // Audio has finished playing
                soundIsPlaying = false;

                // Update the UI to reflect that the 
                // sound has stopped playing
                SetButtonStates(true, true, false);
                UserHelp.Text = "press play\nor record";
                StatusImage.Source = blankImage;
            }
        }
    }


    private void recordButton_Click(object sender, EventArgs e)
    {
        microphone.BufferDuration = TimeSpan.FromMilliseconds(500);

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

        stream.SetLength(0);

        microphone.Start();

        SetButtonStates(false, false, true);
        UserHelp.Text = "record";
        StatusImage.Source = microphoneImage;
    }


    private void stopButton_Click(object sender, EventArgs e)
    {
        if (microphone.State == MicrophoneState.Started)
        {
            // In RECORD mode, user clicked the 
            // stop button to end recording
            microphone.Stop();
        }
        else if (soundInstance.State == SoundState.Playing)
        {
            // In PLAY mode, user clicked the 
            // stop button to end playing back
            soundInstance.Stop();
        }

        SetButtonStates(true, true, false);
        UserHelp.Text = "ready";
        StatusImage.Source = blankImage;
    }


    private void playButton_Click(object sender, EventArgs e)
    {
        if (stream.Length > 0)
        {
            // Update the UI to reflect that
            // sound is playing
            SetButtonStates(false, false, true);
            UserHelp.Text = "play";
            StatusImage.Source = speakerImage;

            // Play the audio in a new thread so the UI can update.
            Thread soundThread = new Thread(new ThreadStart(playSound));
            soundThread.Start();
        }
    }


    private void playSound()
    {
        // Play audio using SoundEffectInstance so we can monitor it's State 
        // and update the UI in the dt_Tick handler when it is done playing.
        SoundEffect sound = new SoundEffect(stream.ToArray(), microphone.SampleRate, AudioChannels.Mono);
        soundInstance = sound.CreateInstance();
        soundIsPlaying = true;
        soundInstance.Play();
    }


    private void SetButtonStates(bool recordEnabled, bool playEnabled, bool stopEnabled)
    {
        (ApplicationBar.Buttons[0] as ApplicationBarIconButton).IsEnabled = recordEnabled;
        (ApplicationBar.Buttons[1] as ApplicationBarIconButton).IsEnabled = playEnabled;
        (ApplicationBar.Buttons[2] as ApplicationBarIconButton).IsEnabled = stopEnabled;
    }
}

另一页

 public partial class Page1 : PhoneApplicationPage
{
    public Page1()
    {
        InitializeComponent();
    }

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));

    }
}

请尽可能提供解决方案,因为我的应用程序依赖于音频  记录..

非常感谢....

1 个答案:

答案 0 :(得分:0)

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

将此代码写入OnNavigateFrom甚至。