Merge pull request #152 from evermind-zz/master

Allow to choose a audio source
This commit is contained in:
Tibor Kaputa 2022-10-28 13:39:40 +02:00 committed by GitHub
commit 755fcc1c54
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 93 additions and 8 deletions

View file

@ -1,11 +1,13 @@
package com.simplemobiletools.voicerecorder.activities
import android.media.MediaRecorder
import android.os.Bundle
import com.simplemobiletools.commons.dialogs.ChangeDateTimeFormatDialog
import com.simplemobiletools.commons.dialogs.FilePickerDialog
import com.simplemobiletools.commons.dialogs.RadioGroupDialog
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.NavigationIcon
import com.simplemobiletools.commons.helpers.isNougatPlus
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.commons.helpers.isTiramisuPlus
import com.simplemobiletools.commons.models.RadioItem
@ -39,6 +41,7 @@ class SettingsActivity : SimpleActivity() {
setupSaveRecordingsFolder()
setupExtension()
setupBitrate()
setupAudioSource()
setupRecordAfterLaunch()
updateTextColors(settings_nested_scrollview)
@ -172,4 +175,36 @@ class SettingsActivity : SimpleActivity() {
config.recordAfterLaunch = settings_record_after_launch.isChecked
}
}
private fun setupAudioSource() {
settings_audio_source.text = config.getAudioSourceText(config.audioSource)
settings_audio_source_holder.setOnClickListener {
val items = getAudioSources().map { RadioItem(it, config.getAudioSourceText(it)) } as ArrayList
RadioGroupDialog(this@SettingsActivity, items, config.audioSource) {
config.audioSource = it as Int
settings_audio_source.text = config.getAudioSourceText(config.audioSource)
}
}
}
private fun getAudioSources(): ArrayList<Int> {
val availableSources = arrayListOf(
MediaRecorder.AudioSource.CAMCORDER,
MediaRecorder.AudioSource.DEFAULT,
MediaRecorder.AudioSource.MIC,
MediaRecorder.AudioSource.VOICE_RECOGNITION,
MediaRecorder.AudioSource.VOICE_COMMUNICATION
)
if (isNougatPlus()) {
availableSources.add(MediaRecorder.AudioSource.UNPROCESSED)
}
if (isQPlus()) {
availableSources.add(MediaRecorder.AudioSource.VOICE_PERFORMANCE)
}
return availableSources
}
}

View file

