【问题标题】:Loading soundpool in class extending fragment activity android在扩展片段活动android的类中加载soundpool
【发布时间】:2013-05-30 14:08:41
【问题描述】:

我正在开发一个安卓应用程序。我的课程扩展了 fragmentactivity。我正在使用 soundpool,使用 asyc 将声音加载到 soundpool。
我在类的 oncreate 中调用异步,并且在 onpagechagelistner 中播放声音。当我运行应用程序时,出现空指针异常。

请看下面我的 onpagechangelistner 和 Async。

_mViewPager.setOnPageChangeListener(new OnPageChangeListener()  
    {
        public void onPageSelected(int page)  
        {
            if(page==0)
            {
                soundPool.play(1, streamVolume, streamVolume, 1, 0, 1f);
            }
            else if(page==1)
            {
                soundPool.play(2, streamVolume, streamVolume, 1, 0, 1f);
            }
            else if(page==2)
            {
                soundPool.play(3, streamVolume, streamVolume, 1, 0, 1f);
            }
     }
protected class MainSoundsforalphabetsleftout extends AsyncTask<Context, Integer, Boolean>
 {
    boolean result = false;
   @Override
    protected Boolean doInBackground(Context... params) 
    {
        Log.e("inside","doin bg");
        soundPoolMapForAlphabetsleftout.put(1, soundPoolForAlphabetsleftout.load(AlphaPager.this, R.raw.tellme, 1));
        soundPoolMapForAlphabetsleftout.put(2, soundPoolForAlphabetsleftout.load(AlphaPager.this, R.raw.okay, 1));
        soundPoolForAlphabetsleftout.setOnLoadCompleteListener(new OnLoadCompleteListener() {

            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) 
            {
                globe.setSoundPoolForalphabetsleftout(soundPoolForAlphabetsleftout);
            }
        });
        return true;
    }

    @Override
    protected void onPostExecute( Boolean result ) 
    {
        super.onPostExecute(result);
                    Log.e("inside","postexecute of left out alpha");        
                    soundPoolleftout = globe.getSoundPoolForalphabetsleftout();
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
                Log.e("inside","preexecute of left out alpha");         
            }

    @Override
    protected void onProgressUpdate(Integer... values) 
    {
        super.onProgressUpdate(values);
    }
           }

请帮帮我。两天以来一直在努力。
谢谢!

【问题讨论】:

    标签: android android-viewpager soundpool


    【解决方案1】:

    首先,向我们展示堆栈跟踪;它会在代码的哪一行显示你正在获取空指针以及它在此之前做了什么。

    另外,请说明您在何处以及如何初始化该声音池。它是单例类还是在活动中声明?我使用以下

    private static SoundManager _instance = null;
    private SoundPool mSoundPool;
    private HashMap<Integer, Integer> mSoundPoolMap;
    private AudioManager  mAudioManager;
    private Context mContext;
    

    使用哈希图存储我的声音(以及播放、暂停、停止播放声音的方法)。

    重点是,显示更多代码 :) 我们看不出哪里出了问题。 `

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多