ソースを参照

chore: voice note playback

beautusg 7 年 前
コミット
17cdebc464

+ 0 - 2
app/src/main/java/com/sduduzog/slimlauncher/ui/main/NoteFragment.kt

@@ -51,6 +51,4 @@ class NoteFragment : BaseFragment() {
         bundle.putSerializable(getString(R.string.nav_key_note), note)
         note_fragment_edit.setOnClickListener(Navigation.createNavigateOnClickListener(R.id.action_noteFragment_to_editNoteFragment, bundle))
     }
-
-    override fun onBack(): Boolean = false
 }

+ 54 - 0
app/src/main/java/com/sduduzog/slimlauncher/ui/notes/VoiceNoteFragment.kt

@@ -1,17 +1,71 @@
 package com.sduduzog.slimlauncher.ui.notes
 
+import android.media.MediaPlayer
 import android.os.Bundle
 import android.view.LayoutInflater
 import android.view.View
 import android.view.ViewGroup
+import androidx.lifecycle.Observer
+import androidx.lifecycle.ViewModelProviders
 import com.sduduzog.slimlauncher.R
+import com.sduduzog.slimlauncher.data.MainViewModel
+import com.sduduzog.slimlauncher.data.model.Note
 import com.sduduzog.slimlauncher.utils.BaseFragment
 import kotlinx.android.synthetic.main.voice_note_fragment.*
 
 class VoiceNoteFragment : BaseFragment() {
+
     override fun getFragmentView(): ViewGroup = voice_note_fragment
 
+    private lateinit var viewModel: MainViewModel
+    private var mediaPlayer: MediaPlayer? = null
+
     override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
         return inflater.inflate(R.layout.voice_note_fragment, container, false)
     }
+
+    override fun onActivityCreated(savedInstanceState: Bundle?) {
+        super.onActivityCreated(savedInstanceState)
+
+        activity?.let {
+            viewModel = ViewModelProviders.of(it).get(MainViewModel::class.java)
+        } ?: throw Error("This is just dumb")
+        var id = 0L
+        arguments.let {
+            id = if (it != null && it.containsKey(getString(R.string.nav_key_note))) {
+                it.getLong(getString(R.string.nav_key_note))
+            } else throw Exception("How did we get here??")
+        }
+        viewModel.notes.observe(this, Observer {
+            it?.let { notes ->
+                notes.firstOrNull { n -> n.id == id }?.let { note -> loadNote(note) }
+            }
+        })
+        voice_note_fragment_play.setOnClickListener {
+            if (mediaPlayer!!.isPlaying) {
+                mediaPlayer?.pause()
+                voice_note_fragment_play.text = getString(R.string.voice_note_fragment_play)
+            } else {
+                mediaPlayer?.start()
+                voice_note_fragment_play.text = getString(R.string.voice_note_fragment_pause)
+            }
+        }
+    }
+
+    override fun onStop() {
+        super.onStop()
+        mediaPlayer?.let {
+            if (it.isPlaying) it.stop()
+            it.release()
+        }
+        mediaPlayer = null
+    }
+
+    private fun loadNote(note: Note) {
+        note.title?.let { title -> if (title.isNotBlank()) voice_note_fragment_title.text = title }
+        voice_note_fragment_body.text = note.body
+        mediaPlayer = MediaPlayer()
+        mediaPlayer?.setDataSource(note.path)
+        mediaPlayer?.prepare()
+    }
 }

+ 3 - 3
app/src/main/java/com/sduduzog/slimlauncher/utils/VoiceRecorder.kt