@ -23,6 +23,22 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getInt(EXTENSION, EXTENSION_M4A)
set(extension) = prefs.edit().putInt(EXTENSION, extension).apply()
var audioSource: Int
get() = prefs.getInt(AUDIO_SOURCE, MediaRecorder.AudioSource.CAMCORDER)
set(audioSource) = prefs.edit().putInt(AUDIO_SOURCE, audioSource).apply()
fun getAudioSourceText(audio_source: Int) = context.getString(
when (audio_source) {
MediaRecorder.AudioSource.DEFAULT -> R.string.audio_source_default
MediaRecorder.AudioSource.MIC -> R.string.audio_source_microphone
MediaRecorder.AudioSource.VOICE_RECOGNITION -> R.string.audio_source_voice_recognition
MediaRecorder.AudioSource.VOICE_COMMUNICATION -> R.string.audio_source_voice_communication
MediaRecorder.AudioSource.UNPROCESSED -> R.string.audio_source_unprocessed
MediaRecorder.AudioSource.VOICE_PERFORMANCE -> R.string.audio_source_voice_performance
else -> R.string.audio_source_camcorder
}
)
var bitrate: Int
get() = prefs.getInt(BITRATE, DEFAULT_BITRATE)
set(bitrate) = prefs.edit().putInt(BITRATE, bitrate).apply()
@ -31,11 +47,13 @@ class Config(context: Context) : BaseConfig(context) {
get() = prefs.getBoolean(RECORD_AFTER_LAUNCH, false)
set(recordAfterLaunch) = prefs.edit().putBoolean(RECORD_AFTER_LAUNCH, recordAfterLaunch).apply()
fun getExtensionText() = context.getString(when (extension) {
EXTENSION_M4A -> R.string.m4a
EXTENSION_OGG -> R.string.ogg
else -> R.string.mp3
})
fun getExtensionText() = context.getString(
when (extension) {
EXTENSION_M4A -> R.string.m4a
EXTENSION_OGG -> R.string.ogg
else -> R.string.mp3
}
)
fun getOutputFormat() = when (extension) {
EXTENSION_OGG -> MediaRecorder.OutputFormat.OGG

View file

@ -33,6 +33,7 @@ const val TOGGLE_WIDGET_UI = "toggle_widget_ui"
const val HIDE_NOTIFICATION = "hide_notification"
const val SAVE_RECORDINGS = "save_recordings"
const val EXTENSION = "extension"
const val AUDIO_SOURCE = "audio_source"
const val BITRATE = "bitrate"
const val RECORD_AFTER_LAUNCH = "record_after_launch"

View file

@ -10,7 +10,7 @@ import java.io.FileDescriptor
class MediaRecorderWrapper(val context: Context) : Recorder {
private var recorder = MediaRecorder().apply {
setAudioSource(MediaRecorder.AudioSource.CAMCORDER)
setAudioSource(context.config.audioSource)
setOutputFormat(context.config.getOutputFormat())
setAudioEncoder(context.config.getAudioEncoder())
setAudioEncodingBitRate(context.config.bitrate)

View file

@ -4,7 +4,6 @@ import android.annotation.SuppressLint
import android.content.Context
import android.media.AudioFormat
import android.media.AudioRecord
import android.media.MediaRecorder
import com.naman14.androidlame.AndroidLame
import com.naman14.androidlame.LameBuilder
import com.simplemobiletools.commons.extensions.showErrorToast
@ -33,7 +32,7 @@ class Mp3Recorder(val context: Context) : Recorder {
@SuppressLint("MissingPermission")
private val audioRecord = AudioRecord(
MediaRecorder.AudioSource.CAMCORDER,
context.config.audioSource,
SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,

View file

@ -235,6 +235,29 @@
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_audio_source_holder"
style="@style/SettingsHolderTextViewStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/ripple_background">
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_audio_source_label"
style="@style/SettingsTextLabelStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/audio_source" />
<com.simplemobiletools.commons.views.MyTextView
android:id="@+id/settings_audio_source"
style="@style/SettingsTextValueStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/settings_audio_source_label"
tools:text="128 kbps" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/settings_bitrate_holder"
style="@style/SettingsHolderTextViewStyle"

View file

@ -19,12 +19,21 @@
<!-- Settings -->
<string name="try_hiding_notification">Try hiding the recording notification</string>
<string name="save_recordings_in">Save recordings in</string>
<string name="audio_source">Audio source</string>
<string name="bitrate">Bitrate</string>
<string name="record_after_launch">Start recording automatically after launching the app</string>
<!-- FAQ -->
<string name="faq_1_title">Can I hide the notification icon during recording?</string>
<string name="faq_1_text">Well, it depends. While you use your device it is no longer possible to fully hide the notifications of apps like this.
If you check the proper setting item, the app will do its best to hide it. You can hide it on the lockscreen though, if you disable the displaying of sensitive notifications in your device settings.</string>
<!-- Settings Audio source selection -->
<string name="audio_source_camcorder">Camera</string>
<string name="audio_source_default">Android default</string>
<string name="audio_source_unprocessed">Unprocessed</string>
<string name="audio_source_microphone">Microphone</string>
<string name="audio_source_voice_recognition">Voice recognition</string>
<string name="audio_source_voice_communication">Voice communication</string>
<string name="audio_source_voice_performance">Voice performance</string>
<!--
Haven't found some strings? There's more at
https://github.com/SimpleMobileTools/Simple-Commons/tree/master/commons/src/main/res