2014-10-23 2 views
0
/// <summary> 
    /// Updates the XNA FrameworkDispatcher and checks to see if a sound is playing. 
    /// If sound has stopped playing, it updates the UI. 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    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; 
      } 
     } 
    } 

    /// <summary> 
    /// The Microphone.BufferReady event handler. 
    /// Gets the audio data from the microphone and stores it in a buffer, 
    /// then writes that buffer to a stream for later playback. 
    /// Any action in this event handler should be quick! 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    void microphone_BufferReady(object sender, EventArgs e) 
    { 
     // Retrieve audio data 
     microphone.GetData(buffer); 

     // Store the audio data in a stream 
     stream.Write(buffer, 0, buffer.Length); 

     var isoStore = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication(); 

     if (isoStore.FileExists("AudioTest.mp3")) 
      isoStore.DeleteFile("AudioTest.mp3"); 
     using (var targetFile = isoStore.CreateFile("AudioTest.mp3")) 
     { 
      // WavHeaderWriter.WriteHeader(targetFile, (int)stream.Length, 1, microphone.SampleRate); 
      var dataBuffer = stream.GetBuffer(); 

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

    /// <summary> 
    /// Handles the Click event for the record button. 
    /// Sets up the microphone and data buffers to collect audio data, 
    /// then starts the microphone. Also, updates the UI. 
    /// </summary> 
    /// <param name="sender"></param> 
    /// <param name="e"></param> 
    private void recordButton_Click(object sender, EventArgs e) 
    { 
     // Get audio data in 1/2 second chunks 
     microphone.BufferDuration = TimeSpan.FromMilliseconds(500); 

     // Allocate memory to hold the audio data 
     buffer = new byte[microphone.GetSampleSizeInBytes(microphone.BufferDuration)]; 

     // Set the stream back to zero in case there is already something in it 
     stream.SetLength(0); 

     // Start recording 
     microphone.Start(); 

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

У меня есть этот код для записи с использованием микрофона и записать его в mp3-файл в локальной папке. При использовании эмулятора я могу получить доступ к файлу, и он воспроизводится с помощью VLC-плеера. Но когда я использую устройство, я не могу открыть файл. Есть ли решениеНевозможно воспроизвести MP3-файл, записанный с использованием микрофона в телефоне Windows 8

+0

Что 'stream'? Я не вижу здесь ничего mp3, кроме имени файла. Как мы хотим помочь? – spender

+0

Просто поместить .mp3 в конец файла не сделать его mp3. MP3 - это особый способ кодирования аудиоданных. Вам нужно выяснить, в какую форму входят данные микрофона, а затем закодировать их в желаемый формат – thorkia

ответ

0

аудиоданные из WP8 микрофона не является MP3, то в PCM формате волны:

Related Question

Я предлагаю вместо того, чтобы смотреть, чтобы кодировать его в качестве MP3, можно использовать WMA, другой формат, напрямую поддерживаемый встроенным API. Этот вопрос имеет образцы вам нужно:

Save Microphone Data to Audio File

Смежные вопросы