单声道为Android - 语音录制 [英] Mono for Android - Voice Recording

查看:231
本文介绍了单声道为Android - 语音录制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用单声道为Android录制的声音。我已经看到了Android的下录制语音各种材料,但他们都不似乎包括单声道版本的主题。

I'm wondering how to record voice using Mono for Android. I've seen various materials for recording voice under Android, but none of them seems to cover the Mono version topic.

感谢。

推荐答案

下面是使用默认audiorecorder和记录音频.3GP格式的一个基本的例子。 它有一对夫妇的按钮和一个TextView,当你录制的音频显示计时器的活动。

Here is a basic example that uses the default audiorecorder and records audio to .3gp format. It has an activity with a couple of buttons and a textview that displays a timer as you record your audio.

活动(AudoRecorderActivity.cs):

Activity (AudoRecorderActivity.cs):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Timers;
using System.Threading;

namespace App.MonoDroid {
    [Activity (Label = "Record Audio")]
    public class AudioRecorderActivity : Activity {
        Button btnStart;
        Button btnStop;
        public TextView tvTime;
        private System.Timers.Timer tm;
        private TimeSpan m_offset;
        private DateTime m_startTime;
        private const string STOPWATCH_ZERO = "00:00:00";
        AudioRecorder rec;
        private string FileName;
        bool isStarted = false;

        protected override void OnCreate (Bundle bundle)
        {
            base.OnCreate (bundle);
            SetContentView (Resource.Layout.audiorecorder);
            btnStart = FindViewById<Button> (Resource.Id.btnStart);
            btnStart.Click += new EventHandler (btnStart_Click); 
            tvTime = FindViewById<TextView> (Resource.Id.tvTimer);
            btnStop = FindViewById<Button> (Resource.Id.btnStop);
            btnStop.Click += new EventHandler (btnStop_Click);
            m_offset = new TimeSpan (0);
            m_startTime = DateTime.Now;
            tvTime.SetText (STOPWATCH_ZERO, TextView.BufferType.Normal);
        }

        void btnStop_Click (object sender, EventArgs e)
        {
            this.Finish ();
        }

        void btnStart_Click (object sender, EventArgs e)
        {
          //Check if SD card is mounted
            if (Android.OS.Environment.ExternalStorageState.Equals (Android.OS.Environment.MediaMounted)) {
                if (isStarted) {
                    tm.Stop ();
                    rec.Stop ();
                    rec = null;
                    Finish ();
                }
                else {
                    rec = new AudioRecorder (Android.OS.Environment.ExternalStorageDirectory.AbsolutePath
                    + "/Android/data/" + this.Application.PackageName, "audiotest.3gp");
                    m_offset = TimeSpan.Parse (tvTime.Text);
                    m_startTime = DateTime.Now;
                    tm = new System.Timers.Timer (1000.0);
                    tm.Elapsed += new ElapsedEventHandler (tm_Elapsed);
                    tm.Start ();
                    rec.Start ();
                    isStarted = true;
                    btnStart.Text = GetString ("Stop");
                }
            }
        }

        protected override void OnPause ()
        {
            base.OnPause ();
            this.Save ();
        }

        void tm_Elapsed (object sender, ElapsedEventArgs e)
        {
            DisplayTime ();
        }

        private void DisplayTime ()
        {
            TimeSpan elapsed = (DateTime.Now - m_startTime) + m_offset;
            RunOnUiThread (() => tvTime.SetText (String.Format ("{0:00}:{1:00}:{2:00}", 
                (int)elapsed.TotalHours, elapsed.Minutes, elapsed.Seconds), TextView.BufferType.Normal));
        }

        //Stop the activity from being rotated so that the timer/recording isn't stopped. 
        public override void OnConfigurationChanged (Android.Content.Res.Configuration newConfig)
        {
            base.OnConfigurationChanged (newConfig);
            SetRequestedOrientation ((Android.Content.PM.ScreenOrientation)this.RequestedOrientation);
        }
    }
}

布局文件(audiorecorder.axml):

Layout file (audiorecorder.axml):

<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"
      android:orientation="vertical"
>

  <TextView
    android:id="@+id/tvTimer"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="100px"
    android:gravity="center_horizontal"
    />
  <LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:weightSum="100"
    android:orientation="horizontal"
    >
    <Button
      android:id="@+id/btnStart"
      android:text="@string/Start"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="50"
      />
    <Button
      android:id="@+id/btnStop"
      android:text="@string/CancelButton"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_weight="50"
      />
  </LinearLayout>
</LinearLayout>

这篇关于单声道为Android - 语音录制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