简体   繁体   中英

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;
        }
    }

}

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM