Author Topic: Initializing and playing audio  (Read 4448 times)

Offline Dane

  • bit
  • Posts: 6
    • View Profile
Initializing and playing audio
« on: December 14, 2011, 11:39:46 PM »
Hi I've already got the referenced libraries setup in eclipse for my project and I just needed some help setting up the SoundSystem, all I need is to simply play .midi audio file and I just can't figure out how to get it to play it.

I've tried SoundSystem.loadSound and SoundSystem.backgroundMusic etc..

I've also already got
Code: [Select] [nofollow]
public static SoundSystem
RSAudioSystem;
defined and have everything pretty much setup except the SoundSystem.


I would really appreciate the help! Thanks! :)


Also: Since I'm not quite sure how to set it up this is what I've done:
Code: [Select] [nofollow]
public RSAudio()
{
try {
SoundSystemConfig.addLibrary(LibraryJavaSound.class);
SoundSystemConfig.setNumberNormalChannels(15);
RSAudioSystem = new SoundSystem(LibraryJavaSound.class);
} catch (SoundSystemException e) {
e.printStackTrace();
}
}

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3495
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Initializing and playing audio
« Reply #1 on: December 15, 2011, 04:23:09 AM »
What is your OS + Java version?  I know there is a problem with MIDI under some of the Java versions for Linux (I don't recall which off the top of my head).  Let me know if you can hear beethove.mid for this applet (using he Enter key to start it):

Sound Effects Player  (download the Source Code)

If you can hear the music there, then try the examples in my tutorial "3D Sound with SoundSystem" and let me know if they don't work.  If you don't hear MIDI in the above applet, you'll have to use another format (such as .mod which is pretty small) until I finish writing a software synthesizer at some point.

Code: [Select]
mySoundSystem.backgroundMusic( "Cool Music", "beethoven.mid", true );
The newSource() / newStreamingSource() + play() methods or quickPlay() / quickStream() should all work for MIDI as well.

Also: Since I'm not quite sure how to set it up this is what I've done:
Code: [Select]
public RSAudio()
{
try {
SoundSystemConfig.addLibrary(LibraryJavaSound.class);
SoundSystemConfig.setNumberNormalChannels(15);
RSAudioSystem = new SoundSystem(LibraryJavaSound.class);
} catch (SoundSystemException e) {
e.printStackTrace();
}
}
That code looks fine.  Of course, you don't actually need a library plug-in to play MIDI, but I assume you want to play other file types as well, so you are on the right track.
« Last Edit: December 15, 2011, 04:26:08 AM by Paul »
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline Dane

  • bit
  • Posts: 6
    • View Profile
Re: Initializing and playing audio
« Reply #2 on: December 15, 2011, 11:25:58 AM »
What is your OS + Java version?
I'm using Windows 7 Home Premium SP1 and JDK1.7.0_01 / JRE7


Also when I start my applet I get this:
Code: [Select] [nofollow]
Error in class 'MidiChannel'
    Unable to load Midi file in method 'setSequence'.
Error in class 'MidiChannel'
    Exception in method 'play'
    ERROR MESSAGE:
        sequence not set
    STACK TRACE:
        com.sun.media.sound.RealTimeSequencer.start(Unknown Source)
        paulscode.sound.MidiChannel.play(MidiChannel.java:650)
        paulscode.sound.Library.play(Library.java:665)
        paulscode.sound.SoundSystem.CommandPlay(SoundSystem.java:2076)
        paulscode.sound.SoundSystem.CommandQueue(SoundSystem.java:2599)
        paulscode.sound.CommandThread.run(CommandThread.java:121)

What I changed it to:
Code: [Select] [nofollow]
public RSAudio()
{
try {
SoundSystemConfig.addLibrary(LibraryLWJGLOpenAL.class);
SoundSystemConfig.addLibrary( LibraryJavaSound.class);
SoundSystemConfig.setCodec("wav", CodecWav.class);
       
RSAudioSystem = new SoundSystem(LibraryJavaSound.class);
RSAudioSystem.newStreamingSource( true, "Music", signlink.GetCacheDirectory() + "jingle1.mid",
true, 0, 0, 0,
SoundSystemConfig.ATTENUATION_NONE,
0 );
RSAudioSystem.play("Music");

} catch (SoundSystemException e) {
e.printStackTrace();
}
}
« Last Edit: December 15, 2011, 11:37:36 AM by Dane »

Offline Paul

  • Administrator
  • double
  • *****
  • Posts: 3495
  • Developer
    • View Profile
    • PaulsCode.Com
Re: Initializing and playing audio
« Reply #3 on: December 15, 2011, 11:44:02 AM »
Is jingle1.mid compiled in the JAR?  If so, make sure it is in the package "Sounds".

If it is outside of the jar at an online location, use something like this in your newStreamingSource call:
Code: [Select]
RSAudioSystem.newStreamingSource( true, "Music", "http://...[path to file].../jingle1.mid",
                    true, 0, 0, 0,
                    SoundSystemConfig.ATTENUATION_NONE,
                    0 );

In any other case, use the version of newStreamingSource that takes a URL parameter and identifier.  From your code, this looks like maybe what you are trying to do.  Something along these lines:

Code: [Select]
File file=new File( signlink.GetCacheDirectory() + "jingle1.mid" );
URL url=null;
try
{
    url=file.toURL();
    RSAudioSystem.newStreamingSource( true, "Music", url, "jingle1.mid",
                   true, 0, 0, 0,
                    SoundSystemConfig.ATTENUATION_NONE,
                    0 );
}...etc
Device: Samsung Galaxy Nexus i515
CPU: TI OMAP4460, 1.2 GHz (dual core, ARM Cortex-A9)
GPU: PowerVR SGX540, 307 MHz
RAM: 1 GB
Resolution: 720 x 1280
Rom: omni-4.4.4-20141014-toro-FML KitKat 4.4.4, rooted

Device: Eee PC 1015PEM
CPU: Intel Atom N550, 1.5 GHz (dual core, x86)
GPU: Intel GMA 3150, 200 MHz (dual core)
RAM: 2GB
Resolution: 1024 x 600
Rom: android-x86-4.3-20130725 Jelly Bean 4.3, rooted

Offline Dane

  • bit
  • Posts: 6
    • View Profile
Re: Initializing and playing audio
« Reply #4 on: December 15, 2011, 11:54:40 AM »
Ahaa Thank you so much! It works =)