Bỏ qua để đến nội dung

Giữ để nói

Giữ để nói, thả để gửi, trượt sang trái để huỷ.

Demo chạy thật, cần micro.Mở toàn màn hình ↗
  1. Spread holdProps vào một View bất kỳ: hook lo cử chỉ và phiên nghe, bạn lo giao diện.
  2. onSend nhận trọn tin nhắn. Khoảng lặng không cắt ngang, và nó nghe thêm một chút sau khi thả tay nên không mất chữ cuối.
  3. isCancelling là true khi ngón tay đã trượt đủ xa: đổi nút sang đỏ. Trượt về thì hết huỷ.
  4. onCancel cho biết vì sao không có gì để gửi, nên chạm quá nhanh cũng có lời giải thích.
  5. Trên web, phím cách và Enter giữ được nút. Trình đọc màn hình chạm để bắt đầu và chạm lần nữa để gửi.

Dùng

Chính file này đang chạy trong điện thoại: trang đọc nó trực tiếp từ repo.

example/scenes/chat.tsx
import { Ionicons } from '@expo/vector-icons';
import { useHoldToTalk, type HoldCancelReason } from 'expo-stt';
import { useState } from 'react';
import { FlatList, StyleSheet, Text, View, type ViewStyle } from 'react-native';
import { ErrorNote, Screen, useDemo, Waveform } from './ui';
const BLUE = '#2f5bea';
// Web only: otherwise phones scroll the page instead of sliding to cancel.
const NO_SCROLL = { touchAction: 'none' } as ViewStyle;
type Message = { id: number; text: string; isMine: boolean };
const HELLO: Message[] = [
{ id: 2, text: 'Hold the button and talk to me.', isMine: false },
{ id: 1, text: 'Too busy to type?', isMine: false },
];
const HINTS: Record<HoldCancelReason, string> = {
cancelled: 'Message discarded.',
'too-short': 'Hold the button while you talk.',
'not-ready': 'Ready. Hold the button to talk.',
empty: 'I did not hear anything.',
};
/** Hold to speak, release to send, slide left to cancel. */
export function Chat() {
const { locale } = useDemo();
const [messages, setMessages] = useState(HELLO);
const [hint, setHint] = useState('');
const talk = useHoldToTalk({
locale,
onSend: (text) => {
setHint('');
setMessages((previous) => [{ id: previous.length + 1, text, isMine: true }, ...previous]);
},
onCancel: (reason) => setHint(HINTS[reason]),
onError: () => setHint(''),
});
const label = talk.isCancelling
? 'Release to cancel'
: talk.isHolding
? 'Release to send'
: talk.isActive
? 'Sending…'
: 'Hold to talk';
return (
<Screen title="Hold to talk" color="#14213d" background="#f2f5ff">
<FlatList
inverted
contentContainerStyle={styles.list}
data={messages}
keyExtractor={({ id }) => String(id)}
renderItem={({ item }) => (
<View style={[styles.bubble, item.isMine ? styles.mine : styles.theirs]}>
<Text style={item.isMine ? styles.mineText : styles.theirsText}>{item.text}</Text>
</View>
)}
/>
<ErrorNote error={talk.error} />
{talk.isActive ? (
<View style={styles.preview}>
<Waveform color={talk.isCancelling ? '#d6503f' : BLUE} height={24} />
<Text numberOfLines={3} style={[styles.previewText, talk.isCancelling && styles.cancelling]}>
{talk.transcript || '‹ Slide left to cancel'}
</Text>
</View>
) : (
<Text accessibilityLiveRegion="polite" style={styles.hint}>
{hint}
</Text>
)}
{/* The hook brings the gesture, this view the looks. */}
<View
{...talk.holdProps}
accessibilityLabel={talk.isHolding ? 'Send message' : 'Record message'}
style={[
styles.button,
NO_SCROLL,
talk.isHolding && styles.pressed,
talk.isCancelling && styles.buttonCancel,
]}
>
<Ionicons name={talk.isCancelling ? 'trash' : 'mic'} size={24} color="#fff" />
<Text selectable={false} style={styles.buttonText}>
{label}
</Text>
</View>
</Screen>
);
}
const styles = StyleSheet.create({
list: { padding: 16, gap: 8 },
bubble: { maxWidth: '80%', paddingHorizontal: 16, paddingVertical: 11, borderRadius: 20 },
mine: { alignSelf: 'flex-end', backgroundColor: BLUE, borderBottomRightRadius: 6 },
theirs: { alignSelf: 'flex-start', backgroundColor: '#fff', borderBottomLeftRadius: 6 },
mineText: { color: '#fff', fontSize: 16, lineHeight: 22 },
theirsText: { color: '#14213d', fontSize: 16, lineHeight: 22 },
preview: {
flexDirection: 'row',
alignItems: 'center',
gap: 14,
marginHorizontal: 16,
padding: 16,
borderRadius: 20,
backgroundColor: '#fff',
},
previewText: { flex: 1, color: '#14213d', fontSize: 16 },
hint: { minHeight: 22, paddingHorizontal: 16, textAlign: 'center', color: '#4a5578', fontSize: 15 },
cancelling: { color: '#b53a2b', fontWeight: '700' },
button: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'center',
gap: 10,
margin: 16,
height: 64,
borderRadius: 32,
backgroundColor: BLUE,
userSelect: 'none',
},
pressed: { backgroundColor: '#1c3fb8' },
buttonCancel: { backgroundColor: '#c8412b' },
buttonText: { color: '#fff', fontSize: 17, fontWeight: '700', userSelect: 'none' },
});