Skip to content

Commit

Permalink
Merge pull request #10 from SheTieJun/feat_mute_record
Browse files Browse the repository at this point in the history
Feat mute record
  • Loading branch information
SheTieJun authored Dec 6, 2024
2 parents 75da57f + 89f4a34 commit 07153fd
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 109 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<a href="README.en.md">English</a> | <a href="README.md">简体中文</a>
</p>

- 边录边转码MP3,默认启动系统自带[如果手机支持]的AEC、NC、AGC。
- 边录边转码MP3,默认启动系统自带[如果手机支持]的AEC、NC、AGC,可以通过`enableAudioEffect`进行开启和关闭,除1.9.1是默认关闭的(因为部分手机外接麦克风时,如果是MIC,会没有声音),其他版本都是默认开启
- 支持暂停,实时返回已**录制时长**和当前**声音大小**,已录制的那段音频是**可以播放**的.
- 支持添加背景音乐,可以设置背景音乐声音的大小
- 可以使用默认耳机配置方式:如果没有连接耳机会只用外放的背景音乐,如果连接上了耳机,会使用写入合成背景音乐的方式
Expand Down Expand Up @@ -146,6 +146,11 @@ abstract class BaseRecorder {

open fun setPCMListener(pcmListener: PCMListener?): BaseRecorder {/*...*/
}
/**
* Mute record
* 静音录制:录制进行,但是录制的声音是静音的,使用场景是用于和其他音视频进行拼接
*/
open fun muteRecord(mute: Boolean)

//设计背景音乐的url,本地的(网络的可能造成卡死)
abstract fun setBackgroundMusic(url: String): BaseRecorder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ open class RecordPage(
musicView = view.findViewById(R.id.bg_music_view)
addMusic = view.findViewById(R.id.ll_add_music)
addMusic!!.setOnClickListener(this)
binding.muteAudio.setOnClickListener(this)
}


Expand Down Expand Up @@ -184,6 +185,11 @@ open class RecordPage(
}
}

override fun onMuteRecordChange(mute: Boolean) {
super.onMuteRecordChange(mute)
binding.muteAudio.text = if (mute) "已静音" else "开启禁音"
}

