MainActivity: prevent crash on file too big
Also, new (and last) sound.
This commit is contained in:
parent
ed91e85e31
commit
0e2d4a8f12
|
@ -5,7 +5,6 @@ import android.app.Activity
|
|||
import android.app.AlertDialog
|
||||
import android.content.ContentValues
|
||||
import android.content.Context
|
||||
import android.content.DialogInterface
|
||||
import android.content.Intent
|
||||
import android.content.pm.PackageManager
|
||||
import android.net.Uri
|
||||
|
@ -153,6 +152,7 @@ class MainActivity : AppCompatActivity() {
|
|||
private fun processVideoUri(inputVideoUri: Uri) {
|
||||
// Save captured video for FFMPEG. Use MP4 extension, completely arbitrary.
|
||||
val inputFile = File.createTempFile("input", ".mp4", cacheDir)
|
||||
try {
|
||||
contentResolver.openInputStream(inputVideoUri).use { inputStream ->
|
||||
if (inputStream == null)
|
||||
return Unit.also { Log.e(TAG, "Could not open input file") }
|
||||
|
@ -160,6 +160,10 @@ class MainActivity : AppCompatActivity() {
|
|||
fos.buffered().write(inputStream.buffered().readBytes())
|
||||
}
|
||||
}
|
||||
} catch (e: OutOfMemoryError) {
|
||||
toast("Video is too large, Android broke!")
|
||||
return
|
||||
}
|
||||
processVideo(inputFile.canonicalPath)
|
||||
}
|
||||
|
||||
|
@ -179,14 +183,14 @@ class MainActivity : AppCompatActivity() {
|
|||
true
|
||||
}
|
||||
} ?: true
|
||||
val duration = mediaInfo?.duration ?: 0
|
||||
val duration = mediaInfo?.duration?.toLong() ?: 0L
|
||||
|
||||
// Save the soundtrack as well cause we can't pass a resource to FFMPEG.
|
||||
// Should be done only once, then it's kept in app internal storage.
|
||||
val themeId = getThemeRawResource(getThemeId())
|
||||
val themeId = getThemeId()
|
||||
val soundFile = File(filesDir, "$themeId.m4a")
|
||||
if (!soundFile.exists()) {
|
||||
resources.openRawResource(themeId).use { resource ->
|
||||
resources.openRawResource(getThemeRawResource(themeId)).use { resource ->
|
||||
FileOutputStream(soundFile).use { fos ->
|
||||
fos.buffered().write(resource.buffered().readBytes())
|
||||
}
|
||||
|
@ -288,10 +292,11 @@ class MainActivity : AppCompatActivity() {
|
|||
}
|
||||
}
|
||||
|
||||
@Suppress("DEPRECATION")
|
||||
private fun save(): Boolean {
|
||||
if (currentVideoPath == null)
|
||||
return false
|
||||
val currentFile = File(currentVideoPath)
|
||||
val currentFile = File(currentVideoPath!!)
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
|
||||
val store = MediaStore.Video.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
|
||||
val videoDetails = ContentValues()
|
||||
|
@ -358,6 +363,7 @@ class MainActivity : AppCompatActivity() {
|
|||
when (id) {
|
||||
0 -> R.raw.harvestdawn
|
||||
1 -> R.raw.sunriseofflutes
|
||||
2 -> R.raw.deathknell
|
||||
else -> R.raw.harvestdawn
|
||||
}
|
||||
|
||||
|
|
BIN
app/src/main/res/raw/deathknell.m4a
Normal file
BIN
app/src/main/res/raw/deathknell.m4a
Normal file
Binary file not shown.
|
@ -3,5 +3,6 @@
|
|||
<string-array name="pick_theme">
|
||||
<item>@string/theme_harvestdawn</item>
|
||||
<item>@string/theme_sunriseofflutes</item>
|
||||
<item>@string/theme_deathknell</item>
|
||||
</string-array>
|
||||
</resources>
|
|
@ -10,4 +10,5 @@
|
|||
<string name="action_theme">Theme</string>
|
||||
<string name="theme_harvestdawn">Harvest Dawn</string>
|
||||
<string name="theme_sunriseofflutes">Sunrise of Flutes</string>
|
||||
<string name="theme_deathknell">Death Knell</string>
|
||||
</resources>
|
Reference in a new issue