简体   繁体   English

为什么 SoundFile.play 不工作处理 4

[英]Why isn't SoundFile.play not working processing 4

So I'm trying to play a simple sound file and it's not working.所以我试图播放一个简单的声音文件,但它不工作。 The specific code is具体代码是

  if(key == 'r' || key == 'R') {
    file = new SoundFile(this, "track1.wav");
    file.play();
  }
  if(key == 't' || key == 'T') {
    file = new SoundFile(this, "track2.wav");
    file.play();
  }
  if(key == 'y' || key == 'Y') {
    file = new SoundFile(this, "track3.wav");
    file.play();
  }

It gives me this error它给了我这个错误

javax.sound.sampled.LineUnavailableException: line with format PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian not supported.
    at java.desktop/com.sun.media.sound.DirectAudioDevice$DirectDL.implOpen(DirectAudioDevice.java:484)
    at java.desktop/com.sun.media.sound.AbstractDataLine.open(AbstractDataLine.java:115)
    at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.start(Unknown Source)
    at com.jsyn.engine.SynthesisEngine$EngineThread.run(Unknown Source)
java.lang.NullPointerException: Cannot invoke "javax.sound.sampled.TargetDataLine.read(byte[], int, int)" because "this.line" is null
    at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.read(Unknown Source)
    at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.read(Unknown Source)
    at com.jsyn.engine.SynthesisEngine$EngineThread.run(Unknown Source)
java.lang.RuntimeException: AudioInput stop attempted when no line created.
    at com.jsyn.devices.javasound.JavaSoundAudioDevice$JavaSoundInputStream.stop(Unknown Source)
    at com.jsyn.engine.SynthesisEngine$EngineThread.run(Unknown Source)

i suggest to add this sound files under res folder.我建议将这个声音文件添加到 res 文件夹下。 in my project, I created folder named "raw" under "res folde" with these files in it, sound.mp3 and for playing it, i used this在我的项目中,我在“res folde”下创建了名为“raw”的文件夹,其中包含这些文件,sound.mp3,为了播放它,我使用了这个

mediaPlayer = MediaPlayer.create(SCREEN_NAME.this,R.track1);
                                    mediaPlayer.start();

edit: and i created this variable编辑:我创建了这个变量

    private MediaPlayer mediaPlayer;

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

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