override fun onMaxChange(time: Long) {
mProgressBarRecord!!.max = time.toInt()
mtvAllTime!!.text = Util.formatSeconds3(time.toInt())
Expand Down Expand Up @@ -309,6 +315,7 @@ open class RecordPage(
R.id.tv_save_record -> recordUtils!!.stopFullRecord()
R.id.iv_record_state -> recordUtils!!.startOrPause(oldRecord?.audio_url ?: "")
R.id.ll_add_music -> showMusicDialog()
R.id.mute_audio -> recordUtils?.muteChange()
else -> {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import android.widget.ImageView
import android.widget.SeekBar
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import me.shetj.base.ktx.convertToTime
import me.shetj.base.tools.time.DateUtils.datePattern
import me.shetj.mp3recorder.R
import me.shetj.mp3recorder.record.bean.Record
import me.shetj.mp3recorder.record.utils.MediaPlayerUtils
Expand Down Expand Up @@ -36,7 +38,7 @@ class RecordAdapter(data: MutableList<Record>?) :
mediaUtils.setSeekToPlay(seekBar.progress)
}
}
holder.setText(R.id.tv_name, item.audioName)
holder.setText(R.id.tv_name, (item.audioName)?.toLong()?.convertToTime(datePattern))
.setGone(R.id.rl_record_view2, curPosition != itemPosition)
.setText(R.id.tv_time_all, Util.formatSeconds3(item.audioLength))
.setText(R.id.tv_read_time, Util.formatSeconds3(0))
Expand All @@ -47,7 +49,7 @@ class RecordAdapter(data: MutableList<Record>?) :
override fun convert(holder: BaseViewHolder, item: Record, payloads: List<Any>) {
val itemPosition = holder.layoutPosition - headerLayoutCount
super.convert(holder, item, payloads)
holder.setText(R.id.tv_name, item.audioName)
holder.setText(R.id.tv_name, (item.audioName)?.toLong()?.convertToTime(datePattern))
.setGone(R.id.rl_record_view2, curPosition != itemPosition)
}

Expand Down
33 changes: 25 additions & 8 deletions app/src/main/java/me/shetj/mp3recorder/record/utils/RecordUtils.kt
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class RecordUtils(
audioChannel = 1
mp3BitRate = 128
mp3Quality = 5
enableAudioEffect = false
enableAudioEffect = true
recordListener = this@RecordUtils
permissionListener = this@RecordUtils
pcmListener = this@RecordUtils
Expand All @@ -161,7 +161,6 @@ class RecordUtils(
BaseRecorder.RecorderType.MIX -> it.buildMix(Utils.app)
.also {
it.isEnableVBR(false) // 请不要使用,虽然可以正常播放,但是会时间错误获取会错误,暂时没有解决方法
it.setFilter(3000, 200)
}

BaseRecorder.RecorderType.SIM -> it.buildSim(Utils.app)
Expand Down Expand Up @@ -194,6 +193,10 @@ class RecordUtils(
}
}

private fun mute(mute:Boolean){
mRecorder?.muteRecord(mute)
}

fun setBackgroundPlayerListener(listener: PlayerListener) {
this.listener = listener
mRecorder?.setBackgroundMusicListener(listener)
Expand Down Expand Up @@ -288,6 +291,11 @@ class RecordUtils(
callBack?.onError(e)
}

override fun onMuteRecordChange(mute: Boolean) {
super.onMuteRecordChange(mute)
callBack?.onMuteRecordChange(mute)
}

fun setVolume(volume: Float) {
mRecorder?.setBGMVolume(volume)
}
Expand All @@ -302,12 +310,13 @@ class RecordUtils(
}

override fun onBeforePCMToMp3(pcm: ShortArray): ShortArray {
val pcmdb = calculateRealVolume(pcm, pcm.size)
"修改PCM前DB:$pcmdb".logD("onBeforePCMToMp3")
val adjustVoice = BytesTransUtil.adjustVoice(pcm, 3)
val afterdb = calculateRealVolume(adjustVoice, adjustVoice.size)
"修改PCM后DB:$afterdb".logD("onBeforePCMToMp3")
return adjustVoice
// 处理完成后导致了噪音
// val pcmdb = calculateRealVolume(pcm, pcm.size)
// "修改PCM前DB:$pcmdb".logD("onBeforePCMToMp3")
// val adjustVoice = BytesTransUtil.adjustVoice(pcm, 3)
// val afterdb = calculateRealVolume(adjustVoice, adjustVoice.size)
// "修改PCM后DB:$afterdb".logD("onBeforePCMToMp3")
return pcm
}


Expand All @@ -328,4 +337,12 @@ class RecordUtils(
}
return mVolume
}

fun muteChange() {
if (mRecorder?.mute == true){
mute(false)
}else{
mute(true)
}
}
}
10 changes: 10 additions & 0 deletions app/src/main/res/layout/page_record_mix.xml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,16 @@
android:visibility="gone">
</me.shetj.mp3recorder.record.view.BackgroundMixMusicView>

<TextView
android:id="@+id/mute_audio"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="15dp"
android:layout_above="@id/rl_record_view"
android:background="@drawable/bg_record_add_music"
android:text="开启静音"/>

<LinearLayout
android:id="@+id/ll_add_music"
android:layout_width="wrap_content"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@ import me.shetj.ndk.lame.LameUtils
* <b>@createTime:</b> 2023/3/22<br>
*/
abstract class BaseEncodeThread(
file: File, bufferSize: Int, var isContinue: Boolean, protected val isEnableVBR:Boolean, name: String
file: File, bufferSize: Int, var isContinue: Boolean, protected val isEnableVBR: Boolean, name: String
) : HandlerThread(name), AudioRecord.OnRecordPositionUpdateListener {
protected var path: String
protected var mFileOutputStream: FileOutputStream?
protected val mMp3Buffer: ByteArray
protected var needUpdate = false
protected var mPCMListener: PCMListener? = null

init {
this.mFileOutputStream = FileOutputStream(file, isContinue)
path = file.absolutePath
if(isEnableVBR){
if (isEnableVBR) {
LameUtils.writeVBRHeader(path)
}
mMp3Buffer = ByteArray((7200 + bufferSize.toDouble() * 2.0 * 1.25).toInt())
Expand Down Expand Up @@ -81,16 +82,16 @@ abstract class BaseEncodeThread(
processData()
}

open fun beforePCMtoMP3(pcm:ShortArray): ShortArray {
open fun beforePCMtoMP3(pcm: ShortArray): ShortArray {
if (mPCMListener == null) return pcm
return mPCMListener!!.onBeforePCMToMp3(pcm)
return mPCMListener!!.onBeforePCMToMp3(pcm)
}


abstract fun addTask(rawData: ByteArray, wax: Float, bgData: ByteArray?, bgWax: Float)
abstract fun addTask(rawData: ByteArray, wax: Float, bgData: ByteArray?, bgWax: Float, mute: Boolean)


abstract fun addTask(rawData: ShortArray, readSize: Int)
abstract fun addTask(rawData: ShortArray, readSize: Int, mute: Boolean)

open fun sendStopMessage() {
mHandler?.sendEmptyMessage(PROCESS_STOP)
Expand Down
Loading

0 comments on commit 07153fd

Please sign in to comment.