Skip to content

Commit

Permalink
添加重试按钮
Browse files Browse the repository at this point in the history
  • Loading branch information
cy745 committed Mar 26, 2024
1 parent 0ab98f2 commit 1cb6715
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
22 changes: 19 additions & 3 deletions app/src/main/java/com/lalilu/fpcalc/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.lalilu.fpcalc

import android.annotation.SuppressLint
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.FileIOUtils
Expand All @@ -15,18 +17,32 @@ class MainActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

findViewById<Button>(R.id.retry).setOnClickListener {
calc()
}

calc()
}

@SuppressLint("SetTextI18n")
fun calc() {
GlobalScope.launch(Dispatchers.IO) {
val tempFile = File(cacheDir, "tempAudio")
val inputStream = resources.openRawResource(R.raw.test)
FileIOUtils.writeFileFromIS(tempFile, inputStream)
// 使用openRawResourceFd获取到的fd用于Fpcalc进行读取会导致MediaExtractor创建失败
// 故先通过读取流后,写入到cache文件夹中,再将该文件的path用于读取测试

val params = FpcalcParams(targetFilePath = tempFile.path)
val result = Fpcalc.calc(params)
val params: FpcalcParams = FpcalcParams(targetFilePath = tempFile.path)
val result: FpcalcResult = Fpcalc.calc(params)

val startTime = System.currentTimeMillis()

withContext(Dispatchers.Main) {
this@MainActivity.findViewById<TextView>(R.id.result).text = "$result"
this@MainActivity.findViewById<TextView>(R.id.result).text = """
startTime: $startTime
result:$result
""".trimIndent()
}
}
}
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,13 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/retry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="retry"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/result" />
</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 1cb6715

Please sign in to comment.