我已经研究了一段时间并尝试过很多东西,但是,我还没有弄清楚这个错误,为了帮助我将我的代码包含在内,它理论上应该起作用,因为它是一个反应的例子-native-android-speech https://github.com/mihirsoni/react-native-android-speech我在尝试中覆盖了大多数方法,我不确定是不是因为它是在不久前编写的,并且与较新版本的反应存在一些兼容性问题。
main activity.java 包com.zero;
import android.os.Bundle;
import com.facebook.react.LifecycleState;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactRootView;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.shell.MainReactPackage;
import com.mihir.react.tts.*;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ReactActivity implements DefaultHardwareBackBtnHandler {
@Override
protected String getMainComponentName() {
String stuff = this.getComponentName().toString();
return stuff;
}
@Override
public boolean getUseDeveloperSupport(){
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage());
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ReactRootView mReactRootView = new ReactRootView(this);
ReactInstanceManager mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new RCTTextToSpeechModule()) // Add any extra modules here
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
mReactRootView.startReactApplication(mReactInstanceManager, "zero", null);
setContentView(mReactRootView);
}
}
index.android.js
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View
} from 'react-native';
var tts = require('react-native-android-speech');
tts.speak({
text:'Please provide some text to speak.', // Mandatory
pitch:1.5, // Optional Parameter to set the pitch of Speech,
forceStop : false , // Optional Parameter if true , it will stop TTS if it is already in process
language : 'en' // Optional Paramenter Default is en you can provide any supported lang by TTS
}).then(isSpeaking=>{
//Success Callback
console.log(isSpeaking);
}).catch(error=>{
//Errror Callback
console.log(error)
});
// class zero extends Component {
// render() {
// return (
// <View style={styles.container}>
// <Text style={styles.welcome}>
// Welcome to React Native!
// </Text>
// <Text style={styles.instructions}>
// To get started, edit index.android.js
// </Text>
// <Text style={styles.instructions}>
// Shake or press menu button for dev menu
// </Text>
// </View>
// );
// }
// }
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});
AppRegistry.registerComponent('zero', () => zero);
提前感谢您的帮助。
答案 0 :(得分:0)
当您使用最新版本的react时,我认为您不应该覆盖onCreate,而是尝试在packages数组中添加插件
import android.os.Bundle;
import com.facebook.react.LifecycleState;
import com.facebook.react.ReactActivity;
import com.facebook.react.ReactInstanceManager;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactRootView;
import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
import com.facebook.react.shell.MainReactPackage;
import com.mihir.react.tts.*;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends ReactActivity implements DefaultHardwareBackBtnHandler {
@Override
protected String getMainComponentName() {
return "zero";
}
@Override
public boolean getUseDeveloperSupport(){
return BuildConfig.DEBUG;
}
@Override
protected List<ReactPackage> getPackages() {
return Arrays.<ReactPackage>asList(
new MainReactPackage(),
new RCTTextToSpeechModule());
}
}