2014-02-14 56 views
0

在我的應用程序的一個頁面中,我通過麥克風錄製。 只要我留在那個頁面上,我就可以停下來並重新錄製。第2次使用頁面時不能使用麥克風

但是,當我導航出頁面並返回並按錄製時,麥克風不會捕捉任何東西。結果是一個靜音。

這裏是構造

private byte[] _buffer; 
private int _passedSecs = 0; 

private const string RECORDFILE = "newrecording.wav"; 

private Microphone _mic = Microphone.Default; 
private MemoryStream _stream = new MemoryStream(); 
private DispatcherTimer _framework; 

public MyPage() 
{ 
    InitializeComponent(); 

    _framework = new DispatcherTimer(); 
    _framework.Interval = TimeSpan.FromMilliseconds(33); 
    _framework.Tick += _framework_Tick; 

    _mic.BufferReady += _mic_BufferReady; 
} 

private void _mic_BufferReady(object sender, EventArgs e) 
{ 
    _mic.GetData(_buffer); 
    _stream.Write(_buffer, 0, _buffer.Length); 

    var isoStore = IsolatedStorageFile.GetUserStoreForApplication(); 
    var targetFile = isoStore.CreateFile(RECORDFILE); 

    WavHeaderWriter.WriteHeader(targetFile, (int)_stream.Length, 1, _mic.SampleRate); 
    var dataBuffer = _stream.GetBuffer(); 

    targetFile.Write(dataBuffer, 0, (int)_stream.Length); 
    targetFile.Flush(); 
    targetFile.Close(); 
} 

在這裏,我記錄

private async void startRecording() 
{ 
    _framework.Start(); 

    _mic.BufferDuration = TimeSpan.FromMilliseconds(500); 
    _buffer = new byte[_mic.GetSampleSizeInBytes(_mic.BufferDuration)]; 

    _stream.SetLength(0); 
    _mic.Start(); 
} 

這裏我停止

private void stopRecording() 
{ 
    var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 
    IsolatedStorageFileStream _data = isoStore.OpenFile(RECORDFILE, FileMode.Open); 

    SoundEffect sound = SoundEffect.FromStream(_data); 
    _data.Close(); 
    sound.Play(); 

    _mic.Stop(); 
    _framework.Stop(); 
    isoStore.Dispose(); 
} 

回答

1

你應該退訂Microphone eventPageNavigatedFrom事件(事件射擊,當你'從頁面導航)

相關問題