Android - 关于TTS和其他一些东西

时间:2017-01-19 10:43:42

标签: java android text-to-speech

我实际上无法定义问题,这就是问题就是这样的原因。

作为一个团队,我们正在制作一个使用网站RSS的新闻应用程序,我的工作是在项目中添加“文本到语音”选项,以便只读取新闻标题。代码如下。我们正在使用TabActivity。

问题是我无法获取精确的数据。例如,当您处于“每日”标签时,它会开始读取“体育”标签,这是之后的标签,其他一些标签会在此之前开始阅读标签等。

MainActivity

    public class MainActivity extends AppCompatActivity implements NewsResultListener {

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private Speaker speakerManager;
private ArrayList<News> titleListStable = new ArrayList<>();
private static ArrayList<News> titleList = new ArrayList<>();
private static ArrayList<ArrayList<News>> newsList = new ArrayList<>();
private int selectedPage;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    speakerManager = new Speaker(this);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            titleListStable = titleList;
            Toast.makeText(getApplicationContext(), "" + position , Toast.LENGTH_LONG).show();
            selectedPage = position;
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);
}

@Override
protected void onDestroy() {
    super.onDestroy();

    if(speakerManager != null)
    {
        speakerManager.shutdown();
    }
}

@Override
public void onSuccess(ArrayList<News> news) {

}

@Override
public void onFail() {

}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment implements NewsResultListener {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number";

    private RecyclerView rvNews;
    private RecyclerView.Adapter adapter;
    private RecyclerView.LayoutManager mLayoutManager;
    private ArrayList<News> dataset;

    public PlaceholderFragment() {
    }

    /**
     * Returns a new instance of this fragment for the given section
     * number.
     */
    public static PlaceholderFragment newInstance(int sectionNumber) {
        PlaceholderFragment fragment = new PlaceholderFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

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

        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        rvNews = (RecyclerView) rootView.findViewById(R.id.rvNews);
        mLayoutManager = new LinearLayoutManager(getContext());
        rvNews.setLayoutManager(mLayoutManager);
        dataset = new ArrayList<>();
        adapter = new NewsAdapter(dataset, getActivity());
        rvNews.setAdapter(adapter);

        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 0:
                new RssFeedParser(this).execute("guncel");
                break;
            case 1:
                new RssFeedParser(this).execute("spor");
                break;
            case 2:
                new RssFeedParser(this).execute("ekonomi");
                break;
            case 3:
                new RssFeedParser(this).execute("turkiye");
                break;
            case 4:
                new RssFeedParser(this).execute("dunya");
                break;
            case 5:
                new RssFeedParser(this).execute("kultur-sanat");
                break;
            case 6:
                new RssFeedParser(this).execute("politika");
                break;
            case 7:
                new RssFeedParser(this).execute("bilim-teknoloji");
                break;
            case 8:
                new RssFeedParser(this).execute("yasam");
                break;
            case 9:
                new RssFeedParser(this).execute("saglik");
                break;
            case 10:
                new RssFeedParser(this).execute("analiz-haber");
                break;
            case 11:
                new RssFeedParser(this).execute("gunun-basliklari");
                break;
        }

        //TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        //textView.setText(getString(R.string.section_format, ));

        return rootView;
    }


    @Override
    public void onSuccess(ArrayList<News> news) {


        for(News item : news)
        {
            dataset.add(item);

        }
        titleList=dataset;
        newsList.add(dataset);

        adapter.notifyDataSetChanged();
    }

    @Override
    public void onFail() {

    }
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return PlaceholderFragment.newInstance(position);
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 12;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position) {
            case 0:
                return "GÜNCEL";
            case 1:
                return "SPOR";
            case 2:
                return "EKONOMİ";
            case 3:
                return "TÜRKİYE";
            case 4:
                return "DÜNYA";
            case 5:
                return "KÜLTÜR SANAT";
            case 6:
                return "POLİTİKA";
            case 7:
                return "BİLİM TEKNOLOJİ";
            case 8:
                return "YAŞAM";
            case 9:
                return "SAĞLIK";
            case 10:
                return "ANALİZ HABER";
            case 11:
                return "GÜNÜN BAŞLIKLARI";
        }
        return null;
    }
}

public void imageButton(View v){


    for(News i: titleListStable){
        String title = i.getTitle();

        speakerManager.speak(title);
    }
}
    }

演讲者班级

    public class Speaker {

private TextToSpeech mTextToSpeech;
private boolean mIsTTSEnabled = true;

public Speaker(final MainActivity act){

    mTextToSpeech = new TextToSpeech(act, new TextToSpeech.OnInitListener() {
        @Override
        public void onInit(int i) {
            if(i == TextToSpeech.SUCCESS){
                mTextToSpeech.setLanguage(new Locale("TR-tr"));


                switch (mTextToSpeech.isLanguageAvailable(new Locale("TR-tr")))
                {
                    case TextToSpeech.LANG_MISSING_DATA:
                    case TextToSpeech.LANG_NOT_SUPPORTED:
                        Toast.makeText(act, "Cihazda Türkçe konuşma sentezi desteklenmemektedir.", Toast.LENGTH_SHORT).show();
                    default:
                        break;
                }

            }else{
                mIsTTSEnabled = false;
            }
        }
    });
}

public void speak(final String announce){
    if(mIsTTSEnabled){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mTextToSpeech.speak(announce, TextToSpeech.QUEUE_ADD, null, "");
        }
        else
        {
            mTextToSpeech.speak(announce, TextToSpeech.QUEUE_FLUSH, null);
        }
    }
}

public void shutdown(){
    if(mTextToSpeech != null)
    {
        mTextToSpeech.stop();
    }
}
    }

注意:如果您想要覆盖整个代码,请点击; https://github.com/dnkilic/AnadoluAjansi

1 个答案:

答案 0 :(得分:0)

这是ViewPager的默认行为,我们可以使用setOffscreenPageLimit自定义此行为,但我们再次无法将限制设置为小于1.

  

设置应保留在页面两侧的页数   处于空闲状态的视图层次结构中的当前页面。页面超出此范围   在需要时,将从适配器重新创建限制。

     

这是作为优化提供的。如果您事先知道这个号码   您将需要支持或具有延迟加载机制的页面   放在你的页面上,调整这个设置可以带来好处   感知到的分页动画和交互的平滑性。如果你有   少量页面(3-4),您可以一次保持活动状态,   在新创建的视图子树的布局中花费的时间会减少   用户来回翻页。

     

您应该保持此限制,特别是如果您的网页很复杂   布局。此设置默认为1.

如果你试图设置小于1的限制(例如 mViewPager.setOffscreenPageLimit(0); ),你应该在LogCat中收到关于此的警告,例如:

Requested offscreen page limit 0 too small; defaulting to 1

检查thisthis SO答案以获取更多信息。

希望这对你有所帮助。