简体   繁体   English

Android MediaPlayer 在 Nougat 中不起作用

[英]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.我正在尝试播放媒体文件,此代码在 android N 以下运行良好,但不适用于 Nougat,请建议我在所有 android 版本上播放媒体文件的最佳方式。

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"))我刚刚用 if(flag.equals("1")) 替换了这段代码 if(flag=="1")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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