【问题标题】:Mono for Android - Voice RecordingMono for Android - 录音
【发布时间】:2012-01-03 05:23:04
【问题描述】:

我想知道如何使用 Mono for Android 录制语音。我看过各种Android下录制语音的资料,但似乎都没有涵盖Mono版本的主题。

谢谢。

【问题讨论】:

    标签: android mono xamarin.android voice-recording


    【解决方案1】:

    这是一个使用默认录音机并将音频录制为 .3gp 格式的基本示例。 它有一个带有几个按钮的活动和一个文本视图,在您录制音频时会显示一个计时器。

    活动(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):

    <?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>
    

    【讨论】:

    • 我会试试这个。顺便问一下,您使用哪个程序进行 UI 设计(控件的组成)?
    • 我只是在 Monodevelop 或 Visual Studio 中手动完成。他们在单声道中内置了自动完成功能,用于 android 插件。否则我认为你可以使用 DroidDraw droiddraw.org
    猜你喜欢
    • 2012-10-31
    • 1970-01-01
    • 2013-03-20
    • 2013-10-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多