Ammy Kang
Ammy Kang

Reputation: 12602

Android MediaPlayer not working in Nougat

I am trying to play media file, this code is working fine for below android N, but its not working for Nougat, please suggest me the best way to play media file on all android version.

private void Beep(String flag)
{
    if(flag=="1")
    {
        if(beepPlay==null)
        {
            beepPlay= MediaPlayer.create(this, R.raw.alarmsound);
            beepPlay.setLooping(true);
            beepPlay.setVolume(100,100);
        }
        AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 20, 0);
        beepPlay.start();
        ((Vibrator)getSystemService(VIBRATOR_SERVICE)).vibrate(2000);
    }
    else
    {
        if(beepPlay!=null) {
            beepPlay.stop();
            beepPlay.release();
            beepPlay = null;
        }
    }

}

Upvotes: 3

Views: 550

Answers (1)

Ammy Kang
Ammy Kang

Reputation: 12602

I've resolved the issue. there was a silly mistake in my code that's why the code was not running in Nougat. I just have replaced this piece of code if(flag=="1") with if(flag.equals("1"))

Upvotes: 1

Related Questions