利用麥克風即時收音並經由喇叭播放,可構成簡易助聽器功能。本文提供完整 UI 布局與核心錄音/播放邏輯架構,供開發者參考擴充。
<LinearLayout
xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<Button
android:id="@+id/btnRecord"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="開始邊錄邊放" />
<Button
android:id="@+id/btnStop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="停止" />
<Button
android:id="@+id/btnExit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="退出" />
<TextView
android:id="@+id/TextView01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="程序音量調節"
android:gravity="center"
android:layout_marginTop="16dp" />
<SeekBar
android:id="@+id/skbVolume"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
class MainActivity : AppCompatActivity() {
private lateinit var audioRecord: AudioRecord
private lateinit var audioTrack: AudioTrack
private var isRecording = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnRecord = findViewById<Button>(R.id.btnRecord)
val btnStop = findViewById<Button>(R.id.btnStop)
val btnExit = findViewById<Button>(R.id.btnExit)
val seekBar = findViewById<SeekBar>(R.id.skbVolume)
// 音量控制
val audioManager = getSystemService(Context.AUDIO_SERVICE) as AudioManager
seekBar.max = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
seekBar.progress = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC)
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, progress, 0)
}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
})
btnRecord.setOnClickListener { startRecordPlay() }
btnStop.setOnClickListener { stopRecordPlay() }
btnExit.setOnClickListener { finish() }
}
private fun startRecordPlay() {
if (isRecording) return
isRecording = true
val sampleRate = 44100
val channelConfig = AudioFormat.CHANNEL_IN_MONO
val audioFormat = AudioFormat.ENCODING_PCM_16BIT
val bufferSize = AudioRecord.getMinBufferSize(sampleRate, channelConfig, audioFormat)
audioRecord = AudioRecord(MediaRecorder.AudioSource.MIC, sampleRate, channelConfig, audioFormat, bufferSize)
audioTrack = AudioTrack(
AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_MEDIA).build(),
AudioFormat.Builder().setSampleRate(sampleRate).setChannelMask(AudioFormat.CHANNEL_OUT_MONO).setEncoding(audioFormat).build(),
bufferSize, AudioTrack.MODE_STREAM, AudioManager.AUDIO_SESSION_ID_GENERATE
)
audioRecord.startRecording()
audioTrack.play()
thread {
val buffer = ByteArray(bufferSize)
while (isRecording) {
val read = audioRecord.read(buffer, 0, bufferSize)
if (read > 0) audioTrack.write(buffer, 0, read)
}
}.start()
}
private fun stopRecordPlay() {
isRecording = false
audioRecord?.stop()
audioRecord?.release()
audioTrack?.stop()
audioTrack?.release()
}
override fun onDestroy() {
stopRecordPlay()
super.onDestroy()
}
}
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
RECORD_AUDIO。onDestroy 釋放資源,避免記憶體洩漏與聲音殘留。此範例展示最基礎的即時錄放架構,實際產品化需加入降噪、增益控制、UI 狀態管理等完善功能。