【问题标题】:Issue while Playing, the recorded audio in WP7播放时出现问题,在 WP7 中录制的音频
【发布时间】:2014-06-01 12:47:47
【问题描述】:

各位开发者,

如果我们将录制的声音保存为 mp3 文件。我们在打开文件时收到错误,因为 Windows Media Player 无法播放文件。播放器可能不支持文件类型或可能不支持用于压缩文件的编解码器。

请给出解决此问题的解决方案....

这是我的源代码

namespace Windows_Phone_Audio_Recorder
{
    public partial class MainPage : PhoneApplicationPage
    {
        MemoryStream m_msAudio = new MemoryStream();
        Microphone m_micDevice = Microphone.Default;
        byte[] m_baBuffer;
        SoundEffect m_sePlayBack;
        ViewModel vm = new ViewModel();
        long m_lDuration = 0;
        bool m_bStart = false;
        bool m_bPlay = false;
        private DispatcherTimer m_dispatcherTimer;

        public MainPage()
        {
            InitializeComponent();

            SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;            
            DataContext = vm;
            m_dispatcherTimer = new DispatcherTimer();

            m_dispatcherTimer.Interval = TimeSpan.FromTicks(10000);

            m_dispatcherTimer.Tick += frameworkDispatcherTimer_Tick;
            m_dispatcherTimer.Start();
            FrameworkDispatcher.Update();        
            //icBar.ItemsSource = vm.AudioData;
        }

        void frameworkDispatcherTimer_Tick(object sender, EventArgs e)
        {
            FrameworkDispatcher.Update();
        }        

        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            m_bStart = true;
            tbData.Text = "00:00:00";
            m_lDuration = 0;
            m_micDevice.BufferDuration = TimeSpan.FromMilliseconds(1000);
            m_baBuffer = new byte[m_micDevice.GetSampleSizeInBytes(m_micDevice.BufferDuration)];
            //m_micDevice.BufferReady += new EventHandler(m_Microphone_BufferReady);
            m_micDevice.BufferReady += new EventHandler<EventArgs>(m_Microphone_BufferReady);
            m_micDevice.Start();
        }

        void m_Microphone_BufferReady(object sender, EventArgs e)
        {
            m_micDevice.GetData(m_baBuffer);
              Dispatcher.BeginInvoke(()=>
              {
                    vm.LoadAudioData(m_baBuffer);
                    m_lDuration++;
                    TimeSpan tsTemp = new TimeSpan(m_lDuration * 10000000); 
                   tbData.Text = tsTemp.Hours.ToString().PadLeft(2, '0') + ":" + tsTemp.Minutes.ToString().PadLeft(2, '0') + ":" + tsTemp.Seconds.ToString().PadLeft(2, '0');
               }
                );
            //this.Dispatcher.BeginInvoke(new Action(() => vm.LoadAudioData(m_baBuffer))); 
            //this.Dispatcher.BeginInvoke(new Action(() => tbData.Text = m_baBuffer[0].ToString() + m_baBuffer[1].ToString() + m_baBuffer[2].ToString() + m_baBuffer[3].ToString()));
            m_msAudio.Write(m_baBuffer,0, m_baBuffer.Length);
        }

        private void btnStop_Click(object sender, RoutedEventArgs e)
        {
            if (m_bStart)
            {
                m_bStart = false;
                m_micDevice.Stop();
                ProgressPopup.IsOpen = true;
            }

            if (m_bPlay)
            {
                m_bPlay = false;
                m_sePlayBack.Dispose();
            }
        }

        private void btnPlay_Click(object sender, RoutedEventArgs e)
        {
            m_bPlay = true;
            m_sePlayBack = new SoundEffect(m_msAudio.ToArray(), m_micDevice.SampleRate, AudioChannels.Mono);
            m_sePlayBack.Play();
        }

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            if (txtAudio.Text != "")
            {
                IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
                string strSource = txtAudio.Text + ".wav";
                int nIndex = 0;
                while (isfData.FileExists(txtAudio.Text))
                {
                    strSource = txtAudio.Text + nIndex.ToString().PadLeft(2, '0') + ".wav";
                }

                IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(strSource, FileMode.Create, IsolatedStorageFile.GetUserStoreForApplication());
                isfStream.Write(m_msAudio.ToArray(), 0, m_msAudio.ToArray().Length);
                isfStream.Close();
            }
            this.Dispatcher.BeginInvoke(new Action(() => ProgressPopup.IsOpen = false));           
        }
    }
}

DotNet Weblineindia

我的将音频上传到 PHP 服务器的源代码:

public void UploadAudio()
    {
        try
        {
            if (m_msAudio != null)
            {
                var fileUploadUrl = "uploadurl";

                var client = new HttpClient();
                m_msAudio.Position = 0;
                MultipartFormDataContent content = new MultipartFormDataContent();

                content.Add(new StreamContent(m_msAudio), "uploaded_file", strFileName);

                // upload the file sending the form info and ensure a result.it will throw an exception if the service doesn't return a valid successful status code

                client.PostAsync(fileUploadUrl, content)

               .ContinueWith((postTask) =>
               {
                   try
                   {
                       postTask.Result.EnsureSuccessStatusCode();
                       var respo = postTask.Result.Content.ReadAsStringAsync();
                       string[] splitChar = respo.Result.Split('"');
                       FilePath = splitChar[3].ToString();
                       Dispatcher.BeginInvoke(() => NavigationService.Navigate(new Uri("/VoiceSlider.xaml?FName=" + strFileName, UriKind.Relative)));                          
                   }
                   catch (Exception ex)
                   {
                      // Logger.Log.WriteToLogger(ex.Message);
                       MessageBox.Show("voice not uploaded" + ex.Message);
                   }

               });
            }
        }
        catch (Exception ex)
        {
            //Logger.Log.WriteToLogger(ex.Message + "Error occured while uploading image to server");
        }
    }

【问题讨论】:

    标签: windows-phone-7 windows-phone-8 windows-phone windows-phone-7.1 windows-phone-7.8


    【解决方案1】:

    首先,Windows Phone 不允许录制 .mp3 文件。因此,您保存的 .mp3 文件将不会被 Windows Phone 播放器播放。

    看看this. 这个演示可以很好地录制 .wav 文件。

    如果您希望保存其他格式,例如 .aac、.amr,请使用 AudioVideoCaptureDevice 类。

    【讨论】:

    • 嗨,DotNet Weblineindia 是的,我接受你的回答如何将录制的 wav 文件上传到 Php 服务器?
    • 您好,您需要在服务器上创建一个多部分网络服务,然后从您的设备上传您的声音的字节数据。在服务器端接收该数据,然后将其保存为服务器上的 .wav 文件。
    • DotNet Weblineindia ,好的,如果我保存在隔离存储中 mp3 如果我上传到服务器,则保存它不会播放。我在这里尝试过,对不起,我无法复制代码并显示在评论页面中,所以我发布了我的源代码,用于将音频上传到 PHP 服务器作为答案,请检查并回复....
    猜你喜欢
    • 1970-01-01
    • 2015-10-30
    • 2011-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多