<tfoot id='d8dUg'></tfoot>
  1. <legend id='d8dUg'><style id='d8dUg'><dir id='d8dUg'><q id='d8dUg'></q></dir></style></legend>
      <bdo id='d8dUg'></bdo><ul id='d8dUg'></ul>

    <small id='d8dUg'></small><noframes id='d8dUg'>

    1. <i id='d8dUg'><tr id='d8dUg'><dt id='d8dUg'><q id='d8dUg'><span id='d8dUg'><b id='d8dUg'><form id='d8dUg'><ins id='d8dUg'></ins><ul id='d8dUg'></ul><sub id='d8dUg'></sub></form><legend id='d8dUg'></legend><bdo id='d8dUg'><pre id='d8dUg'><center id='d8dUg'></center></pre></bdo></b><th id='d8dUg'></th></span></q></dt></tr></i><div id='d8dUg'><tfoot id='d8dUg'></tfoot><dl id='d8dUg'><fieldset id='d8dUg'></fieldset></dl></div>

    2. 想要在 viewpager 上播放声音 滑动

      时间:2023-10-03
          <tbody id='yM32T'></tbody>
        <tfoot id='yM32T'></tfoot>
        <i id='yM32T'><tr id='yM32T'><dt id='yM32T'><q id='yM32T'><span id='yM32T'><b id='yM32T'><form id='yM32T'><ins id='yM32T'></ins><ul id='yM32T'></ul><sub id='yM32T'></sub></form><legend id='yM32T'></legend><bdo id='yM32T'><pre id='yM32T'><center id='yM32T'></center></pre></bdo></b><th id='yM32T'></th></span></q></dt></tr></i><div id='yM32T'><tfoot id='yM32T'></tfoot><dl id='yM32T'><fieldset id='yM32T'></fieldset></dl></div>

        <small id='yM32T'></small><noframes id='yM32T'>

        • <bdo id='yM32T'></bdo><ul id='yM32T'></ul>
          <legend id='yM32T'><style id='yM32T'><dir id='yM32T'><q id='yM32T'></q></dir></style></legend>

              1. 本文介绍了想要在 viewpager 上播放声音 滑动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我正在开发 android 应用程序,该应用程序可以在图像滑动时播放背景声音.我经历了多个教程&找到代码 &我尝试了同样的方法,但它对我不起作用.这是我尝试过的代码..

                i am working on android app which can play background sound whenever image is swipes. I have gone through multiple tutorials & found codes & i tried for the same but it won't work for me. Here is my code which i tried..

                package com.android.learning_numbers;
                
                import android.media.MediaPlayer;
                import android.os.Bundle;
                import android.app.Activity;
                import android.content.Context;
                import android.support.v4.view.PagerAdapter;
                import android.support.v4.view.ViewPager;
                import android.support.v4.view.ViewPager.OnPageChangeListener;
                import android.util.Log;
                import android.view.View;
                import android.view.ViewGroup;
                import android.widget.ImageView;
                
                public class Numbers extends Activity {
                
                    private static final OnPageChangeListener change = null;
                    public static MediaPlayer mp = null;
                
                    @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);
                         viewPager.setOnPageChangeListener(change);}
                
                     private class ImagePagerAdapter extends PagerAdapter {
                
                         private int[] mImages = new int[] {R.drawable.sun,R.drawable.sky,R.drawable.moon,};
                
                
                         private int[] mAudio= new int[]{R.raw.sun,R.raw.sky,R.raw.moon,};
                         @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); 
                            return imageView;
                            }
                
                         public OnPageChangeListener change = new OnPageChangeListener() {
                
                             @Override public void onPageSelected(int position) {
                                 Log.d(ACTIVITY_SERVICE, "OnPageSelected");
                                 if 
                
                                 (mp != null) {
                                  mp.reset();
                                  mp.release();
                                 }
                                 mp = MediaPlayer.create(Numbers.this, mAudio[position]);
                                 mp.start();
                             }
                             @Override public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
                             @Override public void onPageScrollStateChanged(int state) {}
                            };
                
                
                
                         @Override
                         public void destroyItem(ViewGroup container, int position, Object object) {
                           ((ViewPager) container).removeView((ImageView) object);
                         }
                     }
                     }
                

                我认为这里的声音不是由图像滑动类调用的.我的代码有错误吗?因为这里 OnPageChangeListener 根本不适合我.解决方案是什么?如何修改适合我的声音代码?

                As i think here sound is not called by class on image swipe. Is there any error in my code? Because here OnPageChangeListener is not working for me at all. What is the solution for this? How do i modify my sound code that works for me?

                推荐答案

                试试这个代码 &根据您的需要进行更改..它对我有用..

                Try this code & make changes according to your needs.. It works for me..

                public class Alphabets extends Activity {
                
                    MediaPlayer mp;
                    MediaPlayer BG;
                
                
                
                
                    @Override
                    public void onCreate(Bundle savedInstanceState) {
                        super.onCreate(savedInstanceState);
                        setContentView(R.layout.activity_numbers);
                        ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageArra);
                        ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
                        myPager.setOnPageChangeListener(player);
                        myPager.setAdapter(adapter);
                        myPager.setCurrentItem(0);
                        BG = MediaPlayer.create(this, R.raw.jingle);
                        BG.setLooping(true);
                        BG.setVolume(100, 100);
                        BG.start();
                        mp = MediaPlayer.create(Alphabets.this, mAudio[0]);
                        mp.start();
                }
                
                    @Override
                    protected void onPause() {
                        super.onPause();
                        BG.pause();
                        }
                
                    @Override
                    protected void onResume() {
                    super.onResume();
                    BG.start();
                    }
                
                    private int[] mAudio = new int[]{R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,
                                                     R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,R.raw.one,};
                
                    private int imageArra[] =  {R.drawable.ab,R.drawable.bc,R.drawable.cd,R.drawable.de,R.drawable.ef,R.drawable.fg,R.drawable.gh,R.drawable.hi,R.drawable.ij,R.drawable.jk,R.drawable.kl,R.drawable.lm,R.drawable.mn,
                                                R.drawable.no,R.drawable.op,R.drawable.pq,R.drawable.qr,R.drawable.rs,R.drawable.st,R.drawable.tu,R.drawable.uv,R.drawable.vw,R.drawable.wx,R.drawable.xy,R.drawable.yz,R.drawable.zz};
                
                    public  OnPageChangeListener player = new OnPageChangeListener()
                    {
                
                        @Override
                        public void onPageSelected(int arg1)
                        {
                        mp = MediaPlayer.create(Alphabets.this, mAudio[arg1]);
                        mp.stop();
                        }
                
                        @Override
                        public void onPageScrolled(int arg0, float arg1, int arg2)
                        {
                        }
                
                        @Override
                        public void onPageScrollStateChanged(int arg0)
                        {
                        }
                    };
                
                    @Override
                    public boolean onCreateOptionsMenu(Menu menu) {
                        getMenuInflater().inflate(R.menu.numbers, menu);
                        return true;
                    }
                

                }

                这篇关于想要在 viewpager 上播放声音 滑动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Android viewpager检测滑动超出范围 下一篇:点击 Viewpager Xamarin Android

                相关文章

                  <bdo id='Qcsmr'></bdo><ul id='Qcsmr'></ul>

              2. <small id='Qcsmr'></small><noframes id='Qcsmr'>

                <legend id='Qcsmr'><style id='Qcsmr'><dir id='Qcsmr'><q id='Qcsmr'></q></dir></style></legend>
                <tfoot id='Qcsmr'></tfoot>

                    <i id='Qcsmr'><tr id='Qcsmr'><dt id='Qcsmr'><q id='Qcsmr'><span id='Qcsmr'><b id='Qcsmr'><form id='Qcsmr'><ins id='Qcsmr'></ins><ul id='Qcsmr'></ul><sub id='Qcsmr'></sub></form><legend id='Qcsmr'></legend><bdo id='Qcsmr'><pre id='Qcsmr'><center id='Qcsmr'></center></pre></bdo></b><th id='Qcsmr'></th></span></q></dt></tr></i><div id='Qcsmr'><tfoot id='Qcsmr'></tfoot><dl id='Qcsmr'><fieldset id='Qcsmr'></fieldset></dl></div>