我创建了一个小应用程序来录制语音并播放它,使用此代码制作录制按钮和播放按钮。
我测试了应用程序,录音机工作正常(我使用“Windows手机电源工具”从WP模拟器的隔离存储中获取tempAudio.wav并且可以播放此音频文件),但是播放按钮没有播放声音,我找不到btPlay按钮有什么问题:(
XAML代码(两个按钮是roundtoggle按钮和来自coding4fun工具包的圆形按钮)
<StackPanel Orientation="Horizontal">
<toolkit1:RoundToggleButton x:Name="btRecorder" IconUri="..." Checked="btRecorder_Checked" checked="btRecorder_Unchecked"/>
<MediaElement x:Name="meVoicePlayer" AutoPlay="False"/>
<toolkit1:RoundButton x:Name="btPlay" Click="PlayAudio_Click" IconUri="..."/>
</StackPanel>
c#code
MicrophoneRecorder recorder = new MicrophoneRecorder();
private void btRecorder_Checked(object sender, RoutedEventArgs e)
{
recorder.Start();
}
private void btRecorder_Unchecked(object sender, RoutedEventArgs e)
{
recorder.Stop();
SaveTempAudio(recorder.Buffer);
}
public void SaveTempAudio(MemoryStream buffer)
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
var bytes = buffer.GetWavAsByteArray(recorder.SampleRate);
using (IsolatedStorageFileStream audio = new IsolatedStorageFileStream("TempAudio.wav",FileMode.Create,isf))
{
audio.Write(bytes, 0, bytes.Length);
}
}
}
private void PlayAudio_Click(object sender, RoutedEventArgs e)
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream audio = new IsolatedStorageFileStream("TempAudio.wav",FileMode.Open,isf))
{
meVoicePlayer.Stop();
meVoicePlayer.SetSource(audio);
meVoicePlayer.Position = new TimeSpan(0, 0, 0, 0);
meVoicePlayer.Play();
}
}
}
答案 0 :(得分:0)
你提到的是模拟器。这是唯一的地方,你的音频没有播放?在这种情况下,答案很简单:MediaElement不支持在模拟器中播放。请参阅平台说明:&#34; Silverlight for Windows Phone&#34;在msdn
答案 1 :(得分:0)
你必须使用&#34;仿真器8.0更新3 WVGA 512MB&#34;。它工作正常