gpt4 book ai didi

c# - Windows Phone 8 中的录音机问题

转载 作者:行者123 更新时间:2023-11-30 22:05:49 25 4
gpt4 key购买 nike

首先,如果做错了什么请原谅我,我是这个平台的新手。对于那些想要的人来说,这是一个很好的录音机示例。我在 Windows Phone 8 中创建了录音机功能,一切正常,但我如何获得时间意义上的音频扩展、音频名称和音频长度?有关详细信息,请参阅我的代码:

private Microphone microphone = Microphone.Default;
private byte[] buffer;
private MemoryStream stream = new MemoryStream();
private SoundEffectInstance soundInstance;
private bool soundIsPlaying = false;

private BitmapImage blankImage;
private BitmapImage microphoneImage;
private BitmapImage speakerImage;

public App()
{
InitializeComponent();

DispatcherTimer dt = new DispatcherTimer();
dt.Interval = TimeSpan.FromMilliseconds(33);
dt.Tick += dt_Tick;
dt.Start();

microphone.BufferReady += microphone_BufferReady;

blankImage = new BitmapImage(new Uri("Img/blank.png", UriKind.RelativeOrAbsolute));
microphoneImage = new BitmapImage(new Uri("Img/microphone.png", UriKind.RelativeOrAbsolute));
speakerImage = new BitmapImage(new Uri("Img/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;
}
}
}

void microphone_BufferReady(object sender, EventArgs e)
{
microphone.GetData(buffer);

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

}

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


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

string audiofile = Convert.ToBase64String(buffer);
string audiosize = stream.Length.ToString();

stream.SetLength(0);

microphone.Start();

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

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();
btnAdd.Visibility = Visibility.Visible;
}

}

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;

btnAdd.Visibility = Visibility.Visible;
}

private void playSound()
{

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 void WriteWavHeader(int sampleRate)
{
const int bitsPerSample = 16;
const int bytesPerSample = bitsPerSample / 8;
var encoding = System.Text.Encoding.UTF8;

// ChunkID Contains the letters "RIFF" in ASCII form (0x52494646 big-endian form).
stream.Write(encoding.GetBytes("RIFF"), 0, 4);

// NOTE this will be filled in later
stream.Write(BitConverter.GetBytes(0), 0, 4);

// Format Contains the letters "WAVE"(0x57415645 big-endian form).
stream.Write(encoding.GetBytes("WAVE"), 0, 4);

// Subchunk1ID Contains the letters "fmt " (0x666d7420 big-endian form).
stream.Write(encoding.GetBytes("fmt "), 0, 4);

// Subchunk1Size 16 for PCM. This is the size of therest of the Subchunk which follows this number.
stream.Write(BitConverter.GetBytes(16), 0, 4);

// AudioFormat PCM = 1 (i.e. Linear quantization) Values other than 1 indicate some form of compression.
stream.Write(BitConverter.GetBytes((short)1), 0, 2);

// NumChannels Mono = 1, Stereo = 2, etc.
stream.Write(BitConverter.GetBytes((short)1), 0, 2);

// SampleRate 8000, 44100, etc.
stream.Write(BitConverter.GetBytes(sampleRate), 0, 4);

// ByteRate = SampleRate * NumChannels * BitsPerSample/8
stream.Write(BitConverter.GetBytes(sampleRate * bytesPerSample), 0, 4);

// BlockAlign NumChannels * BitsPerSample/8 The number of bytes for one sample including all channels.
stream.Write(BitConverter.GetBytes((short)(bytesPerSample)), 0, 2);

// BitsPerSample 8 bits = 8, 16 bits = 16, etc.
stream.Write(BitConverter.GetBytes((short)(bitsPerSample)), 0, 2);

// Subchunk2ID Contains the letters "data" (0x64617461 big-endian form).
stream.Write(encoding.GetBytes("data"), 0, 4);

// NOTE to be filled in later
stream.Write(BitConverter.GetBytes(0), 0, 4);
}


public void UpdateWavHeader()
{
if (!stream.CanSeek) throw new Exception("Can't seek stream to update wav header");

var oldPos = stream.Position;

// ChunkSize 36 + SubChunk2Size
stream.Seek(4, SeekOrigin.Begin);
stream.Write(BitConverter.GetBytes((int)stream.Length - 8), 0, 4);

// Subchunk2Size == NumSamples * NumChannels * BitsPerSample/8 This is the number of bytes in the data.
stream.Seek(40, SeekOrigin.Begin);
stream.Write(BitConverter.GetBytes((int)stream.Length - 44), 0, 4);

stream.Seek(oldPos, SeekOrigin.Begin);
}

然后你应该在 recordButton_Click 方法上调用 WriteWavHeader,在 之后的 stopButton_Click 上调用 UpdateWavHeader >microphone.Stop() 通话。

因为麦克风返回 PCM 数据,这些 header 的作用是将流转换为 wav 文件,例如,您稍后可以将其保存在独立存储中。

下面是将文件保存到 IsolatedStorage 的代码:

    private void SaveToIsolatedStorage(string filename)
{
// first, we grab the current apps isolated storage handle
IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();

// we give our file a filename
string strSaveName = filename;

// if that file exists...
if (isf.FileExists(strSaveName))
{
// then delete it
isf.DeleteFile(strSaveName);
}

// now we set up an isolated storage stream to point to store our data
var isfStream = new IsolatedStorageFileStream(strSaveName, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());

isfStream.Write(stream.ToArray(), 0, stream.ToArray().Length);

// ok, done with isolated storage... so close it
isfStream.Close();
}

关于c# - Windows Phone 8 中的录音机问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24116611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com