1、麦克风的音量实时展示
效果图如下
引入依赖
implementation 'com.github.giswangsj:AndRatingBar:1.0.4'
布局如下
<per.wsj.library.AndRatingBar
android:id="@+id/vrb_volume"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="@id/pb_7"
app:layout_constraintBottom_toBottomOf="@id/pb_7"
app:layout_constraintStart_toEndOf="@id/tv_rest_time"
app:layout_constraintEnd_toStartOf="@id/tv_submit"
android:paddingStart="20dp"
android:paddingEnd="20dp"
android:paddingTop="13dp"
android:paddingBottom="13dp"
android:numStars="20"
app:bgDrawable="@drawable/drawable_voice_off"
app:starDrawable="@drawable/drawable_voice_on"
app:keepOriginColor="true"
android:visibility="gone"
/>
两个drawable都是白色?只是颜色的透明度不一样
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="rectangle">
<solid android:color="#2Effffff"/>
<corners
android:radius="10dp"
/>
<size android:width="4dp" android:height="22dp"/>
</shape>
</item>
</layer-list>
随后在麦克风的音量回调函数中设置控件的rating即可
2、使用retrofit进行文件下载
@Streaming
@GET
fun download(@Url url:String): Call<ResponseBody>
private fun download(downloadUrl: String, savePath: String, callback: DownloadCallback) {
val saveFile = File(savePath)
val handler = Handler(Looper.getMainLooper())
val call = mService.download(downloadUrl)
call.enqueue(object : retrofit2.Callback<ResponseBody>{
override fun onResponse(call: Call<ResponseBody>, response: Response<ResponseBody>) {
launch(Dispatchers.IO, {
if (response.code() != 200) {
handler.post {
callback.onError("download failure: ${response.code()}")
}
}
var inputStream: InputStream? = null
var outputStream: OutputStream? = null
try {
val responseBody = response.body() ?: return@launch
//文件总长度
val totalLen = responseBody.contentLength()
//当前保存的字节数
var curLen = 0
//从请求到的结果中获取输入流
inputStream = responseBody.byteStream()
outputStream = FileOutputStream(saveFile)
//缓存数据的数组,用于将数据从输入流写入到输出流
val buffer = ByteArray(1024)
var bufferLen = 0
while (!call.isCanceled && inputStream.read(buffer).also { bufferLen = it } != -1) {
outputStream.write(buffer, 0, bufferLen)
outputStream.flush()
curLen += bufferLen
//回调下载进度
handler.post {
callback.onProgress(curLen.toFloat() / totalLen, totalLen)
}
}
handler.post {
callback.onSuccess()
}
}catch (e: Throwable) {
if (BuildConfig.DEBUG) {
e.printStackTrace()
}
handler.post {
callback.onError(e.localizedMessage ?: "")
}
}finally {
inputStream?.close()
outputStream?.close()
}
}, {it.printStackTrace()})
}
override fun onFailure(call: Call<ResponseBody>, t: Throwable) {
handler.post {
callback.onError(t.localizedMessage ?: "")
}
}
})
}
interface DownloadCallback {
fun onSuccess()
fun onProgress(curLength: Float, totalLength: Long)
fun onError(msg: String)
}
|