从silverlight wp7中的扬声器录制声音

时间:2011-09-19 06:49:19

标签: windows-phone-7

我需要在文件中录制不同的声音。文件可能是.mp3,.wav等。在Windows Phone 7中怎么可能?

1 个答案:

答案 0 :(得分:0)

在Windows Phone中有一种简单的方法可以执行此操作。您基本上使用框架提供的Microphone类。有关该主题的精彩文章,请转至http://msdn.microsoft.com/en-us/magazine/gg598930.aspx

void OnRecordButtonClick(对象发送者,RoutedEventArgs args)         {             if(microphone.State == MicrophoneState.Stopped)             {                 //清除用于存储缓冲区的集合                 memoBufferCollection.Clear();

            // Stop any playback in progress (not really necessary, but polite I guess)
            playback.Stop();

            // Start recording
            microphone.Start();
        }
        else
        {
            StopRecording();
        }

        // Update the record button
        bool isRecording = microphone.State == MicrophoneState.Started;
        UpdateRecordButton(isRecording);
    }

    void StopRecording()
    {
        // Get the last partial buffer
        int sampleSize = microphone.GetSampleSizeInBytes(microphone.BufferDuration);
        byte[] extraBuffer = new byte[sampleSize];
        int extraBytes = microphone.GetData(extraBuffer);

        // Stop recording
        microphone.Stop();

        // Create MemoInfo object and add at top of collection
        int totalSize = memoBufferCollection.Count * sampleSize + extraBytes;
        TimeSpan duration = microphone.GetSampleDuration(totalSize);
        MemoInfo memoInfo = new MemoInfo(DateTime.UtcNow, totalSize, duration);
        memoFiles.Insert(0, memoInfo);

        // Save data in isolated storage
        using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
        {
            using (IsolatedStorageFileStream stream = storage.CreateFile(memoInfo.FileName))
            {
                // Write buffers from collection
                foreach (byte[] buffer in memoBufferCollection)
                    stream.Write(buffer, 0, buffer.Length);

                // Write partial buffer
                stream.Write(extraBuffer, 0, extraBytes);
            }
        }

        // Scroll to show new MemoInfo item
        memosListBox.UpdateLayout();
        memosListBox.ScrollIntoView(memoInfo);
    }
相关问题