gpt4 book ai didi

java - 处理最小空指针

转载 作者:行者123 更新时间:2023-11-30 09:13:51 26 4
gpt4 key购买 nike

我正在使用 Minim 处理,但如果 minim 加载文件在另一个类中,我不知道如何播放文件。我同时使用了 AudioSnippetAudioSample,它们都给我 NullPointerException。这是一个 PlayAudio 类。

  Minim minim;
AudioSample sample;
AudioSnippet snippet;

class PlayAudio {

PlayAudio() {
minim = new Minim(this);

sample = minim.loadSample("Audio/hover1.mp3", 2048);
snippet = minim.loadSnippet("Audio/hover1.mp3");
}

void playSnippet() {
sample.trigger();
snippet.play();
}
}

另一个是标准处理setup()draw()。按下鼠标时应播放文件。

import ddf.minim.spi.*;
import ddf.minim.signals.*;
import ddf.minim.*;
import ddf.minim.analysis.*;
import ddf.minim.ugens.*;
import ddf.minim.effects.*;

PlayAudio audio = new PlayAudio();

void setup() {
size(300, 300);
background(0);
}

void draw() {
if(mousePressed) {
audio.playSnippet();
}
}

我得到的错误是:

==== JavaSound Minim Error ====
==== Couldn't find a sketchPath method on the file loading object provided!
==== File recording will be disabled.

==== JavaSound Minim Error ====
==== Couldn't find a createInput method in the file loading object provided!
==== File loading will be disabled.

==== JavaSound Minim Error ====
==== Error invoking createInput on the file loader object: null

=== Minim Error ===
=== Couldn't load the file Audio/hover1.mp3

最佳答案

只需将 minim = new Minim( this ); 放入设置 block 。如果您在 PlayAudio 类中执行此操作,您会得到错误的 this 参数。

工作代码:

import ddf.minim.*;

Minim minim;
AudioSample sample;
AudioSnippet snippet;

PlayAudio audio;

void setup() {
size(300, 300);
minim = new Minim(this);
audio = new PlayAudio();
background(0);
}

void draw() {
if (mousePressed) {
audio.playSnippet();
}
}

class PlayAudio {
PlayAudio() {
sample = minim.loadSample("Audio/hover1.mp3", 2048);
snippet = minim.loadSnippet("Audio/hover1.mp3");
}

void playSnippet() {
sample.trigger();
snippet.play();
}
}

关于java - 处理最小空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20816031/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com