Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: pause playback on mute #663

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.noxplay.noxplayer

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.media.AudioManager
import android.os.Bundle

class APMVolumeListener(val emit: (t: String, b: Bundle) -> Unit): BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action == "android.media.VOLUME_CHANGED_ACTION" &&
intent.extras?.getInt("android.media.EXTRA_VOLUME_STREAM_TYPE") == AudioManager.STREAM_MUSIC) {
emit("APMVolume", Bundle().apply { putInt("volume",
intent.extras?.getInt("android.media.EXTRA_VOLUME_STREAM_VALUE") ?: -1
)})
}
}
}
10 changes: 10 additions & 0 deletions android/app/src/main/java/com/noxplay/noxplayer/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import android.app.PictureInPictureParams
import android.content.ComponentCallbacks2
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.res.Configuration
import android.os.Build
import android.os.Bundle
Expand All @@ -33,11 +34,19 @@ interface NoxActivity {

class MainActivity(override var loadedRN: Boolean = false) :
ReactActivity(), ComponentCallbacks2, NoxActivity {


fun emit (tag: String, data: Bundle) {
getReactContext()?.emitDeviceEvent(tag, Arguments.fromBundle(data))
}
private lateinit var volumeListener: APMVolumeListener
/**
* for react navigation;
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(null)
volumeListener = APMVolumeListener(::emit)
registerReceiver(volumeListener, IntentFilter("android.media.VOLUME_CHANGED_ACTION"))

val content: View = findViewById(android.R.id.content)
content.viewTreeObserver.addOnPreDrawListener(
Expand Down Expand Up @@ -165,6 +174,7 @@ class MainActivity(override var loadedRN: Boolean = false) :
}

override fun onDestroy() {
unregisterReceiver(volumeListener)
// https://stackoverflow.com/questions/5764099/how-to-update-a-widget-if-the-related-service-gets-killed
val am = getSystemService(Context.ALARM_SERVICE) as AlarmManager
val intent = Intent(this, APMWidget::class.java)
Expand Down
21 changes: 10 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,18 @@
"@react-native-community/netinfo": "^11.4.1",
"@react-native-cookies/cookies": "^6.2.1",
"@react-native-masked-view/masked-view": "^0.3.2",
"@react-native/babel-preset": "^0.76.2",
"@react-native/gradle-plugin": "^0.76.2",
"@react-native/metro-config": "^0.76.2",
"@react-native/typescript-config": "0.76.2",
"@react-native/babel-preset": "^0.76.3",
"@react-native/gradle-plugin": "^0.76.3",
"@react-native/metro-config": "^0.76.3",
"@react-native/typescript-config": "0.76.3",
"@react-navigation/drawer": "^7.0.6",
"@react-navigation/native": "^7.0.3",
"@react-navigation/native-stack": "^7.1.0",
"@revenuecat/purchases-js": "^0.12.1",
"@revenuecat/purchases-js": "^0.13.0",
"@sentry/react-native": "^6.2.0",
"@sharcoux/slider": "8.0.6",
"@shopify/flash-list": "^1.7.2",
"@shopify/react-native-skia": "1.5.4",
"@stripe/stripe-react-native": "^0.39.0",
"@shopify/react-native-skia": "1.5.7",
"axios": "^1.7.7",
"base-64": "^1.0.0",
"base64-js": "^1.5.1",
Expand All @@ -62,7 +61,7 @@
"deepmerge": "^4.3.1",
"dropbox": "git+https://lovegaoshi@github.com/lovegaoshi/dropbox-sdk-js.git",
"event-target-polyfill": "^0.0.4",
"expo": "^52.0.8",
"expo": "^52.0.9",
"expo-auth-session": "~6.0.0",
"expo-clipboard": "~7.0.0",
"expo-crypto": "~14.0.1",
Expand All @@ -74,7 +73,7 @@
"fflate": "^0.8.2",
"ffmpeg-kit-react-native": "^6.0.2",
"he": "^1.2.0",
"i18next": "^23.16.6",
"i18next": "^23.16.8",
"js-base64": "^3.7.7",
"libmuse": "https://github.com/lovegaoshi/muse.git#commit=0bffe2319baf825037bece6869d360d6d307eb72",
"lodash": "^4.17.21",
Expand Down Expand Up @@ -103,9 +102,9 @@
"react-native-paper": "^5.12.5",
"react-native-purchases": "^8.2.7",
"react-native-qrcode-svg": "^6.3.12",
"react-native-reanimated": "3.16.1",
"react-native-reanimated": "3.16.2",
"react-native-safe-area-context": "^4.14.0",
"react-native-screens": "4.2.0",
"react-native-screens": "4.3.0",
"react-native-shadow-2": "^7.1.1",
"react-native-share-menu": "git+https://github.com/Expensify/react-native-share-menu.git",
"react-native-svg": "15.9.0",
Expand Down
4 changes: 2 additions & 2 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function App(appProps: NoxComponent.AppProps) {
const isSplashReady = useSplash(
__DEV__ || appProps.intentData || vip ? 1 : 2500,
);
const [isSplashAnimReady, setIsSplashReady] = React.useState(false);
const [isSplashAnimReady, setIsSplashAnimReady] = React.useState(vip);
const isPlayerReady = useSetupPlayer({ ...appProps, vip });
const isLandscape = useIsLandscape();
const PIPMode = useStore(appStore, state => state.pipMode);
Expand All @@ -81,7 +81,7 @@ export default function App(appProps: NoxComponent.AppProps) {
if (!(isPlayerReady && isSplashReady && isSplashAnimReady)) {
return (
<SafeAreaView style={styles.screenContainer}>
<AppOpenSplash setIsSplashReady={setIsSplashReady} />
<AppOpenSplash setIsSplashReady={setIsSplashAnimReady} />
</SafeAreaView>
);
}
Expand Down
1 change: 0 additions & 1 deletion src/components/landscape/AzusaPlayerLandscape.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ const AzusaPlayer = () => {
const actionPanelWidth = Math.max(50, Math.min(120, height / 5));
const playerPanelWidth = Math.max(50, width / 2 - actionPanelWidth);

console.log(actionPanelWidth, playerPanelWidth);
return (
<PaperProvider
theme={{
Expand Down
4 changes: 4 additions & 0 deletions src/components/setting/GeneralSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,10 @@ const GEN_SETTING_BOOLEAN: SettingEntry[] = [
settingName: 'preferYTMSuggest',
settingCategory: 'GeneralSettings',
},
{
settingName: 'pausePlaybackOnMute',
settingCategory: 'GeneralSettings',
},
];

export default () => {
Expand Down
2 changes: 2 additions & 0 deletions src/localization/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,8 @@
"prefetchTrackDesc": "Prefetch the next track (only works when cache is enabled)",
"chatGPTResolveSongNameName": "ChatGPT on Songs",
"chatGPTResolveSongNameDesc": "TODO: Use chatGPT to resolve song names",
"pausePlaybackOnMuteName": "Pause Playback on Mute",
"pausePlaybackOnMuteDesc": "Android only. Pause playback when volume becomes 0",
"artworkCarouselName": "artwork carousel",
"artworkCarouselDesc": "enable artwork carousel (swipe to skip; bugs)",
"preferYTMSuggestName": "Prefers YTM suggest",
Expand Down
2 changes: 2 additions & 0 deletions src/localization/zhcn/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@
"prefetchTrackDesc": "预加载下一首歌(仅在歌曲缓存开启时有用)",
"chatGPTResolveSongNameName": "ChatGPT找歌",
"chatGPTResolveSongNameDesc": "TODO:用chatGPT解析歌名",
"pausePlaybackOnMuteName": "静音时暂停播放",
"pausePlaybackOnMuteDesc": "在Android上静音时暂停播放",
"artworkCarouselName": "artwork carousel",
"artworkCarouselDesc": "artowrk carousel",
"preferYTMSuggestName": "优先使用YTM搜索",
Expand Down
1 change: 1 addition & 0 deletions src/objects/Storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const DefaultSetting: NoxStorage.PlayerSettingDict = {
preferYTMSuggest: true,
artworkRes: 0,
artworkCarousel: false,
pausePlaybackOnMute: false,

appID: AppID,
language: undefined,
Expand Down
7 changes: 7 additions & 0 deletions src/services/PlaybackService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ export async function additionalPlaybackService({
}

export async function PlaybackService() {
DeviceEventEmitter.addListener(
'APMVolume',
(e: { volume: number }) =>
e.volume === 0 &&
useNoxSetting.getState().playerSetting.pausePlaybackOnMute &&
TrackPlayer.pause(),
);
DeviceEventEmitter.addListener('APMEnterPIP', (e: boolean) =>
setState({ pipMode: e }),
);
Expand Down
1 change: 1 addition & 0 deletions src/types/storage.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ declare global {
preferYTMSuggest: boolean;
artworkRes: number;
artworkCarousel: boolean;
pausePlaybackOnMute: boolean;

appID: string;
language?: string;
Expand Down
Loading