0

Is there an error in the code?

It works from my main activity and my fragment. But if I use the method (loadSound) in a sound class, getAssets() doesn't work.

please help me,Any ideas? Thanks!

this is my fragment code :

public class CarnivoreFragment extends Fragment {

    private int mCowSound;
    private View view;

    private ImageButton mCowImageButton;


    public CarnivoreFragment() {
    }


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

        view = inflater.inflate(R.layout.fragment_carnivore, container, false);

        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {

            Sound.createOldSoundPool();
        } else {
            Sound.createNewSoundPool();
        }

        mCowSound = Sound.loadSound("cow.wav");

        mCowImageButton = (ImageButton) view.findViewById(R.id.imageButtonCow);
        mCowImageButton.setOnClickListener(onClickListener);

        return view;
    }

    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.imageButtonCow:
                    Sound.playSound(mCowSound);
                    Toast.makeText(view.getContext(), "Cow", Toast.LENGTH_SHORT).show();

                    break;

            }
        }
    };
}

and my "Sound" class :

public class Sound {

    private  static SoundPool mSoundPool;
    private  static AssetManager mAssetManager;
    private  static int mStreamID;
    private  static Context context;


    public Sound(Context context) {
        this.context = context;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public static void createNewSoundPool() {
        AudioAttributes attributes = new AudioAttributes.Builder()
                .setUsage(AudioAttributes.USAGE_GAME)
                .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                .build();
        mSoundPool = new SoundPool.Builder()
                .setAudioAttributes(attributes)
                .build();
    }

    @SuppressWarnings("deprecation")
    public static void createOldSoundPool() {
        mSoundPool = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
    }

    public static int playSound(int sound) {
        if (sound > 0) {
            mStreamID = mSoundPool.play(sound, 1, 1, 1, 0, 1);
        }
        return mStreamID;
    }

    public static int loadSound(String fileName) {
        mAssetManager = context.getAssets();
        AssetFileDescriptor afd;
        try {
            afd = mAssetManager.openFd(fileName);
        } catch (IOException e) {
            e.printStackTrace();
              Toast.makeText(context, "Не могу загрузить файл " + fileName,
                    Toast.LENGTH_SHORT).show();
            return -1;
        }
        return mSoundPool.load(afd, 1);
    }


}
koral
  • 2,513
  • 1
  • 32
  • 41
o.m.k
  • 45
  • 1
  • 4

1 Answers1

0

It looks as though you might never actually be setting the context in your Sound class since all of the calls appear to be called via static methods. You have the class setting the context in the constructor, but the constructor does not appear to ever be called.

lodlock
  • 3,638
  • 1
  • 19
  • 15