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

Ghi chú bằng giọng nói

Đọc chính tả không sợ ngắt quãng: nghe liên tục cho tới khi bạn dừng.

Demo chạy thật, cần micro.Mở toàn màn hình ↗
  1. continuous: true tự bắt đầu câu kế tiếp, nên dừng lại suy nghĩ không làm kết thúc ghi chú.
  2. finalTranscript không đổi nữa, interimTranscript thì còn có thể đổi nên được vẽ nhạt hơn.
  3. Nút micro chỉ cần toggle(). Lần đầu nó cũng tự xin quyền.
  4. Sóng âm đọc useSpeechVolume() trong component riêng, nên chỉ các cột render 15 lần mỗi giây.

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/notes.tsx
import { Ionicons } from '@expo/vector-icons';
import { useSpeechRecognition } from 'expo-stt';
import { useState } from 'react';
import { Pressable, ScrollView, StyleSheet, Text, View } from 'react-native';
import { ErrorNote, MicButton, Screen, useDemo, Waveform } from './ui';
const INK = '#3b2a1a';
const PAPER = '#fbf4e4';
const RED = '#c8412b';
/** Dictation: keeps listening through pauses, until you stop it. */
export function Notes() {
const { locale } = useDemo();
const [notes, setNotes] = useState<string[]>([]);
const speech = useSpeechRecognition({ locale, continuous: true });
const save = () => {
setNotes([speech.transcript, ...notes]);
speech.reset();
};
return (
<Screen title="Voice notes" color={INK} background={PAPER}>
<View style={styles.page}>
<ScrollView contentContainerStyle={styles.draft}>
{speech.transcript ? (
<Text style={styles.text}>
{speech.finalTranscript}
{/* What may still change is lighter. */}
<Text style={styles.interim}> {speech.interimTranscript}</Text>
</Text>
) : (
<Text style={styles.hint}>
{speech.isActive ? 'Go ahead, take your time…' : 'Tap the microphone and think out loud.'}
</Text>
)}
</ScrollView>
{speech.transcript !== '' && !speech.isActive && (
<View style={styles.actions}>
<Pressable accessibilityRole="button" onPress={speech.reset} style={styles.action}>
<Text style={styles.actionText}>Discard</Text>
</Pressable>
<Pressable accessibilityRole="button" onPress={save} style={[styles.action, styles.primary]}>
<Text style={[styles.actionText, styles.primaryText]}>Save note</Text>
</Pressable>
</View>
)}
</View>
<ErrorNote error={speech.error} />
<ScrollView style={styles.list} contentContainerStyle={styles.listContent}>
{notes.map((note, index) => (
<View key={notes.length - index} style={styles.note}>
<Ionicons name="document-text-outline" size={18} color={INK} />
<Text numberOfLines={2} style={styles.noteText}>
{note}
</Text>
</View>
))}
</ScrollView>
<View style={styles.dock}>
<View style={styles.wave}>{speech.isActive && <Waveform color={RED} />}</View>
<MicButton
isActive={speech.isActive}
color="#fff"
background={RED}
onPress={() => void speech.toggle()}
/>
</View>
</Screen>
);
}
const styles = StyleSheet.create({
page: {
flex: 3,
marginHorizontal: 16,
borderRadius: 20,
backgroundColor: '#fffdf7',
borderWidth: 1,
borderColor: '#eadfc4',
},
draft: { padding: 20 },
text: { color: INK, fontSize: 22, lineHeight: 34 },
interim: { color: '#b09a78' },
hint: { color: '#8a744f', fontSize: 18, lineHeight: 28 },
actions: { flexDirection: 'row', gap: 10, padding: 14 },
action: { flex: 1, padding: 13, borderRadius: 12, alignItems: 'center', backgroundColor: '#f3e8cf' },
actionText: { color: INK, fontWeight: '700' },
primary: { backgroundColor: INK },
primaryText: { color: PAPER },
list: { flex: 1.2 },
listContent: { padding: 16, gap: 8 },
note: { flexDirection: 'row', gap: 10, padding: 14, borderRadius: 14, backgroundColor: '#f3e8cf' },
noteText: { flex: 1, color: INK, fontSize: 15, lineHeight: 21 },
dock: { alignItems: 'center', gap: 22, paddingBottom: 28 },
wave: { height: 36 },
});