简体   繁体   English

使用媒体元素在隔离的存储中播放WAV文件

[英]Play wav file in isolated storage with media element

I created a small app to record voice and play it, use this code to make a record button and a play button. 我创建了一个小应用程序来记录和播放声音,使用此代码创建一个录音按钮和一个播放按钮。

I tested the app, recorder worked fine (I used "Windows phone power tools" to get tempAudio.wav from isolated storage of WP emulator and this audio file can played), but the play button didn't play the sound, I can't find anything wrong with the btPlay button :( 我测试了该应用程序,录音机运行正常(我使用“ Windows Phone电动工具”从WP仿真器的隔离存储中获取了tempAudio.wav,并且可以播放此音频文件),但是播放按钮没有播放声音,我可以用btPlay按钮找不到任何问题:(

XAML code (the two button is roundtoggle button and round button from coding4fun toolkit) XAML代码(两个按钮分别是来自coding4fun工具包的roundtoggle按钮和round button)

<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 C#代码

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();
            }
        } 
    }

You are mentioning the Emulator. 您提到的是模拟器。 is this the only place, your audio doesn't play? 这是唯一的地方,您的音频无法播放吗? In that case, the answer is simple: The MediaElement does not support playback within the Emulator. 在这种情况下,答案很简单:MediaElement不支持在模拟器中播放。 See Platform Notes: "Silverlight for Windows Phone" on msdn 请参阅msdn上的平台说明:“ Windows Phone的Silverlight”

You must use " Emulator 8.0 Update 3 WVGA 512MB". 您必须使用“ Emulator 8.0 Update 3 WVGA 512MB”。 It work fine 工作正常

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM