Skip to content

Commit

Permalink
chromecast controls
Browse files Browse the repository at this point in the history
  • Loading branch information
jakequade committed Aug 24, 2024
1 parent e9783d2 commit fb6e3dc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
18 changes: 9 additions & 9 deletions components/PlayButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { View } from "react-native";
import CastContext, {
PlayServicesState,
useRemoteMediaClient,
useMediaStatus,
} from "react-native-google-cast";
import { Button } from "./Button";
import { isCancel } from "axios";
Expand All @@ -19,7 +20,8 @@ interface Props extends React.ComponentProps<typeof Button> {
export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
const { showActionSheetWithOptions } = useActionSheet();
const client = useRemoteMediaClient();
const { setCurrentlyPlayingState, isPlaying, currentlyPlaying } = usePlayback();
const { setCurrentlyPlayingState } = usePlayback();
const mediaStatus = useMediaStatus()

const onPress = async () => {
if (!url || !item) return;
Expand All @@ -38,18 +40,17 @@ export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
cancelButtonIndex,
},
async (selectedIndex: number | undefined) => {
const isOpeningCurrentlyPlayingMedia = isPlaying
&& currentlyPlaying?.item?.Name
&& currentlyPlaying?.item?.Name === item?.Name
const currentTitle = mediaStatus?.mediaInfo?.metadata?.title
const isOpeningCurrentlyPlayingMedia = currentTitle && currentTitle === item?.Name

switch (selectedIndex) {
case 0:
await CastContext.getPlayServicesState().then((state) => {
if (state && state !== PlayServicesState.SUCCESS)
CastContext.showPlayServicesErrorDialog(state);
else {
// If we're opening a currently playing item, don't restart the media.
// Instead just open controls
console.log({ isOpeningCurrentlyPlayingMedia, currentlyPlaying })
// Instead just open controls.
if (isOpeningCurrentlyPlayingMedia) {
CastContext.showExpandedControls();
return;
Expand All @@ -66,14 +67,13 @@ export const PlayButton: React.FC<Props> = ({ item, url, ...props }) => {
},
startTime: 0,
}).then(() => {
// state is already set when reopening current media, so skip it here.
if (isOpeningCurrentlyPlayingMedia) {
return
}
setCurrentlyPlayingState({ item, url });
CastContext.showExpandedControls();
}).catch(e => {
console.log({ e })
});
})
}
});
break;
Expand Down
2 changes: 1 addition & 1 deletion plugins/withAndroidMainActivityAttributes.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,4 @@ module.exports = function withAndroidMainActivityAttributes(config, attributes)
config.modResults = addAttributesToMainActivity(config.modResults, attributes);
return config;
});
};
};
6 changes: 4 additions & 2 deletions providers/PlaybackProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,15 @@ export const PlaybackProvider: React.FC<{ children: ReactNode }> = ({
useEffect(() => {
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("http://", "")}/socket?api_key=${
api?.accessToken
}&deviceId=${deviceId}`;

console.log("WS", url);
console.log(protocol, url);

const newWebSocket = new WebSocket(url);

Expand Down

0 comments on commit fb6e3dc

Please sign in to comment.