windows phone 7 通过麦克风录音,并且播放

原文:windows phone 7 通过麦克风录音,并且播放

 
//模拟XNA的框架(凡是在wp7中应用xna的都必须先模拟此类)
public class XNAAsyncDispatcher : IApplicationService
        {
            private DispatcherTimer frameworkDispatcherTimer;
            public XNAAsyncDispatcher(TimeSpan dispatchInterval)
            {
                this.frameworkDispatcherTimer = new DispatcherTimer();
                this.frameworkDispatcherTimer.Tick += new EventHandler(frameworkDispatcherTimer_Tick);
                this.frameworkDispatcherTimer.Interval = dispatchInterval;
            }
            void IApplicationService.StartService(ApplicationServiceContext context)
            {
                this.frameworkDispatcherTimer.Start();
            }
            void IApplicationService.StopService()
            {
                this.frameworkDispatcherTimer.Stop();
            }
            void frameworkDispatcherTimer_Tick(object sender, EventArgs e)
            {
                FrameworkDispatcher.Update();
            }
     }
在App 构造函数里写
this.ApplicationLifetimeObjects.Add(new XNAAsyncDispatcher(TimeSpan.FromMilliseconds(50)));

当程序激动时就开始模拟XNA GameTimer

//后代代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO;
using Microsoft.Xna.Framework.Audio;

namespace Wp7_录音
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        public MainPage()
        {
            InitializeComponent();
            mic.BufferReady += Default_BufferReady;
            SoundEffect.MasterVolume = 1.0f;
        }

        MemoryStream ms;
        Microphone mic = Microphone.Default;  

        // Wire up an event handler so we can empty the buffer when full
        // Crank up the volume to max      

        // When the buffer's ready we need to empty it
        // We'll copy to a MemoryStream
        // We could push into IsolatedStorage etc
        void Default_BufferReady(object sender, EventArgs e)
        {
            byte[] buffer = new byte[1024];
            int bytesRead = 0;
            while ((bytesRead = mic.GetData(buffer, 0, buffer.Length)) > 0)
            ms.Write(buffer, 0, bytesRead);
        }
        // The user wants to start recording. If we've already made
        // a recording, close that MemoryStream and create a new one.
        // Start recording on the default device.
        private void start_Click(object sender, RoutedEventArgs e)
        {
            if (ms != null)
                ms.Close();
            ms = new MemoryStream();
            mic.Start();
        }
        // The user wants to stop recording. Checks the microphone
        // is stopped. Reset the MemoryStream position.
        // Play back the recording. Pitch is based on slider value
        private void stop_Click(object sender, RoutedEventArgs e)
        {
            if (mic.State != MicrophoneState.Stopped)
                mic.Stop();
            ms.Position = 0;
            SoundEffect se =  new SoundEffect(ms.ToArray(), mic.SampleRate, AudioChannels.Mono);
            //se.Play(1.0f, (float)slider1.Value, 0.0f);
            se.Play();
        }
    }
}

本实例UI就两个Button  一个start 一个stop, 还有一个滑动条  Silder.

时间: 2024-08-04 01:48:02

windows phone 7 通过麦克风录音,并且播放的相关文章

电脑麦克风录音后重放声音小怎么办

  为什么会出现麦克风录音后重放声音小问题? 1.麦克风被设置为较低的音量. 2.安装了不正确的声卡驱动. 下面为大家详细列出相对应的解决方法. 解决方法: 一.如果是Windows XP系统. 1.依次单击"开始"."程序"."设置"."控制面板",双击打开"声音,语音和音频设备",单击"选项"菜单打开"属性".如图: 2.在打开的属性界面中,单击"录音&

Android简易“吹一吹实现”以及录音和播放示例

最近在做一些跟传感器相关的东西,有注意到以前腾讯微博以前出过一个吹一吹交互,虽然和传感器无关,但是感觉也比较有兴趣,就写了一个拙劣的demo,因为接触媒体文件操作比较少,顺带写了一个录音和播放的例子,总结了一下一些小坑的地方,一并在此分享给大家. 主要思路和坑的地方 主要的思路是通过MediaRecorder提供的getMaxAmplitude()函数,获取一段时间内输入的音频最大幅值来进行检测,所以除了吹的动作,其他声音也会被录进来. "吹"这个动作如果想和其他动作进行区分,其实本质

Android使用MediaRecorder实现录音及播放

现在项目中有使用到音视频相关技术,在参考了网上各种大牛的资料及根据自己项目实际情况(兼容安卓6.0以上版本动态权限管理等),把声音录制及播放相关代码做个记录. public class MediaRecorderActivity extends BaseActivity { private Button start_tv; private ListView listView; //线程操作 private ExecutorService mExecutorService; //录音API pri

麦克风录音声音小怎么办?

  经常会有一些网友向笔者反馈,在使用电脑麦克风录音或者K歌的时候,总感觉麦克风录音声音小,但又不知道如何设置解决.下面小编为大家带来了一篇比较完整的麦克风录音声音小的原因分析与解决办法,希望对遇到类似电脑问题的朋友,有所帮助. 在谈解决办法之前,我们首先了解下电脑麦克风录音声音小的原因,大致有以下几种可能: 1.麦克风音量设置不当,音量设置过低; 2.耳机或者话筒麦不佳; 3.声卡驱动安装不正确或者声卡驱动版本过低. 一.麦克风录音声音小解决办法 首先是检查电脑麦克风设置,主要看音量是否设置的

解决Windows7旗舰版系统下麦克风录音后重放声音小的方法

  麦克风录音后重放声音小的原因分析: 1.麦克风可能被设置为较低的音量 2.声卡驱动安装不正确 具体解决方法如下: 1.首先鼠标单击windows7旗舰版系统任务栏上的小喇叭图标,然后选择"录音设备"项; 2.在弹出来的声音界面中,切换到"录制"选项卡,然后双击默认设备"麦克风"; 3.在弹出来的麦克风属性窗口中,切换到"级别"选项卡,将其中的"麦克风"音量调低到合适,并将"麦克风加强"

windows speech sdk 语音识别,能够识别播放器播放的声音吗,如何实现

问题描述 windows speech sdk 语音识别,能够识别播放器播放的声音吗,如何实现 请问windows speech sdk语音识别,能够识别播放器播放的声音吗,如何实现

c语言-自己用c写的录音代码播放没声音,可以保证播放部分没有问题,等大神

问题描述 自己用c写的录音代码播放没声音,可以保证播放部分没有问题,等大神 #include #include #include #include #include typedef struct WAV { char riff[4]; long len; char type[4]; char fmt[4]; char tmp[4]; short pcm; short channel; long sample; long rate; short framesize; short bit; char

关于微信js接口本地录音和播放服务器录音问题!

问题描述 关于微信js接口本地录音和播放服务器录音问题! 在微信内置浏览器里面调用播放声音的接口,要在微信内置浏览器中播放服务器的语言,获取的时serverId即media_id,播放接口用播放音频的本地ID,先用下载再用部分,再下载接口就一直下载不结束.解决不了一起寻求答案! 解决方案 wx.playVoice({ localId: localId, success: function () { wx.stopVoice({ localId: localId }); }, fail: func

Android编程开发录音和播放录音简单示例_Android

本文实例讲述了Android编程开发录音和播放录音的方法.分享给大家供大家参考,具体如下: /* * The application needs to have the permission to write to external storage * if the output file is written to the external storage, and also the * permission to record audio. These permissions must be