A
A
Alexander Sinitsyn2018-02-15 23:53:34
Android
Alexander Sinitsyn, 2018-02-15 23:53:34

Why does sound disappear on some devices?

There are no errors related to this in the console, but users sometimes write that the sound disappears after a while. Appears after restarting the application. How can you catch and fix?

It's done like this
public class MyAudioControl {

    private final Context mContext;

    private AudioManager mAudioManager;
    private SoundPool mSoundPool;
    private int mStream;

    /**
     *
     * @param context Context
     * @return MyAudioControl
     */
    public static MyAudioControl get(Context context) {

        return new MyAudioControl(context, AudioManager.STREAM_MUSIC);
    }

    /**
     *
     * @param context Context
     * @param stream int
     * @return SkAudioControl
     */
    public static MyAudioControl get(Context context, int stream) {

        return new MyAudioControl(context, stream);
    }

    /**
     *
     * @param context Context
     * @param stream int
     */
    private MyAudioControl(Context context, int stream) {

        mContext = context;
        mStream = stream;
        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);

        mSoundPool = new SoundPool(1, mStream, 1);

        mSoundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {
            @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {

                float soundValue = (float) getSoundValue();
                float soundMaxValue = (float) getSoundMaxValue();

                float value = soundValue / soundMaxValue;

                soundPool.play(sampleId, value, value, 1, 0, 1);
            }
        });

    }

    /**
     *
     * @return int
     */
    private int getSoundValue() {

        return mAudioManager.getStreamVolume(mStream);
    }

    /**
     *
     * @return int
     */
    private int getSoundMaxValue() {

        return mAudioManager.getStreamMaxVolume(mStream);
    }

    /**
     *
     * @param fileName String
     */
    public void play(String fileName) {

        if (mAudioManager.getStreamVolume(mStream) == 0) {
            return;
        }

        AssetFileDescriptor afd = null;

        try {
            afd = mContext.getAssets().openFd("sounds/" + fileName + ".mp3");
        } catch (IOException e) {
            e.printStackTrace();
        }

        mSoundPool.load(afd, 1);
    }
}


Made it a singleton, like the problem disappeared?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
O
one pavel, 2018-02-16
@onepavel

a little off topic,
my experience, I wrote a music player, I did everything, sings, pumps,
and when the stream was interrupted, all devices shut up safely
in the absence of bytes in the cache, and the budget lg started to whistle,
I solved the problem by leaving the android api to libbass

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question