预加载音效

发布于 2024-11-24 08:33:43 字数 2025 浏览 3 评论 0原文

我的计数器正在工作(感谢您的帮助),并且我已经使用 MediaPlayer 添加了声音,问题是加载计数器存在延迟,因为声音加载在同一个类上。

有没有办法使用主类预加载声音,这样,当调用计数器时,数字就会被缓冲并可以从该类中调用?

该应用程序有一个启动屏幕,唯一的目的是加载其他类所需的任何内容以避免延迟。

如果无法从其他类加载声音,是否可以在播放时单独加载它们?

这就是我到目前为止所拥有的...

final boolean sound = timer_sound.getBoolean("timer_sound", true);

  final MediaPlayer number01 = MediaPlayer.create(this, R.raw.number_1);
  final MediaPlayer number02 = MediaPlayer.create(this, R.raw.number_2);
  final MediaPlayer number03 = MediaPlayer.create(this, R.raw.number_3);
  final MediaPlayer number04 = MediaPlayer.create(this, R.raw.number_4);
  final MediaPlayer number05 = MediaPlayer.create(this, R.raw.number_5);
  final MediaPlayer number06 = MediaPlayer.create(this, R.raw.number_6);
  final MediaPlayer number07 = MediaPlayer.create(this, R.raw.number_7);
  final MediaPlayer number08 = MediaPlayer.create(this, R.raw.number_8);
  final MediaPlayer number09 = MediaPlayer.create(this, R.raw.number_9);
  final MediaPlayer number10 = MediaPlayer.create(this, R.raw.number_10);
  final MediaPlayer number15 = MediaPlayer.create(this, R.raw.number_15);
  final MediaPlayer number20 = MediaPlayer.create(this, R.raw.number_20);

lastSeconds = (TextView) findViewById(R.id.lastminuteseconds);

lastMinute = new CountDownTimer(60 * 1000, 1000) {

  public void onTick(long secondsToStart) {
    int elapseTime = (int) (secondsToStart / 1000);

    if (sound == true){
      switch(elapseTime) {
        case 1: number01.start();break;
        case 2: number02.start();break;
        case 3: number03.start();break;
        case 4: number04.start();break;
        case 5: number05.start();break;
        case 6: number06.start();break;
        case 7: number07.start();break;
        case 8: number08.start();break;
        case 9: number09.start();break;
        case 10: number10.start();break;
        case 15: number15.start();break;
        case 20: number20.start();break;
      }
    }

这几乎可以正常工作,问题是加载文件有 2 秒的延迟,我想再添加一些(50、40、30)。

I have the counter working (thanks for the help) and I have added sound using MediaPlayer, the problem is that there is a delay loading the counter because the sounds are loaded on the same class.

Is there a way to pre-load the sounds using the main class, so then, when the counter is called, the numbers are buffered and can be called from this class?

The application has a splash screen and the only purpose is to load anything needed by the other classes to avoid delays.

If sounds can't be loaded from another class, could they be loaded individually at play time?

This is what I have so far...

final boolean sound = timer_sound.getBoolean("timer_sound", true);

  final MediaPlayer number01 = MediaPlayer.create(this, R.raw.number_1);
  final MediaPlayer number02 = MediaPlayer.create(this, R.raw.number_2);
  final MediaPlayer number03 = MediaPlayer.create(this, R.raw.number_3);
  final MediaPlayer number04 = MediaPlayer.create(this, R.raw.number_4);
  final MediaPlayer number05 = MediaPlayer.create(this, R.raw.number_5);
  final MediaPlayer number06 = MediaPlayer.create(this, R.raw.number_6);
  final MediaPlayer number07 = MediaPlayer.create(this, R.raw.number_7);
  final MediaPlayer number08 = MediaPlayer.create(this, R.raw.number_8);
  final MediaPlayer number09 = MediaPlayer.create(this, R.raw.number_9);
  final MediaPlayer number10 = MediaPlayer.create(this, R.raw.number_10);
  final MediaPlayer number15 = MediaPlayer.create(this, R.raw.number_15);
  final MediaPlayer number20 = MediaPlayer.create(this, R.raw.number_20);

lastSeconds = (TextView) findViewById(R.id.lastminuteseconds);

lastMinute = new CountDownTimer(60 * 1000, 1000) {

  public void onTick(long secondsToStart) {
    int elapseTime = (int) (secondsToStart / 1000);

    if (sound == true){
      switch(elapseTime) {
        case 1: number01.start();break;
        case 2: number02.start();break;
        case 3: number03.start();break;
        case 4: number04.start();break;
        case 5: number05.start();break;
        case 6: number06.start();break;
        case 7: number07.start();break;
        case 8: number08.start();break;
        case 9: number09.start();break;
        case 10: number10.start();break;
        case 15: number15.start();break;
        case 20: number20.start();break;
      }
    }

this is working almost fine, the problem is that there is a 2 second delay loading the files, and I wanted to add few more (50, 40, 30).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

往日 2024-12-01 08:33:43

你的方法看起来很浪费。您应该加载第一个声音,并在播放时触发下一个声音的加载,因此任何时候您都只在 RAM 中缓存两个声音(当前和下一个)。

因此,您只预加载第一个声音,然后在 ontick() 中,通过 run() 播放当前声音时触发加载下一个声音,并将声音对象交换为下一个 ontick()。

Your approach seems very wasteful. You should load the first sound, and upon playing it trigger the load of the next sound, so at any time you are only caching two sounds (current and next) in RAM.

So you pre-load only first sound, then in ontick() you trigger load of next sound upon playing current sound via run() and swap the sound objects for next ontick().

风和你 2024-12-01 08:33:43

我正在慢慢学习,我已经弄清楚了这一点。

我不敢相信这样做有多么简单......

不放所有代码,这里是所做的更改;

首先,我停止使用开关加载声音,但我使用开关调用类。

  if (sound == true){
    switch(elapseTime) {
      case 1: number01();break;
      case 2: number02();break;

然后,我在调用类时加载声音;

 public void number01(){
   final MediaPlayer number01 = MediaPlayer.create(this, R.raw.number_1);
     number01.start();
   }
 public void number02(){
    final MediaPlayer number02 = MediaPlayer.create(this, R.raw.number_2);
    number02.start();
 }

现在,当调用开关时,它会加载声音并播放它。这会停止加载应用程序时的延迟。

我希望这能帮助其他像我一样通过犯错误并纠正错误来学习的初学者。

I am learning slowly and I have figured this out.

I can't believe how simple it was to do this....

Not to put all the code, here are the changes done;

First, I stop loading the sound with the switch, but I used the switch call a class..

  if (sound == true){
    switch(elapseTime) {
      case 1: number01();break;
      case 2: number02();break;

Then, I load the sounds when the class is called;

 public void number01(){
   final MediaPlayer number01 = MediaPlayer.create(this, R.raw.number_1);
     number01.start();
   }
 public void number02(){
    final MediaPlayer number02 = MediaPlayer.create(this, R.raw.number_2);
    number02.start();
 }

Now when the switch is called and it loads the sound and plays it. This stops the delay when loading the application.

I hope this helps other beginners, that like me, are learning by making mistakes and correcting them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文