Skip to content

Commit

Permalink
feat(music play)
Browse files Browse the repository at this point in the history
  • Loading branch information
EdisonE3 committed Dec 10, 2022
1 parent eb1d5a1 commit e9817bc
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 32 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/com/wireless/spyapp/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@ class MainActivity : AppCompatActivity() {
Log.d(TAG, "-----------------------------------------")
for (it in mAccelerometerValues!!){
val data = it.x.toString() + "," + it.y.toString() + "," + it.z.toString()
Log.d("path", applicationContext.filesDir.toString())
// Log.d("path", applicationContext.filesDir.toString())
fileManager?.writeTxtToFile(data, applicationContext.filesDir.toString(), "accelerometer.txt")
Log.d(TAG, it.id.toString() + " accelerometer: [x:" + it.x + ", y:" + it.y + ", z:" + it.z + "]")
// Log.d(TAG, it.id.toString() + " accelerometer: [x:" + it.x + ", y:" + it.y + ", z:" + it.z + "]")

}
mAccelerometerValues?.clear()
Expand Down
9 changes: 6 additions & 3 deletions app/src/main/java/com/wireless/spyapp/MusicActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class MusicActivity : AppCompatActivity(), View.OnClickListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_music)
musicManager = MusicManager()
musicManager = MusicManager(this, R.raw.test)

val btnPlay: Button = findViewById<View>(R.id.btnPlay) as Button
val btnPause: Button = findViewById<View>(R.id.btnPause) as Button
val btnStop: Button = findViewById<View>(R.id.btnStop) as Button
Expand All @@ -30,14 +31,16 @@ class MusicActivity : AppCompatActivity(), View.OnClickListener {
when (v!!.id) {
R.id.btnPlay -> {
Log.d("MusicActivity", "play")
musicManager?.setStart()
//如果没在播放中,立刻开始播放。
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start()
}
}
R.id.btnPause -> {
//如果在播放中,立刻暂停。
Log.d("MusicActivity", "pause")
Log.d("MusicActivity", "pause11")
musicManager?.setPause()
if (mediaPlayer.isPlaying()) {
mediaPlayer.pause()
}
Expand All @@ -47,7 +50,7 @@ class MusicActivity : AppCompatActivity(), View.OnClickListener {
Log.d("MusicActivity", "stop")
if (mediaPlayer.isPlaying()) {
mediaPlayer.reset()
musicManager?.initMediaPlayer() //初始化播放器 MediaPlayer
musicManager?.initMediaPlayer(this, R.raw.test) //初始化播放器 MediaPlayer
}
}
else -> {}
Expand Down
37 changes: 16 additions & 21 deletions app/src/main/java/com/wireless/spyapp/util/MusicManager.kt
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
package com.wireless.spyapp.util

import android.content.Context
import android.media.AudioManager
import android.media.MediaPlayer
import android.os.Environment
import java.io.File
import java.io.IOException


class MusicManager {
// this class is used to play and pause music
// It can be used to play music in the background
// the music can be load from sdcard
var mediaPlayer: MediaPlayer? = null
class MusicManager// construction method with two parameter
(context: Context, resId: Int) {
var mediaPlayer :MediaPlayer? = null
private var isPause = false //是否暂停

// construct function
init {
this.mediaPlayer = MediaPlayer()
initMediaPlayer()
initMediaPlayer(context, resId)
}

fun initMediaPlayer() {
try {
val filePath = ""
mediaPlayer?.setDataSource(filePath)
mediaPlayer!!.isLooping = true //设置为循环播放
mediaPlayer!!.prepare() //初始化播放器MediaPlayer
} catch (e: Exception) {
e.printStackTrace()
}
fun initMediaPlayer(context: Context, resId: Int) {
mediaPlayer = MediaPlayer.create(context, resId)
mediaPlayer?.setLooping(true)
}

fun setPause() {
isPause = true
}

fun setStart() {
isPause = false
}
}
12 changes: 6 additions & 6 deletions app/src/main/res/layout/activity_music.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MusicActivity">
<LinearLayout android:layout_width="412dp" android:layout_height="439dp"
android:orientation="vertical" tools:ignore="MissingConstraints">
<LinearLayout android:layout_width="408dp" android:layout_height="823dp"
android:orientation="vertical" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="0dp">
<Button
android:id="@+id/btnPlay"
android:layout_width="match_parent"
android:layout_width="366dp"
android:layout_height="107dp"
android:textAllCaps="false"
android:text="@string/play"
tools:ignore="MissingConstraints"
/>
<Button
android:id="@+id/btnStop"
android:layout_width="129dp"
android:layout_width="348dp"
android:layout_height="102dp"
android:textAllCaps="false"
android:text="@string/stop"
tools:ignore="MissingConstraints"
/>
android:layout_marginVertical="100dp"/>
<Button
android:id="@+id/btnPause"
android:layout_width="337dp"
android:layout_height="86dp"
android:textAllCaps="false"
android:text="@string/pause"
tools:ignore="MissingConstraints"
/>
android:layout_marginHorizontal="0dp" android:layout_marginVertical="60dp"/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Binary file added app/src/main/res/raw/test.wav
Binary file not shown.

0 comments on commit e9817bc

Please sign in to comment.