Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a fragment a spinner, a textview and 3 buttons.
I want to be able to reproduce the text in audio on button click, from my fragment, and even though I got rid of all the errors, I can't hear nything, when I click the buttons that should reproduce the text in audio.
I also want to be able to change the language in the tts, and I was able to do this, but on an activity, and in the fragment, its not working.
Any help will be greatly appreciated.
Thanks.

What I have tried:

<pre>public class fragment2 extends Fragment implements View.OnClickListener,AdapterView.OnItemSelectedListener, TextToSpeech.OnInitListener {
    private Spinner spinner;
TextView txt1,txt2;
Button r1,b1,s1;
private int id;
    ArrayList<String> lat;
    ArrayList<String> lon;
    private static final int MY_DATA_CHECK_CODE = 1;
    private static final int TTS_DATA_CHECK = 1;
    static final String TAG = "TTS";
    double latitude;
    double longitude;
    private TextToSpeech mTts;
    SharedPreferences.Editor editor;

SharedPreferences preferences;

    public fragment2() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_fragment2, container, false);//Inflate Layout

        Intent checkIntent=new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent,MY_DATA_CHECK_CODE);
        getActivity().setVolumeControlStream(AudioManager.STREAM_MUSIC);
        mTts= new TextToSpeech(getActivity(), fragment2.this);
        //mTts= new TextToSpeech(getActivity(), (TextToSpeech.OnInitListener) fragment2.this);

        preferences = this.getActivity().getSharedPreferences("pref", Context.MODE_PRIVATE);
        spinner=(Spinner)view.findViewById(R.id.spin);
        txt2=(TextView) view.findViewById(R.id.txt);
        txt1=(TextView) getActivity().findViewById(R.id.txt1);

        //Toast.makeText(getContext(), txt1.getText().toString(),Toast.LENGTH_SHORT).show();
        switch(Integer.valueOf(txt1.getText().toString())){
            case R.id.part1:
                Toast.makeText(getContext(), "part1",Toast.LENGTH_SHORT).show();
                break;
            case R.id.part2:
                Toast.makeText(getContext(), "part2",Toast.LENGTH_SHORT).show();
                break;
        }
        view.findViewById(R.id.rb).setOnClickListener(mListener);
        view.findViewById(R.id.fb).setOnClickListener(mListener);
        view.findViewById(R.id.sb).setOnClickListener(mListener);
        spinner.setSelection(preferences.getInt("lang", 0));

        // check if GPS enabled
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                Context context;
                Resources resources;
                String languageToLoad = null;
                    switch (i) {
                        case 0:
                            context = LocaleHelper.setLocale(getActivity(), "es");
                            editor.putInt("lang", 0);
                            editor.commit();
                            //prefs.setString("lang2","\"spa\", \"MEX\"");
                            mTts.setLanguage(new Locale("spa", "MEX"));
                            resources = context.getResources();
                            txt1.setText(txt1.getText().toString());
                            //txt2.setText(resources.getString(R.string.txt2));
                            languageToLoad = "es";
                        /*if(languageToLoad!=null) {
                            Locale locale = new Locale(languageToLoad);
                            Locale.setDefault(locale);
                            Configuration config = new Configuration();
                            config.locale = locale;
                            getBaseContext().getResources().updateConfiguration(config,
                                    getBaseContext().getResources().getDisplayMetrics());
                        }*/
                            break;
                        case 1:
                            context = LocaleHelper.setLocale(getActivity(), "en");
                            editor.putInt("lang", 1);
                            editor.commit();
                            //prefs.setString(";ang2","Locale.US");
                            mTts.setLanguage(Locale.US);
                            resources = context.getResources();
                            //Aca
                            txt1.setText(resources.getString(R.string.txt1));
                            txt2.setText(resources.getString(R.string.txt2));
                        /*languageToLoad = "en";
                        if(languageToLoad!=null) {
                            Locale locale = new Locale(languageToLoad);
                            Locale.setDefault(locale);
                            Configuration config = new Configuration();
                            config.locale = locale;
                            getBaseContext().getResources().updateConfiguration(config,
                                    getBaseContext().getResources().getDisplayMetrics());
                        }*/
                            break;
                }
            }

            public void setLanguage(String l) {
                Intent intent = getActivity().getIntent();
                getActivity().overridePendingTransition(0, 0);
                intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                getActivity().finish();
                getActivity().overridePendingTransition(0, 0);
                startActivity(intent);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });
        return view;
    }

    void speak(String s) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Log.v(TAG, "Speak new API");
            Bundle bundle = new Bundle();
            bundle.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_MUSIC);
            mTts.speak(s, TextToSpeech.QUEUE_FLUSH, bundle, null);
        } else {
            Log.v(TAG, "Speak old API");
            HashMap<String, String> param = new HashMap<>();
            param.put(TextToSpeech.Engine.KEY_PARAM_STREAM, String.valueOf(AudioManager.STREAM_MUSIC));
            mTts.speak(s, TextToSpeech.QUEUE_FLUSH, param);
        }
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        // Don't forget to shutdown tts!
        if (mTts != null) {
            Log.v(TAG, "onDestroy: shutdown TTS");
            mTts.stop();
            mTts.shutdown();
        }
    }
    private final View.OnClickListener mListener = new View.OnClickListener() {
        public void onClick(View view) {
            switch (view.getId()) {
                case R.id.rb:
                    // do stuff
                    break;
                case R.id.fb:
                    // do stuff
                    break;
                case R.id.sb:
                    speak(txt1.getText().toString());
                    // do stuff
                    break;
            }
        }
    };

    @Override
    public void onClick(View v) {

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    @Override
    public void onInit(int status) {
        if(preferences.getInt("lang",0)==0){
            Locale locSpanish = new Locale("spa", "MEX");
            mTts.setLanguage(locSpanish);
        }else if(preferences.getInt("lang",0)==1){
            mTts.setLanguage(Locale.US);
        }
    }
}
Posted
Comments
David Crow 11-Sep-18 12:23pm    
"...I can't hear nything..."

Is the volume loud enough? Using the debugger, have you stepped into the speak() method to see what is happening and if the variables are as expected?

"...its not working."

What exactly does this mean?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900