2023-09-05

import React, {useState, useRef}  from 'react';
import { View, StyleSheet, Text, TouchableOpacity, Dimensions, ScrollView } from 'react-native';
import * as Speech from 'expo-speech';
const windowWidth = Dimensions.get('window').width;
const sliderLeft = 36+16+50;
const sliderWidth = windowWidth - 2*sliderLeft+50;
const initRateValue = 2;
const initPitchValue = 1.0;
export default function App() {
  const [voiceList, setVoiceList] = useState([]);
  const [voiceId, setVoiceId] = useState("");
  const rate = useRef(initRateValue);
  const pitch = useRef(initPitchValue);
  Speech.getAvailableVoicesAsync().then(r => {
    setVoiceList(([r.filter(i => i.language == "en-US"), r.filter(i => i.language == "en-GB")]) || []);
    setVoiceId(r.filter(i => i.language == "en-US")[0].identifier);
  })
  const speak = () => {
    const thingToSay = '1';
    Speech.speak(thingToSay);
  };

  const selectVoice = (identifier) => {
    setVoiceId(identifier);
  }

  const settingAction = () => {
    
  }

  return (
    <View style={styles.container}>
      <Slider style={{marginHorizontal: 16, marginTop: 24}}
        name="速度:" 
        value={initRateValue} 
        valueChanged={(i) => {rate.current = i}}/>
      <Slider style={{marginHorizontal: 16, marginTop: 20}} 
        name="音調(diào):"
        value={initPitchValue} 
        valueChanged={(i) => {pitch.current = i} }/>
      
      <ScrollView style={{flex: 1, backgroundColor: 'red', marginTop: 10}}>
        {voiceList.map((list, i) =>
          <View style={{backgroundColor: '#f5f6f7'}}>
            <Text style={{lineHeight: 40, marginLeft: 16}} key={i}>{i == 0 ? "美音": "英音"}</Text>
            {list.map((voice, index) => 
              <TouchableOpacity onPress={() => selectVoice(voice.identifier)} activeOpacity={0.6} style={{backgroundColor: '#fff'}} key={`${i}_${index}`}>
                <View style={{ flexDirection: 'row', alignItems: 'center', marginHorizontal: 16}}>
                  <Text style={{lineHeight: 54, fontSize: 16}}>{voice.name}</Text>
                  <View style={{flex: 1}}/>
                  <Text style={{fontSize: 14, color: '#67676a'}}>{voice.quality == "Default" ? "標準音質(zhì)": "高級音質(zhì)"}</Text>
                  <View style={{width: 10, height: 10, borderRadius: 5, backgroundColor: voiceId === voice.identifier ? 'red': '#eee', marginLeft: 15 }}/>
                </View>
                <View style={{height: 1, backgroundColor: '#ececef'}}/>
              </TouchableOpacity>)}
          </View>
        )}
      </ScrollView>
    </View>
  );
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ffffff',
  },
});

const Slider = ({style, name, value=1.1, valueChanged}) => {
  const [_value, setValue] = useState(value);
  const onPress = (event) => {
    var v = ((event.nativeEvent.pageX - sliderLeft) / sliderWidth * 1.5 + 0.5).toFixed(1);
    v = Math.max(0.5, Math.min(v, 2.0));
    setValue(v);
    valueChanged(0.8);
  }
  return <View style={style}>
    <View style={{height: 40, flexDirection: 'row', alignItems: 'center'}}>
    <Text style={{lineHeight: 40, width: 50}}>{name}</Text>
      <Text style={{lineHeight: 40, width: 31}}>0.5</Text>
      <TouchableOpacity onPress={(e) =>onPress(e)} activeOpacity={1} style={{flex: 1, height: 40, flexDirection: 'row', paddingHorizontal: 5}}>
        <View style={{position: "absolute", left:5, right: 15, top: 19.5, height: 1, backgroundColor: '#e1e2e4'}}/>
        <View style={{flex:(_value-0.5)/(1.5)}}/>
        <View style={{ paddingVertical: 5, width: 30, height: 40, marginLeft: -15}}>
          <View style={{ backgroundColor: 'white', alignItems: 'center', width: 30, height: 30,
            shadowColor: "#000", shadowOpacity: 0.13, shadowRadius: 8, borderRadius: 15, elevation: 5}}>
            <Text style={{color: "#6a6a67", fontSize: 11, fontWeight: 700, lineHeight: 30}}>{_value}</Text>
          </View>
        </View>
      </TouchableOpacity>
      <Text style={{lineHeight: 40, width: 21, textAlign: 'end'}}>2.0</Text>
    </View>
  </View>
}
class AudioArticlePlay {
    const addListener = ({ onNext, onSpeakingChanged, didFinished }) => {
        this._didFinished = didFinished;
        this._onNext = onNext;
        this._onSpeakingChanged = onSpeakingChanged;
    }
    const removeListener = () => {
        this._didFinished = null;
        this._onNext = null;
        this._onSpeakingChanged = null;
    }
    
    set isSpeaking(value) {
        this._isSpeaking = value;
        if (this._onSpeakingChanged) {
            this._onSpeakingChanged(value);
        }
    }
    get isSpeaking() {
        return this._isSpeaking;
    }
    get location() {
        return this._location;
    }
    
    const releasDatas = () => {
        this.removeListener();
        this._article = null;
        this._title = null;
        this._id = null;
        this._sentences = null;
        this._location = null;
        this._isSpeaking = false;
    }
    
