if the mediastore contains no file duration and size, use other way

This commit is contained in:
tibbi 2020-04-02 15:19:42 +02:00
parent 1b9561aa46
commit db35f2d761
4 changed files with 51 additions and 2 deletions

View File

@ -81,6 +81,7 @@ class RecordingsAdapter(
fun updateItems(newItems: ArrayList<Recording>) { fun updateItems(newItems: ArrayList<Recording>) {
if (newItems.hashCode() != recordings.hashCode()) { if (newItems.hashCode() != recordings.hashCode()) {
recordings = newItems recordings = newItems
notifyDataSetChanged()
finishActMode() finishActMode()
} }
fastScroller?.measureRecyclerView() fastScroller?.measureRecyclerView()

View File

@ -5,6 +5,7 @@ import android.content.ContentUris
import android.content.Context import android.content.Context
import android.database.Cursor import android.database.Cursor
import android.media.AudioManager import android.media.AudioManager
import android.media.MediaMetadataRetriever
import android.media.MediaPlayer import android.media.MediaPlayer
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
@ -18,17 +19,23 @@ import com.simplemobiletools.voicerecorder.activities.SimpleActivity
import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter import com.simplemobiletools.voicerecorder.adapters.RecordingsAdapter
import com.simplemobiletools.voicerecorder.extensions.config import com.simplemobiletools.voicerecorder.extensions.config
import com.simplemobiletools.voicerecorder.interfaces.RefreshRecordingsListener import com.simplemobiletools.voicerecorder.interfaces.RefreshRecordingsListener
import com.simplemobiletools.voicerecorder.models.Events
import com.simplemobiletools.voicerecorder.models.Recording import com.simplemobiletools.voicerecorder.models.Recording
import kotlinx.android.synthetic.main.fragment_player.view.* import kotlinx.android.synthetic.main.fragment_player.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.* import java.util.*
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshRecordingsListener { class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPagerFragment(context, attributeSet), RefreshRecordingsListener {
private val FAST_FORWARD_SKIP_MS = 10000 private val FAST_FORWARD_SKIP_MS = 10000
private var player: MediaPlayer? = null private var player: MediaPlayer? = null
private var progressTimer = Timer() private var progressTimer = Timer()
private var playedRecordingIDs = Stack<Int>() private var playedRecordingIDs = Stack<Int>()
private var bus: EventBus? = null
override fun onResume() { override fun onResume() {
setupColors() setupColors()
@ -39,12 +46,15 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
player?.release() player?.release()
player = null player = null
bus?.unregister(this)
progressTimer.cancel() progressTimer.cancel()
} }
override fun onAttachedToWindow() { override fun onAttachedToWindow() {
super.onAttachedToWindow() super.onAttachedToWindow()
bus = EventBus.getDefault()
bus!!.register(this)
setupColors() setupColors()
setupAdapter() setupAdapter()
initMediaPlayer() initMediaPlayer()
@ -111,6 +121,7 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
private fun setupAdapter() { private fun setupAdapter() {
val recordings = getRecordings() val recordings = getRecordings()
recordings_placeholder.beVisibleIf(recordings.isEmpty()) recordings_placeholder.beVisibleIf(recordings.isEmpty())
if (recordings.isEmpty()) { if (recordings.isEmpty()) {
resetProgress(null) resetProgress(null)
@ -158,8 +169,17 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
val title = cursor.getStringValue(MediaStore.Audio.Media.DISPLAY_NAME) val title = cursor.getStringValue(MediaStore.Audio.Media.DISPLAY_NAME)
val path = "" val path = ""
val timestamp = cursor.getIntValue(MediaStore.Audio.Media.DATE_ADDED) val timestamp = cursor.getIntValue(MediaStore.Audio.Media.DATE_ADDED)
val duration = cursor.getLongValue(MediaStore.Audio.Media.DURATION) / 1000 var duration = cursor.getLongValue(MediaStore.Audio.Media.DURATION) / 1000
val size = cursor.getIntValue(MediaStore.Audio.Media.SIZE) var size = cursor.getIntValue(MediaStore.Audio.Media.SIZE)
if (duration == 0L) {
duration = getDurationFromUri(id.toLong())
}
if (size == 0) {
size = getSizeFromUri(id.toLong())
}
val recording = Recording(id, title, "", timestamp, duration.toInt(), size) val recording = Recording(id, title, "", timestamp, duration.toInt(), size)
recordings.add(recording) recordings.add(recording)
} while (cursor.moveToNext()) } while (cursor.moveToNext())
@ -173,6 +193,27 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
return recordings return recordings
} }
private fun getDurationFromUri(id: Long): Long {
val recordingUri = ContentUris.withAppendedId(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
id
)
val retriever = MediaMetadataRetriever()
retriever.setDataSource(context, recordingUri)
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
}
private fun initMediaPlayer() { private fun initMediaPlayer() {
player = MediaPlayer().apply { player = MediaPlayer().apply {
setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK) setWakeMode(context, PowerManager.PARTIAL_WAKE_LOCK)
@ -308,4 +349,9 @@ class PlayerFragment(context: Context, attributeSet: AttributeSet) : MyViewPager
it.applyColorFilter(textColor) it.applyColorFilter(textColor)
} }
} }
@Subscribe(threadMode = ThreadMode.MAIN)
fun recordingCompleted(event: Events.RecordingCompleted) {
refreshRecordings()
}
} }

View File

@ -4,4 +4,5 @@ class Events {
class RecordingDuration internal constructor(val duration: Int) class RecordingDuration internal constructor(val duration: Int)
class RecordingStatus internal constructor(val isRecording: Boolean) class RecordingStatus internal constructor(val isRecording: Boolean)
class RecordingAmplitude internal constructor(val amplitude: Int) class RecordingAmplitude internal constructor(val amplitude: Int)
class RecordingCompleted internal constructor()
} }

View File

@ -120,6 +120,7 @@ class RecorderService : Service() {
} else { } else {
addFileInLegacyMediaStore() addFileInLegacyMediaStore()
} }
EventBus.getDefault().post(Events.RecordingCompleted())
} }
} }
recorder = null recorder = null