From 29396d17dfd5b30bb1b74d80e5eb4c27a0b0a447 Mon Sep 17 00:00:00 2001 From: lyswhut Date: Sat, 7 Aug 2021 11:45:55 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E9=80=80=E5=87=BA=E5=BA=94?= =?UTF-8?q?=E7=94=A8=E7=9A=84=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/lxmusicmobile/utils/UtilsModule.java | 18 ++++++++++++- package-lock.json | 4 +-- package.json | 2 +- publish/changeLog.md | 4 +++ src/navigation/components/PactModal.js | 2 +- src/plugins/player/service.js | 26 ++++++++++++++----- src/utils/errorHandle.js | 2 +- src/utils/timeoutExit.js | 8 +++--- 8 files changed, 50 insertions(+), 16 deletions(-) diff --git a/android/app/src/main/java/com/lxmusicmobile/utils/UtilsModule.java b/android/app/src/main/java/com/lxmusicmobile/utils/UtilsModule.java index 23603ef..a4e00df 100644 --- a/android/app/src/main/java/com/lxmusicmobile/utils/UtilsModule.java +++ b/android/app/src/main/java/com/lxmusicmobile/utils/UtilsModule.java @@ -37,7 +37,23 @@ public class UtilsModule extends ReactContextBaseJavaModule { @ReactMethod public void exitApp() { // https://github.com/wumke/react-native-exit-app/blob/master/android/src/main/java/com/github/wumke/RNExitApp/RNExitAppModule.java - android.os.Process.killProcess(android.os.Process.myPid()); + // android.os.Process.killProcess(android.os.Process.myPid()); + + // https://stackoverflow.com/questions/6330200/how-to-quit-android-application-programmatically + Activity currentActivity = reactContext.getCurrentActivity(); + if (currentActivity == null) { + Log.d("Utils", "killProcess"); + android.os.Process.killProcess(android.os.Process.myPid()); + } else { + if(Build.VERSION.SDK_INT >= 21){ + currentActivity.finishAndRemoveTask(); + } else if(Build.VERSION.SDK_INT >= 16){ + currentActivity.finishAffinity(); + } else{ + currentActivity.finish(); + } + System.exit(0); + } } @ReactMethod diff --git a/package-lock.json b/package-lock.json index a033881..426d257 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10682,8 +10682,8 @@ "integrity": "sha512-Ls9qiNZzW/OLFoI25wfjjAcrf2DZ975hn2vr6U9gyuxi2nooVbzQeFoQS5vQcbCt9QX5NY8ASEEAtlLdIa6KVg==" }, "react-native-track-player": { - "version": "git+https://github.com/lyswhut/react-native-track-player.git#732171172d64194698a130ab5edbde6c3fa00a44", - "from": "git+https://github.com/lyswhut/react-native-track-player.git#732171172d64194698a130ab5edbde6c3fa00a44" + "version": "git+https://github.com/lyswhut/react-native-track-player.git#7d5ed18acc7cade159d895c4148b97082e1a08a1", + "from": "git+https://github.com/lyswhut/react-native-track-player.git#7d5ed18acc7cade159d895c4148b97082e1a08a1" }, "react-native-vector-icons": { "version": "8.1.0", diff --git a/package.json b/package.json index 1de2f7f..1b291e0 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "react-native-pager-view": "^5.4.0", "react-native-randombytes": "^3.6.1", "react-native-splash-screen": "^3.2.0", - "react-native-track-player": "git+https://github.com/lyswhut/react-native-track-player.git#732171172d64194698a130ab5edbde6c3fa00a44", + "react-native-track-player": "git+https://github.com/lyswhut/react-native-track-player.git#7d5ed18acc7cade159d895c4148b97082e1a08a1", "react-native-vector-icons": "^8.1.0", "react-redux": "^7.2.4", "readable-stream": "1.0.33", diff --git a/publish/changeLog.md b/publish/changeLog.md index 055897a..a306ae5 100644 --- a/publish/changeLog.md +++ b/publish/changeLog.md @@ -2,6 +2,10 @@ - 新增局域网同步功能(实验性,首次使用前建议先备份一次列表),此功能需要配合PC端使用,移动端与PC端处在同一个局域网(路由器的网络)下时,可以多端实时同步歌曲列表,使用问题请看"常见问题"。 +### 优化 + +- 优化退出应用的机制,现在在需要退出应用的场景将会完全退出应用 + ### 修复 - 修复某些情况下出现恢复播放信息失败的问题 diff --git a/src/navigation/components/PactModal.js b/src/navigation/components/PactModal.js index d28412b..764098f 100644 --- a/src/navigation/components/PactModal.js +++ b/src/navigation/components/PactModal.js @@ -16,7 +16,7 @@ const VersionModal = ({ componentId }) => { const handleRejct = () => { exitApp() - Navigation.dismissOverlay(componentId) + // Navigation.dismissOverlay(componentId) } const handleConfirm = () => { diff --git a/src/plugins/player/service.js b/src/plugins/player/service.js index 0a41829..4dad28b 100644 --- a/src/plugins/player/service.js +++ b/src/plugins/player/service.js @@ -3,7 +3,7 @@ import { getStore } from '@/store' import { action as playerAction, STATUS } from '@/store/modules/player' import { isTempTrack } from './utils' import { play as lrcPlay, pause as lrcPause } from '@/utils/lyric' -import { exitApp } from '@/utils/tools' +import { exitApp } from '@/utils/utils' const store = getStore() @@ -50,9 +50,9 @@ let errorTime = 0 // }, // } // 销毁播放器并退出 -const stopPlay = () => { +const handleExitApp = async() => { global.isPlayedExit = false - TrackPlayer.pause() + await TrackPlayer.pause() store.dispatch(playerAction.destroy()).finally(() => { exitApp() }) @@ -88,8 +88,20 @@ export default async() => { TrackPlayer.addEventListener('remote-stop', async() => { console.log('remote-stop') - await store.dispatch(playerAction.destroy()) - exitApp() + handleExitApp() + }) + + TrackPlayer.addEventListener('remote-duck', async({ permanent, paused, ducking }) => { + console.log('remote-duck') + if (paused) { + store.dispatch(playerAction.setStatus({ status: STATUS.pause, text: '已暂停' })) + lrcPause() + } else { + store.dispatch(playerAction.setStatus({ status: STATUS.playing, text: '播放中...' })) + TrackPlayer.getPosition().then(position => { + lrcPlay(position * 1000) + }) + } }) TrackPlayer.addEventListener('playback-error', async err => { @@ -164,11 +176,11 @@ export default async() => { console.log('playback-state', info) break } - if (global.isPlayedExit) return stopPlay() + if (global.isPlayedExit) return handleExitApp() }) TrackPlayer.addEventListener('playback-track-changed', async info => { // console.log('nextTrack====>', info) - if (global.isPlayedExit) return stopPlay() + if (global.isPlayedExit) return handleExitApp() trackId = await TrackPlayer.getCurrentTrack() if (trackId && isTempTrack(trackId)) { diff --git a/src/utils/errorHandle.js b/src/utils/errorHandle.js index 95ac7c4..9a27328 100644 --- a/src/utils/errorHandle.js +++ b/src/utils/errorHandle.js @@ -1,5 +1,5 @@ import { Alert } from 'react-native' -import { exitApp } from '@/utils/tools' +import { exitApp } from '@/utils/utils' import { setJSExceptionHandler, setNativeExceptionHandler } from 'react-native-exception-handler' import { log } from '@/utils/log' diff --git a/src/utils/timeoutExit.js b/src/utils/timeoutExit.js index 413bc21..c39a154 100644 --- a/src/utils/timeoutExit.js +++ b/src/utils/timeoutExit.js @@ -2,7 +2,7 @@ import { useEffect, useState } from 'react' import BackgroundTimer from 'react-native-background-timer' import { getStore } from '@/store' import { action as playerAction, STATUS } from '@/store/modules/player' -import { exitApp } from '@/utils/tools' +import { exitApp } from '@/utils/utils' const isStop = status => { switch (status) { @@ -23,8 +23,10 @@ const timeoutTools = { if (state.common.setting.player.timeoutExitPlayed && !isStop(state.player.status)) { global.isPlayedExit = true } else { - store.dispatch(playerAction.destroy()).finally(() => { - exitApp() + store.dispatch(playerAction.pauseMusic()).finally(() => { + store.dispatch(playerAction.destroy()).finally(() => { + exitApp() + }) }) } },