| @@ -12,7 +12,11 @@ import kotlin.concurrent.timerTask | |||||
| import android.content.Intent | import android.content.Intent | ||||
| import android.R.attr.data | import android.R.attr.data | ||||
| import android.app.Activity | import android.app.Activity | ||||
| import android.content.Context | |||||
| import android.net.Uri | import android.net.Uri | ||||
| import java.io.BufferedInputStream | |||||
| import java.io.BufferedOutputStream | |||||
| import java.io.FileInputStream | |||||
| class MainActivity : AppCompatActivity() { | class MainActivity : AppCompatActivity() { | ||||
| @@ -20,6 +24,8 @@ class MainActivity : AppCompatActivity() { | |||||
| super.onCreate(savedInstanceState) | super.onCreate(savedInstanceState) | ||||
| setContentView(R.layout.activity_main) | setContentView(R.layout.activity_main) | ||||
| loadPreferences() | |||||
| var player: MediaPlayer? = null | var player: MediaPlayer? = null | ||||
| val progressBar = findViewById<ProgressBar>(R.id.progress_bar) | val progressBar = findViewById<ProgressBar>(R.id.progress_bar) | ||||
| val objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 1000) | val objectAnimator = ObjectAnimator.ofInt(progressBar, "progress", progressBar.getProgress(), 1000) | ||||
| @@ -45,8 +51,9 @@ class MainActivity : AppCompatActivity() { | |||||
| playing = false | playing = false | ||||
| progressBar.progress = 0 | progressBar.progress = 0 | ||||
| } else { | } else { | ||||
| currentUri?.let { uri -> | |||||
| val mp = MediaPlayer.create(this, uri) | |||||
| val file = getFileStreamPath("audio.mp3") | |||||
| if (file.exists()) { | |||||
| val mp = MediaPlayer.create(this, Uri.fromFile(file)) | |||||
| mp.setOnCompletionListener { | mp.setOnCompletionListener { | ||||
| progressBar.progress = 0 | progressBar.progress = 0 | ||||
| mp.reset() | mp.reset() | ||||
| @@ -65,7 +72,6 @@ class MainActivity : AppCompatActivity() { | |||||
| btn.setOnLongClickListener { | btn.setOnLongClickListener { | ||||
| val intent = Intent(baseContext, EditActivity::class.java) | val intent = Intent(baseContext, EditActivity::class.java) | ||||
| intent.putExtra("uri", currentUri) | |||||
| intent.putExtra("caption", btn.text) | intent.putExtra("caption", btn.text) | ||||
| startActivityForResult(intent, 1234) | startActivityForResult(intent, 1234) | ||||
| @@ -73,7 +79,14 @@ class MainActivity : AppCompatActivity() { | |||||
| } | } | ||||
| } | } | ||||
| var currentUri: Uri? = null | |||||
| private fun loadPreferences() { | |||||
| val preferences = getPreferences(Context.MODE_PRIVATE) | |||||
| val caption = preferences.getString("caption", "") | |||||
| val btn = findViewById<TextView>(R.id.text_view_button) | |||||
| btn.setText(caption) | |||||
| } | |||||
| // var currentUri: Uri? = null | |||||
| override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { | ||||
| super.onActivityResult(requestCode, resultCode, data) | super.onActivityResult(requestCode, resultCode, data) | ||||
| @@ -84,9 +97,29 @@ class MainActivity : AppCompatActivity() { | |||||
| if (caption != null) { | if (caption != null) { | ||||
| btn.setText(caption) | btn.setText(caption) | ||||
| } | } | ||||
| currentUri = data.getParcelableExtra<Uri>("uri") | |||||
| if (currentUri != null) { | |||||
| grantUriPermission(getPackageName(), currentUri, Intent.FLAG_GRANT_READ_URI_PERMISSION) | |||||
| val uri = data.getParcelableExtra<Uri>("uri") | |||||
| savePreferences(caption, uri) | |||||
| } | |||||
| } | |||||
| private fun savePreferences(caption: String, uri: Uri?) { | |||||
| val btn = findViewById<TextView>(R.id.text_view_button) | |||||
| val editor = getPreferences(Context.MODE_PRIVATE).edit() | |||||
| editor.commit() | |||||
| uri?.let { uri -> | |||||
| grantUriPermission(getPackageName(), uri, Intent.FLAG_GRANT_READ_URI_PERMISSION) | |||||
| val output = BufferedOutputStream(openFileOutput("audio.mp3", Context.MODE_PRIVATE)) | |||||
| val input = BufferedInputStream(getContentResolver().openInputStream(uri)) | |||||
| try { | |||||
| val buf = ByteArray(1024) | |||||
| input.read(buf) | |||||
| do { | |||||
| output.write(buf) | |||||
| } while (input.read(buf) !== -1) | |||||
| } finally { | |||||
| input.close() | |||||
| output.close() | |||||
| } | } | ||||
| } | } | ||||
| } | } | ||||