为我的android应用程序播放声音

为我的android应用程序播放声音,android,audio,Android,Audio,我试图加载一个声音效果,当特定事件发生时播放 出于某种原因,我可以听到声音,但它不能播放整个文件,它会在中间刹车。 我的声音代码: public class SoundManager { private Context context; private int nudgeSound = R.raw.nudge; public SoundManager(Context context) { this.context = context;

我试图加载一个声音效果,当特定事件发生时播放

出于某种原因,我可以听到声音,但它不能播放整个文件,它会在中间刹车。

我的声音代码:

public class SoundManager {

    private Context context;
    private int nudgeSound = R.raw.nudge;


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

    public void playNudgeSound()
    {           
        final MediaPlayer mediaPlayer = MediaPlayer.create(context, nudgeSound);

          mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

              @Override
              public void onCompletion(MediaPlayer mp) {
                  mediaPlayer.reset();
              }

          });   
          mediaPlayer.start();

    }
}
我的初始化:

    SoundManager soundManager = new SoundManager(this);
    soundManager.playNudgeSound();

你有没有想过改用声音池?!在我的应用程序中,这很好

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);

SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_NOTIFICATION, 0);
int sound = soundPool.load(context, R.raw.beep, 1);
soundPool.play(sound, maxVolume, maxVolume, 1, 0, 1f);

你有没有想过改用声音池?!在我的应用程序中,这很好

AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_NOTIFICATION);

SoundPool soundPool = new SoundPool(10, AudioManager.STREAM_NOTIFICATION, 0);
int sound = soundPool.load(context, R.raw.beep, 1);
soundPool.play(sound, maxVolume, maxVolume, 1, 0, 1f);

好的,经过几天的思考和挫折,我发现我的问题是MediaPlayer.create()没有太多时间将文件加载到内存中。。。 这是因为create()和start()几乎同时被调用

我的建议是在应用程序加载时加载所有声音文件,并在需要时调用start()。构建某种SoundManager类


谢谢大家!希望它能帮助别人

好的,经过几天的思考和挫折,我发现我的问题是MediaPlayer.create()没有太多时间将文件加载到内存中。。。 这是因为create()和start()几乎同时被调用

我的建议是在应用程序加载时加载所有声音文件,并在需要时调用start()。构建某种SoundManager类


谢谢大家!希望它能帮助别人

如果不调用reset(),它会在播放孔文件之前制动吗?是的,它会。。。我确实有一个震动在同一时间进行,但即使我禁用它,我仍然得到相同的刹车..有一个小的可能性是有什么问题的音频文件。您是否可以尝试其他方式,例如使用示例应用程序或通过浏览器?播放声音时活动是否关闭?如果活动在中途关闭,则只会播放一部分声音。如果不调用reset(),则在播放孔文件之前是否会停止?是的,它会。。。我确实有一个震动在同一时间进行,但即使我禁用它,我仍然得到相同的刹车..有一个小的可能性是有什么问题的音频文件。您是否可以尝试其他方式,例如使用示例应用程序或通过浏览器?播放声音时活动是否关闭?如果活动中途结束,则只会播放一部分声音。很高兴听到您整理好了它。很高兴听到您整理好了它