Reputation: 72
I have this code for Android, that plays a custom sound, it works fine:
AudioTrack output = new AudioTrack(AudioManager.STREAM_MUSIC, baudRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_8BIT, bufferSize, AudioTrack.MODE_STATIC);
output.write(myGeneratedWaveform, 0, myGeneratedWaveform.length);
output.play();
How can I achieve the same with j2me?
I'm checking the API of Manager.createPlayer(...) but didn't find a sample code.
Any help would be apreciated.
EDIT: I have an array with a PCM encoded waveform. I wish to reproduce this with Nokia Series 40. But I conclude that there no such support for this format, just AMR. So, a library in open source to convert from PCM to AMR will be ok.
Upvotes: 0
Views: 610
Reputation: 21
The J2ME built-in music player is able to play any input stream, not only one captured from Media Store. I mean, you can create raw bytes array, which represents your sound data in certain audio format and then, representing it as an input stream, play it as if it were the prestored audio file.
byte[] data; // your audio data, let us say it represents content of a proper wav-file
Player player = Manager.createPlayer(new ByteArrayInputStream(data), "audio/x-wav");
player.start();
That's all. By the way, you may simply use an old version of JFugue, which can be compiled for J2ME using the above trick. You will get midi output generated from music string, such as "Cmaj G I[Flute] C" -- it may be convenient for ear training midlet, or so. Anyone interested may check the http://code.google.com/p/jfugue2me/
Upvotes: 0
Reputation: 4043
Please, check Scenario 7 at JSR-135 Overview: Playing Back from Media Stored in JAR.
InputStream is = getClass().getResourceAsStream("audio.wav");
Player p = Manager.createPlayer(is, "audio/X-wav");
p.start();
Upvotes: 0