adding a helper function for getting audio file uris

This commit is contained in:
tibbi
2020-04-02 15:30:32 +02:00
parent db35f2d761
commit cd88ac1343
3 changed files with 24 additions and 23 deletions

View File

@ -1,6 +1,5 @@
package com.simplemobiletools.voicerecorder.dialogs
import android.content.ContentUris
import android.content.ContentValues
import android.provider.MediaStore
import androidx.appcompat.app.AlertDialog
@ -9,6 +8,7 @@ import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.helpers.ensureBackgroundThread
import com.simplemobiletools.commons.helpers.isQPlus
import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.helpers.getAudioFileContentUri
import com.simplemobiletools.voicerecorder.models.Recording
import kotlinx.android.synthetic.main.dialog_rename_recording.view.*
@ -52,8 +52,6 @@ class RenameRecordingDialog(val activity: BaseSimpleActivity, val recording: Rec
}
private fun updateMediaStoreTitle(recording: Recording, newTitle: String) {
val baseUri = MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
val uri = ContentUris.withAppendedId(baseUri, recording.id.toLong())
val oldExtension = recording.title.getFilenameExtension()
val newDisplayName = "${newTitle.removeSuffix(".$oldExtension")}.$oldExtension"
@ -62,6 +60,6 @@ class RenameRecordingDialog(val activity: BaseSimpleActivity, val recording: Rec
put(MediaStore.Audio.Media.DISPLAY_NAME, newDisplayName)
}
activity.contentResolver.update(uri, values, null, null)
activity.contentResolver.update(getAudioFileContentUri(recording.id.toLong()), values, null, null)
}
}

View File

@ -1,7 +1,6 @@
package com.simplemobiletools.voicerecorder.fragments
import android.annotation.SuppressLint
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.media.AudioManager
@ -18,6 +17,7 @@ import com.simplemobiletools.voicerecorder.R
import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter
import com.simplemobiletools.voicerecorder.extensions.config
import com.simplemobiletools.voicerecorder.helpers.getAudioFileContentUri
import com.simplemobiletools.voicerecorder.interfaces.RefreshRecordingsListener
import com.simplemobiletools.voicerecorder.models.Events
import com.simplemobiletools.voicerecorder.models.Recording
@ -194,24 +194,15 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
}
private fun getDurationFromUri(id: Long): Long {
val recordingUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
id
)
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, recordingUri)
retriever.setDataSource(context, getAudioFileContentUri(id))
val time = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION)
return Math.round(time.toLong() / 1000.toDouble())
}
private fun getSizeFromUri(id: Long): Int {
val recordingUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
id
)
return context.contentResolver.openInputStream(recordingUri)?.available()?.toInt() ?: 0
val recordingUri = getAudioFileContentUri(id)
return context.contentResolver.openInputStream(recordingUri)?.available() ?: 0
}
private fun initMediaPlayer() {
@ -234,17 +225,12 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
}
override fun playRecording(recording: Recording) {
val recordingUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
recording.id.toLong()
)
resetProgress(recording)
(recordings_list.adapter as RecordingsAdapter).updateCurrentRecording(recording.id)
player!!.apply {
reset()
setDataSource(context, recordingUri)
setDataSource(context, getAudioFileContentUri(recording.id.toLong()))
prepare()
}

View File

@ -1,5 +1,11 @@
package com.simplemobiletools.voicerecorder.helpers
import android.annotation.SuppressLint
import android.content.ContentUris
import android.net.Uri
import android.provider.MediaStore
import com.simplemobiletools.commons.helpers.isQPlus
const val RECORDER_RUNNING_NOTIF_ID = 10000
private const val PATH = "com.simplemobiletools.voicerecorder.action."
@ -9,3 +15,14 @@ const val STOP_AMPLITUDE_UPDATE = PATH + "STOP_AMPLITUDE_UPDATE"
// shared preferences
const val HIDE_NOTIFICATION = "hide_notification"
const val SAVE_RECORDINGS = "save_recordings"
@SuppressLint("InlinedApi")
fun getAudioFileContentUri(id: Long): Uri {
val baseUri = if (isQPlus()) {
MediaStore.Audio.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY)
} else {
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI
}
return ContentUris.withAppendedId(baseUri, id)
}