2016-08-30 1 views
1

저는 Android 프로그래밍에 새로운 경험이 있습니다! 나는 TTS 작업을 할 수 있었지만 파편에는 사용할 수 없었다. 나는 이미지를 스 와이프하려고 할 때마다 그럴 때마다 무언가를 말합니다. 그러나 나는 어떤 오류도 나타나지 않는다. 그러나 본문은 말하지 않는다. 코드는 다음과 같습니다.TextToSpeech가 조각에서 작동하지 않습니다.

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"); 
     } 
    } 
} 

답변

0

알아 냈습니다. 나는 전에 앞에 말하십시오()을 사용하려고했습니다. 외부에서 말하기를 사용할 수 있다는 것은 흥미 롭습니다. 그러나 단편으로는 speak() 메서드와 필요한 모든 코드를내부에서 실행해야했습니다 (). 그것이 호출 된 시스템이기 때문에 강제로 외부에서 작동하도록 할 수는 없습니다.

+0

음성 엔진이 초기화 될 때까지'speak() '를 사용할 수 없습니다. 코드에서 다른 곳에서 작동하는 경우 경쟁 조건에서 이기기 만하면됩니다. 매번 발생하지는 않습니다. – brandall