TextToSpeech没有处理片段

时间:2016-08-30 20:01:33

标签: android android-fragments google-text-to-speech

我对Android编程很新!我设法让TTS工作但不是片段。我试图刷一些图像,并在每次这样做时说些什么。我没有收到任何错误,但是没有说出文字。这是代码:

package com.example.nightrain.sliderTTS1;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.Locale;

public class Wild extends FragmentActivity{

    ImageFragmentPagerAdapter imageFragmentPagerAdapter;
    ViewPager viewPager;
    public static final String[] IMAGE_NAME = {
            "1", "2", "3"};
    static final int NUM_ITEMS = IMAGE_NAME.length;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_pager);

        imageFragmentPagerAdapter = new ImageFragmentPagerAdapter(getSupportFragmentManager());
        viewPager = (ViewPager) findViewById(R.id.pager);
        viewPager.setAdapter(imageFragmentPagerAdapter);
    }

    public static class ImageFragmentPagerAdapter extends FragmentPagerAdapter {
        public ImageFragmentPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return NUM_ITEMS;
        }

        @Override
        public Fragment getItem(int position) {
            SwipeFragment fragment = new SwipeFragment();
            return fragment.newInstance(position);
        }
    }

    public static class SwipeFragment extends Fragment implements TextToSpeech.OnInitListener {

        private static TextToSpeech tts;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            View swipeView = inflater.inflate(R.layout.swipe_fragment, container, false);
            ImageView imageView = (ImageView) swipeView.findViewById(R.id.imageView);
            Bundle bundle = getArguments();
            int position = bundle.getInt("position");
            String imageFileName = IMAGE_NAME[position];
            int imgResId = getResources().getIdentifier(imageFileName, "drawable", "com.example.nightrain.sliderTTS1");
            imageView.setImageResource(imgResId);

            tts = new TextToSpeech( getActivity(), SwipeFragment.this );
            speak("help");

            return swipeView;
        }

        static SwipeFragment newInstance(int position) {
            SwipeFragment swipeFragment = new SwipeFragment();
            Bundle bundle = new Bundle();
            bundle.putInt("position", position);
            swipeFragment.setArguments(bundle);
            return swipeFragment;
        }

        public void onInit(int status) {
            Log.d("Speech", "OnInit - Status ["+status+"]");

            if (status == TextToSpeech.SUCCESS) {
                Log.d("Speech", "Success!");
                tts.setLanguage(Locale.US);
            }
        }

        @Override
        public void onDestroy() {
            if (tts != null) {
                tts.stop();
                tts.shutdown();
            }
            super.onDestroy();
        }


        public void speak( String text){
            tts.speak( text, TextToSpeech.QUEUE_FLUSH, null, "1" );
        }
    }
}

1 个答案:

答案 0 :(得分:0)

想出来。我试图在 onInit()之前使用 speak()。有趣的是,我可以在 onInit()之外使用speak而不会出现活动问题,但是,对于片段我需要模式化 speak()方法和所有必要的代码的OnInit()的。由于它是系统调用的,我无法强迫它在它之外工作。