0

以下のコードは左から右へのスワイプで機能しています。右から左へのスワイプもthe同じように設定したいと思います。スワイプは機能していますが、スピーチアウトが位置でクラッシュしています。良いだけですが、Texttoスピーチは正しく機能していません

以下はコードです:

  public Object instantiateItem(ViewGroup container, int position) {
                ImageView image=new ImageView(context);
               image.setImageResource(fruits[position]);
              ((ViewPager) container).addView(image);
              if(position>=1){
                  image.setImageResource(fruits[position]);
                  speakOut(names[position-1]);
              }
              return image;
            }

ここでは機能していません。誰かが私に提案をして、この問題から私を連れ出すことができますか

   public class Learn extends Activity implements TextToSpeech.OnInitListener{
        private TextToSpeech tts;
        private ViewPager pic;
        private Button next,back;
        private TextView text;
        private Context context;
        private int i=0;
        public Integer[] fruits={R.drawable.apple1,R.drawable.banana1,R.drawable.grapes1,R.drawable.cherry1,R.drawable.lemon1,
                R.drawable.orange1,R.drawable.strwberry1,R.drawable.water1};
        public String [] names={"Apple","Banana","Grapes","Cherry","Lemon","Orange","Strawberry","Water melon"};

        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.learn);
            tts = new TextToSpeech(this, this);
            pic=(ViewPager)findViewById(R.id.view_pager);
            next=(Button)findViewById(R.id.button1);
            back=(Button)findViewById(R.id.button2);
            text=(TextView)findViewById(R.id.text);
            ImagePageAdapter adapterpic=new ImagePageAdapter(this);
            pic.setAdapter(adapterpic);

        }
        private class ImagePageAdapter extends PagerAdapter {

            public ImagePageAdapter(Context learn) {
                // TODO Auto-generated constructor stub
                context=learn;
            }

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return names.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
              return view == ((ImageView) object);
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                ImageView image=new ImageView(context);
               image.setImageResource(fruits[position]);
              ((ViewPager) container).addView(image);
              if(position>=1){
                  image.setImageResource(fruits[position]);
                  speakOut(names[position-1]);
              }
              return image;
            }
            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
              ((ViewPager) container).removeView((ImageView) object);
            }

        }
        private void speakOut(String text) {
            // TODO Auto-generated method stub
             tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
        }
        @Override
        public void onDestroy() {
            // Don't forget to shutdown tts!
            if (tts != null) {
                tts.stop();
                tts.shutdown();
            }
            super.onDestroy();
        }

        @Override
        public void onInit(int status) {

           if (status == TextToSpeech.SUCCESS) {

                int result = tts.setLanguage(Locale.US);

                if (result == TextToSpeech.LANG_MISSING_DATA
                        || result == TextToSpeech.LANG_NOT_SUPPORTED) {
                    Log.e("TTS", "This Language is not supported");
                } else {
                    pic.setEnabled(true);

                    //speakOut("apple");
                }

            } else {
                Log.e("TTS", "Initilization Failed!");
            }

        }
    }
    Here is stack trace

02-27 16:39:00.994: I/TTS received:(2369): Apple
02-27 16:39:05.144: I/TTS received:(2369): Banana
02-27 16:39:06.534: I/TTS received:(2369): Grapes
02-27 16:39:24.273: I/TTS received:(2369): Banana
02-27 16:39:25.355: I/TTS received:(2369): Grapes
02-27 16:39:26.593: I/TTS received:(2369): Cherry
02-27 16:39:28.624: I/TTS received:(2369): Apple

この問題から抜け出すための提案をください

4

1 に答える 1

0

テキストを音声に変換する代わりに、オブジェクトの音を入れてみませんか..このコードを試すことができます

public class Learn extends Activity {

    MediaPlayer mp;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_numbers);
        ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
        ImagePagerAdapter adapter = new ImagePagerAdapter();
        viewPager.setAdapter(adapter);
     }

private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9,R.drawable.no10,R.drawable.bg2};

private int[] mAudio = new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five,R.raw.six,R.raw.seven,R.raw.eight,R.raw.nine,R.raw.ten,R.raw.one};

         @Override
         public int getCount() {
         return mImages.length;
         }

         @Override
         public boolean isViewFromObject(View view, Object object) {
         return view == ((ImageView) object);
         }

         @Override
         public Object instantiateItem(ViewGroup container, int position) {
            Context context = Numbers.this;
            ImageView imageView = new ImageView(context);
            int padding =context.getResources().  
            getDimensionPixelSize(R.dimen.activity_vertical_margin);
            imageView.setPadding(padding, padding, padding, padding);
            imageView.setScaleType(ImageView.ScaleType.CENTER);
            imageView.setImageResource(mImages[position]);
            ((ViewPager) container).addView(imageView, 0); 
            if(position>=1){
            mp = MediaPlayer.create(Numbers.this, mAudio[position-1]);
            mp.start();
            }
            return imageView;
         }

          @Override
         public void destroyItem(ViewGroup container, int position, Object object) {
           ((ViewPager) container).removeView((ImageView) object);
         }
     }
     }

ここの値は、要件に応じて変更できます。

于 2014-01-18T09:36:37.060 に答える