    const setNewDatas = ({article, title, id}) => {
        this._article = article;
        this._title = title;
        this._id = id;
        this._sentences = getSentences(article);
        this._location = {section: -1, item: 0};
    }
    
    const playSentence = () => {
        const location = this._location;
        var sentence = "";
        if (location.section == -1) {
            sentence = this._title;
        } else {
            sentence = this._sentences[location.section][location.item];
        }
        Speech.speak(sentence, {
            voice: this._voice.identifier,
            language: this._voice.language || "en_US",
            rate: (this._voice.rate > 0.1) ? this._voice.rate: 1.0,
            pitch: (this._voice.pitch> 0.1) ? this._voice.pitch: 1.0,
            onDone: () => {
                let next = this.getNextLocation();
                if (next != null) {
                    if (this._id) {
                        this._location = next;
                        this.playSentence();
                    } else {
                        this.isSpeaking = false;
                    }
                    if (this._onNext) {
                        this._onNext(next);
                    }
                } else {
                    if (this._didFinished) {
                        this._didFinished();
                    }
                    this.releasDatas();
                }
            },
            onError: () => {
                this.isSpeaking = false;
            },
        })
    }
    const newPlaySentence = async (l) => {
        try {
            const location = l || this._location;
            this._location = location;
            this._voice = JSON.parse(await AsyncStorage.getItem('Speech_Setting'));
            await Speech.stop();
            if (this._onNext) {
                this._onNext(location);
            }
            this.playSentence();
            this.isSpeaking = true;
        } catch (e) {
            this.isSpeaking = false;
        }
    }
    
    const stop = async () => {
        await Speech.stop();
        this.isSpeaking = false;
    }
    
    const getNextLocation = () => {
        return null;
    }
}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,997評論 6 502
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件氛堕,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機汪诉,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,603評論 3 392
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來谈秫,“玉大人扒寄,你說我怎么就攤上這事∧馓蹋” “怎么了该编?”我有些...
    開封第一講書人閱讀 163,359評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長硕淑。 經(jīng)常有香客問我课竣,道長,這世上最難降的妖魔是什么置媳? 我笑而不...
    開封第一講書人閱讀 58,309評論 1 292
  • 正文 為了忘掉前任于樟,我火速辦了婚禮,結(jié)果婚禮上半开,老公的妹妹穿的比我還像新娘隔披。我一直安慰自己,他們只是感情好寂拆,可當我...
    茶點故事閱讀 67,346評論 6 390
  • 文/花漫 我一把揭開白布奢米。 她就那樣靜靜地躺著,像睡著了一般纠永。 火紅的嫁衣襯著肌膚如雪鬓长。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,258評論 1 300
  • 那天尝江,我揣著相機與錄音涉波,去河邊找鬼。 笑死炭序,一個胖子當著我的面吹牛啤覆,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播惭聂,決...
    沈念sama閱讀 40,122評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼窗声,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了辜纲?” 一聲冷哼從身側(cè)響起笨觅,我...
    開封第一講書人閱讀 38,970評論 0 275
  • 序言:老撾萬榮一對情侶失蹤拦耐,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后见剩,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體杀糯,經(jīng)...
    沈念sama閱讀 45,403評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,596評論 3 334
  • 正文 我和宋清朗相戀三年苍苞,在試婚紗的時候發(fā)現(xiàn)自己被綠了固翰。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,769評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡柒啤,死狀恐怖倦挂,靈堂內(nèi)的尸體忽然破棺而出畸颅,到底是詐尸還是另有隱情担巩,我是刑警寧澤,帶...
    沈念sama閱讀 35,464評論 5 344
  • 正文 年R本政府宣布没炒,位于F島的核電站涛癌,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏送火。R本人自食惡果不足惜拳话,卻給世界環(huán)境...
    茶點故事閱讀 41,075評論 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望种吸。 院中可真熱鬧弃衍,春花似錦、人聲如沸坚俗。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,705評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽猖败。三九已至速缆,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間恩闻,已是汗流浹背艺糜。 一陣腳步聲響...
    開封第一講書人閱讀 32,848評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留幢尚,地道東北人破停。 一個月前我還...
    沈念sama閱讀 47,831評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像尉剩,于是被迫代替她去往敵國和親真慢。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,678評論 2 354

推薦閱讀更多精彩內(nèi)容

  • 今天看了一個大佬的分享边涕,了解了如何判斷一個崗位是否適合晤碘,做了總結(jié)褂微,給別人做了分享主要是要直接找到這個崗位人去問下面...
    探索踐行者閱讀 30評論 0 0
  • 兩性篇章:雙向奔赴才能贏 在人生的旅途中,愛情是最美好的一段路程园爷。在愛情中宠蚂,雙方都需要付出真心、共同經(jīng)營童社,才能讓愛...
    森林走心篇章閱讀 51評論 0 0
  • 主題教育風正勁 乘勢而上開新局 當前求厕,全黨正深入開展學習貫徹習近平新時代中國特色社會主...
    wen11閱讀 50評論 0 0
  • 孩子不帶手機,家長群里炸鍋了 開學第一周扰楼,學校不讓帶手機呀癣,如果特殊情況需要,提前打個申請弦赖,家長簽字项栏,但手機帶到學校...
    花阡陌2022閱讀 81評論 0 0
  • 生活好似又重新步入了一種方式。比之前慢了下來蹬竖。不是說事物變少了沼沈,而是心態(tài)上多一些從容不迫了。這樣的狀態(tài)是好的币厕。 昨...
    浪矢雜貨店閱讀 279評論 2 10