Skip to content

Commit

Permalink
feat:
Browse files Browse the repository at this point in the history
update
  • Loading branch information
EdisonE3 committed Jan 10, 2023
1 parent 1ba39ed commit bb09107
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 32 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ android {

defaultConfig {
applicationId "com.wireless.spyapp"
minSdk 29
minSdk 28
targetSdk 30
versionCode 1
versionName "1.0"
Expand Down
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 @@ -209,7 +209,7 @@ class MainActivity : AppCompatActivity() {
for (it in mAccelerometerValues!!){
val data = it.toString()
// Log.d("path", applicationContext.filesDir.toString())
fileManager?.writeTxtToFile(data, applicationContext.filesDir.toString(), "acc"+MusicManager.fileName)
fileManager?.writeTxtToFile(data, applicationContext.getExternalFilesDir(null)?.path.toString(), "acc"+MusicManager.fileName)
// Log.d(TAG, it.id.toString() + " accelerometer: [x:" + it.x + ", y:" + it.y + ", z:" + it.z + "]")

}
Expand All @@ -219,7 +219,7 @@ class MainActivity : AppCompatActivity() {
for (it in mGyroscopeValues!!){
val data = it.toString()
// Log.d("path", applicationContext.filesDir.toString())
fileManager?.writeTxtToFile(data, applicationContext.filesDir.toString(), "gyr"+MusicManager.fileName)
fileManager?.writeTxtToFile(data, applicationContext.getExternalFilesDir(null)?.path.toString(), "gyr"+MusicManager.fileName)
// Log.d(TAG, it.id.toString() + " gyroscope: [x:" + it.x + ", y:" + it.y + ", z:" + it.z + "]")
}
mGyroscopeValues?.clear()
Expand Down
27 changes: 18 additions & 9 deletions app/src/main/java/com/wireless/spyapp/MusicActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,24 @@ class MusicActivity : AppCompatActivity(), View.OnClickListener {
setContentView(R.layout.activity_music)

val list: ArrayList<Int> = ArrayList()
list.add(R.raw.t0_01_0)
list.add(R.raw.t0_01_1)
list.add(R.raw.t0_01_10)
list.add(R.raw.t0_01_11)
list.add(R.raw.t0_01_12)

// get all raw IDs in raw in res dir
val fields = R.raw::class.java.fields
for (i in fields.indices) {
val field = fields[i]
try {
val resId = field.getInt(R.raw::class.java)
list.add(resId)
} catch (e: Exception) {
e.printStackTrace()
}
}


musicManager = MusicManager(this, list)

val btnPlay: Button = findViewById<View>(R.id.btnPlay) as Button
val btnPause: Button = findViewById<View>(R.id.btnPause) as Button
val btnPause: Button = findViewById<View>(R.id.btnTest) as Button
val btnStop: Button = findViewById<View>(R.id.btnStop) as Button

btnPlay.setOnClickListener(this)
Expand All @@ -43,10 +52,10 @@ class MusicActivity : AppCompatActivity(), View.OnClickListener {
// mediaPlayer.start()
// }
}
R.id.btnPause -> {
R.id.btnTest -> {
//如果在播放中,立刻暂停。
Log.d("MusicActivity", "pause")
musicManager?.pauseMusic()
Log.d("MusicActivity", "test data")
musicManager?.testMusic()
// musicManager?.setPause()
// if (mediaPlayer.isPlaying()) {
// mediaPlayer.pause()
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/wireless/spyapp/util/FileManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class FileManager {
fun writeTxtToFile(strcontent: String, filePath: String, fileName: String) {
//生成文件夹之后,再生成文件,不然会出错
makeFilePath(filePath, fileName)
Log.d("FileManager", "writeTxtToFile: $filePath/$fileName")
val strFilePath = "$filePath/$fileName"
// 每次写入时,都换行写
val strContent = """
Expand Down
111 changes: 93 additions & 18 deletions app/src/main/java/com/wireless/spyapp/util/MusicManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,47 @@ package com.wireless.spyapp.util

import android.content.Context
import android.media.MediaPlayer
import android.media.MediaPlayer.OnCompletionListener
import android.os.SystemClock.sleep
import android.util.Log
import kotlin.concurrent.thread


class MusicManager// construction method with two parameter
(context: Context, musicList: ArrayList<Int>) {
// the context of the activity
private var mContext: Context? = null
// A list used to store all media player
private var mediaPlayerList: MutableList<MediaPlayer> = ArrayList()
private var mediaPlayerList: MutableList<Int> = ArrayList()
// The music currently played
private var nextMusicListIndex = 0
// current media player
private var mCurrentMediaPlayer: MediaPlayer? = null

// implement static variable in kotlin
companion object {
var dataSetNumber = 1
var currentMusicNumber = 0 //当前播放的数字
var currentListCount = 0
var count = 0 //计数
var isStart = false
var isPause = true //是否暂停
var fileName = "" //文件名
get() {
return "zero.txt"
return "_" + dataSetNumber + "_" + currentMusicNumber + "_" + currentListCount + ".txt"
}
var count = 0 //计数
}

init {
this.mContext = context
// create media player for each musicList
for (i in musicList.indices) {
Log.d("MusicManager", "init media player: $i")
val mediaPlayer = MediaPlayer.create(context, musicList[i])
mediaPlayer.isLooping = false
mediaPlayerList.add(mediaPlayer)
mediaPlayerList.add(musicList[i])
}
}

fun startMusic() {
Log.d("MusicManager", "start music, isPause: $isPause")
Log.d("MusicManager", "start music, isPause: $isPause, isStart: $isStart")
if (isStart){
if (isPause) {
// 如果暂停了,就继续播放
Expand All @@ -57,31 +58,84 @@ class MusicManager// construction method with two parameter
}
}

fun pauseMusic() {
Log.d("MusicManager", "pause music")
mCurrentMediaPlayer?.pause()
isPause = true
fun testMusic() {
Log.d("MusicManager", "test music")
val thread = Thread {
currentListCount = 777
currentMusicNumber = 777
isPause = false
isStart = true


val playList = listOf(999, 58, 2009, 3222, 654, 4233)
for (i in playList.indices) {
Log.d("MusicManager", "test music: $i")
mCurrentMediaPlayer = MediaPlayer.create(mContext, mediaPlayerList[playList[i]])
mCurrentMediaPlayer?.isLooping = false
mCurrentMediaPlayer?.start()
while (mCurrentMediaPlayer?.isPlaying == true){
if (isPause){
break
}

if (!isStart){
break
}
}
mCurrentMediaPlayer?.stop()
mCurrentMediaPlayer?.release()
sleep(200)
}


isPause = true
isStart = false
currentListCount = 0
currentMusicNumber = 0
}
thread.start()


}

fun stopMusic() {
Log.d("MusicManager", "stop music")
mCurrentMediaPlayer?.stop()
isPause = true
isStart = false
}

private fun startFromZero() {
// launch a new thread to start music
var thread = Thread(Runnable {
val thread = Thread(Runnable {
// currentListCount = 999
// currentMusicNumber = 999
// isPause = false
// isStart = true
// sleep(10000)
// isPause = true
// isStart = false
// currentListCount = 0
// currentMusicNumber = 0

isPause = false
isStart = true
Log.d("MusicManager", "start music from zero, isPause: $isPause, isStart: $isStart")
nextMusicListIndex = 0
count = 0
var currentCount = 0
val select_num = 0
currentMusicNumber = select_num
currentCount = select_num * 500
nextMusicListIndex = currentCount
val endCount = currentCount + 500 * 5
while (nextMusicListIndex < mediaPlayerList.size && !isPause && isStart) {
if (currentCount >= endCount) {
break
}

sleep(200)
Log.d("tag", "开始播放$count 当前:$count 下一首:$nextMusicListIndex size:${mediaPlayerList.size}")
mCurrentMediaPlayer = mediaPlayerList[nextMusicListIndex++]
Log.d("tag", "开始播放$currentCount 下一首:$nextMusicListIndex size:${mediaPlayerList.size}")
mCurrentMediaPlayer = MediaPlayer.create(mContext, mediaPlayerList[nextMusicListIndex++])
mCurrentMediaPlayer?.isLooping = false
mCurrentMediaPlayer?.start()
while (mCurrentMediaPlayer?.isPlaying == true){
if (isPause){
Expand All @@ -94,12 +148,33 @@ class MusicManager// construction method with two parameter
}
mCurrentMediaPlayer?.stop()
mCurrentMediaPlayer?.release()
Log.d("tag", "播放结束$count 当前:$count 下一首:$nextMusicListIndex size:${mediaPlayerList.size}")
Log.d("tag", "播放结束$currentCount 下一首:$nextMusicListIndex size:${mediaPlayerList.size}")


currentCount++
if (currentCount % 500 == 0) {
Log.d("tag", "Stored File Name: $fileName")
isPause = true
sleep(20)
currentListCount = 0
currentMusicNumber++
isPause = false
Log.d("tag", "Stored File Name: $fileName")
} else {
Log.d("tag", "Stored File Name: $fileName")
isPause = true
sleep(20)
currentListCount++
isPause = false
Log.d("tag", "Stored File Name: $fileName")
}
}
Log.d("MusicManager", "all music complete")
isPause = true
isStart = false
})
thread.start()
// thread.join()
Log.d("MusicManager", "start music from zero end")
}
}
4 changes: 2 additions & 2 deletions app/src/main/res/layout/activity_music.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@
tools:ignore="MissingConstraints"
android:layout_marginVertical="100dp"/>
<Button
android:id="@+id/btnPause"
android:id="@+id/btnTest"
android:layout_width="337dp"
android:layout_height="86dp"
android:textAllCaps="false"
android:text="@string/pause"
android:text="@string/test"
tools:ignore="MissingConstraints"
android:layout_marginHorizontal="0dp" android:layout_marginVertical="60dp"/>
</LinearLayout>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
<string name="pause">Pause</string>
<string name="stop">Stop</string>
<string name="back">Back</string>
<string name="test">Test</string>
</resources>

0 comments on commit bb09107

Please sign in to comment.