chromecast controls

This commit is contained in:
jakequade
2024-08-24 14:53:33 +10:00
parent e9783d293d
commit fb6e3dc690
3 changed files with 14 additions and 12 deletions

View File

@@ -7,6 +7,7 @@ import { View } from "react-native";
import CastContext, { import CastContext, {
PlayServicesState, PlayServicesState,
useRemoteMediaClient, useRemoteMediaClient,
useMediaStatus,
} from "react-native-google-cast"; } from "react-native-google-cast";
import { Button } from "./Button"; import { Button } from "./Button";
import { isCancel } from "axios"; import { isCancel } from "axios";
@@ -19,7 +20,8 @@ interface Props extends React.ComponentProps<typeof Button> {
export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => { export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
const { showActionSheetWithOptions } = useActionSheet(); const { showActionSheetWithOptions } = useActionSheet();
const client = useRemoteMediaClient(); const client = useRemoteMediaClient();
const { setCurrentlyPlayingState, isPlaying, currentlyPlaying } = usePlayback(); const { setCurrentlyPlayingState } = usePlayback();
const mediaStatus = useMediaStatus()
const onPress = async () => { const onPress = async () => {
if (!url || !item) return; if (!url || !item) return;
@@ -38,9 +40,9 @@ export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
cancelButtonIndex, cancelButtonIndex,
}, },
async (selectedIndex: number | undefined) => { async (selectedIndex: number | undefined) => {
const isOpeningCurrentlyPlayingMedia = isPlaying const currentTitle = mediaStatus?.mediaInfo?.metadata?.title
&& currentlyPlaying?.item?.Name const isOpeningCurrentlyPlayingMedia = currentTitle && currentTitle === item?.Name
&& currentlyPlaying?.item?.Name === item?.Name
switch (selectedIndex) { switch (selectedIndex) {
case 0: case 0:
await CastContext.getPlayServicesState().then((state) => { await CastContext.getPlayServicesState().then((state) => {
@@ -48,8 +50,7 @@ export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
CastContext.showPlayServicesErrorDialog(state); CastContext.showPlayServicesErrorDialog(state);
else { else {
// If we're opening a currently playing item, don't restart the media. // If we're opening a currently playing item, don't restart the media.
// Instead just open controls // Instead just open controls.
console.log({ isOpeningCurrentlyPlayingMedia, currentlyPlaying })
if (isOpeningCurrentlyPlayingMedia) { if (isOpeningCurrentlyPlayingMedia) {
CastContext.showExpandedControls(); CastContext.showExpandedControls();
return; return;
@@ -66,14 +67,13 @@ export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
}, },
startTime: 0, startTime: 0,
}).then(() => { }).then(() => {
// state is already set when reopening current media, so skip it here.
if (isOpeningCurrentlyPlayingMedia) { if (isOpeningCurrentlyPlayingMedia) {
return return
} }
setCurrentlyPlayingState({ item, url }); setCurrentlyPlayingState({ item, url });
CastContext.showExpandedControls(); CastContext.showExpandedControls();
}).catch(e => { })
console.log({ e })
});
} }
}); });
break; break;

View File

@@ -39,4 +39,4 @@ module.exports = function withAndroidMainActivityAttributes(config, attributes)
config.modResults = addAttributesToMainActivity(config.modResults, attributes); config.modResults = addAttributesToMainActivity(config.modResults, attributes);
return config; return config;
}); });
}; };

View File

@@ -181,13 +181,15 @@ export const PlaybackProvider: React.FC<{ children: ReactNode }> = ({
useEffect(() => { useEffect(() => {
if (!deviceId || !api?.accessToken) return; if (!deviceId || !api?.accessToken) return;
const url = `ws://${api?.basePath const protocol = api?.basePath.includes('https') ? 'wss' : 'ws'
const url = `${protocol}://${api?.basePath
.replace("https://", "") .replace("https://", "")
.replace("http://", "")}/socket?api_key=${ .replace("http://", "")}/socket?api_key=${
api?.accessToken api?.accessToken
}&deviceId=${deviceId}`; }&deviceId=${deviceId}`;
console.log("WS", url); console.log(protocol, url);
const newWebSocket = new WebSocket(url); const newWebSocket = new WebSocket(url);