@@ -23,14 +23,14 @@ class VoiceRecorder private constructor() {
 
     fun startRecording(fileName: String) {
         val timestamp = Date().time
-        val title = "Voice note : ${DateFormat.getDateInstance().format(Date(timestamp))}"
-        val body = "Recorded at ${DateFormat.getTimeInstance().format(Date(timestamp))}"
+        val title = "Voice note : ${DateFormat.getDateInstance(DateFormat.SHORT).format(Date(timestamp))}"
+        val body = "Recorded at ${DateFormat.getTimeInstance(DateFormat.SHORT).format(Date(timestamp))}"
         val path = "$fileName/$timestamp.3gp"
         note = Note(timestamp, body, title, timestamp, Note.TYPE_VOICE, path)
         mediaRecorder?.setAudioSource(MediaRecorder.AudioSource.MIC)
         mediaRecorder?.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP)
         mediaRecorder?.setOutputFile(path)
-        mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB)
+        mediaRecorder?.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_WB)
         try {
             mediaRecorder?.prepare()
             mediaRecorder?.start()

+ 27 - 15
app/src/main/res/layout/voice_note_fragment.xml

@@ -16,6 +16,7 @@
         android:background="@android:color/transparent"
         android:hint="@string/edit_note_fragment_untitled"
         android:padding="12dp"
+        android:textAppearance="@style/Base.AlertDialog.AppCompat"
         android:textSize="18sp"
         app:layout_constraintEnd_toStartOf="@+id/voice_note_fragment_save"
         app:layout_constraintStart_toStartOf="parent"
@@ -29,6 +30,7 @@
         android:layout_marginEnd="16dp"
         android:layout_marginRight="16dp"
         android:padding="12dp"
+
         android:text="SHARE"
         android:textSize="18sp"
         app:layout_constraintEnd_toEndOf="parent"
@@ -39,34 +41,44 @@
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:padding="12dp"
+        android:textAppearance="@style/Base.AlertDialog.AppCompat"
         android:textSize="14sp"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@id/voice_note_fragment_title" />
 
     <TextView
-        android:id="@+id/textView"
+        android:id="@+id/voice_note_fragment_counter"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="00:00"
-        android:textSize="38sp"
+        android:textAppearance="@style/Base.AlertDialog.AppCompat"
+        android:textSize="@dimen/_64ssp"
         app:layout_constraintBottom_toBottomOf="parent"
         app:layout_constraintEnd_toEndOf="parent"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toTopOf="parent" />
 
-    <SeekBar
-        android:id="@+id/seekBar"
-        android:layout_width="0dp"
+    <TextView
+        android:id="@+id/voice_note_fragment_play"
+        android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:layout_marginStart="8dp"
-        android:layout_marginLeft="8dp"
-        android:layout_marginEnd="8dp"
-        android:layout_marginRight="8dp"
-        android:layout_marginBottom="64dp"
-        android:max="100"
-        android:progress="13"
-        app:layout_constraintBottom_toBottomOf="parent"
-        app:layout_constraintEnd_toEndOf="parent"
-        app:layout_constraintStart_toStartOf="parent" />
+        android:padding="@dimen/_8sdp"
+        android:text="PLAY"
+        android:textAppearance="@style/Base.AlertDialog.AppCompat"
+        android:textSize="@dimen/_18ssp"
+        app:layout_constraintStart_toStartOf="@+id/voice_note_fragment_counter"
+        app:layout_constraintTop_toBottomOf="@+id/voice_note_fragment_counter" />
+
+    <TextView
+        android:id="@+id/textView3"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        android:padding="@dimen/_8sdp"
+        android:text="STOP"
+        android:textAppearance="@style/Base.AlertDialog.AppCompat"
+        android:textSize="@dimen/_18ssp"
+        app:layout_constraintRight_toRightOf="@+id/voice_note_fragment_counter"
+        app:layout_constraintTop_toTopOf="@+id/voice_note_fragment_play" />
+
 </androidx.constraintlayout.widget.ConstraintLayout>

+ 2 - 0
app/src/main/res/values/strings.xml

@@ -56,5 +56,7 @@
     <string name="edit_note_fragment_untitled">Untitled</string>
     <string name="note_fragment_edit">EDIT</string>
     <string name="nav_key_note">note</string>
+    <string name="voice_note_fragment_pause">PAUSE</string>
+    <string name="voice_note_fragment_play">PLAY</string>
 
 </resources>