CodenameOne
CodenameOne copied to clipboard
sounds not played (sometimes)
These is a problem with android sound clips, where "sometimes" a clip simply is not played, or is not played in its entirety, or is rendered as a nondescript "thunk" instead of correctly. Curiously, the problem seems to be more prevalent on newer and faster devices, and when the sound clips are shorter. The overall profile of this makes me suspect some interaction with the garbage collector or other memory management issues between codename1 and the underlying android vm.
The attached test problem plays an infinite loop of 1 second cheep-boop sounds. The actual sounds are much shorter than 1 second, and the timing is done with the clock so the rhythm ought to be very regular.
''''java
package com.boardspace.dtest;
import com.codename1.ui.Component; // issue # // // drawing positioned windows // // import com.codename1.ui.Display; import com.codename1.ui.Form; import com.codename1.ui.Graphics; import com.codename1.ui.plaf.UIManager; import com.codename1.ui.util.Resources; import com.codename1.ui.layouts.BorderLayout;
import java.io.IOException; import java.io.InputStream; import java.util.Random; import java.util.Vector;
import com.codename1.media.Media; import com.codename1.media.MediaManager;
class AudioClip { String clipFile; String format = "audio/wav"; //"audio/wave"; Vector<String>messages = new Vector<String>();
public String toString() { return("<clip "+clipFile+">"); }
public AudioClip(String url)
{
clipFile = url;
}
boolean completed = false;
Media media = null;
InputStream stream = null;
AudioClip clip = this;
Runnable complete = new Runnable()
{ public void run()
{ completed = true; media = null;
try { InputStream s = stream;
if(s!=null) { s.close(); }
}
catch (IOException e) { messages.addElement("Close "+e); }
}};
Runnable play = new Runnable()
{ public void run() {
try {
Resources res = Resources.open("/theme.res");
final InputStream instream = res.getData(clipFile);
stream = instream;
completed = false;
media = MediaManager.createMedia(instream, format ,complete);
if(media!=null)
{
media.play() ;
}
}
catch (Throwable ex)
{ String m = "Trouble playing "+clip+" : "+ex;
System.out.println(m);
ex.printStackTrace();
messages.addElement(m);
}
}};
public void play() {
messages.clear();
messages.addElement("playing "+clip);
play.run();
messages.addElement("Played");
}
}
class Window extends Component { AudioClip playing; Window() { super(); } int step = 0; public void play(AudioClip clip) { playing = clip; clip.play(); long now = System.currentTimeMillis(); long later = now; step = 0; byte bb[] = null; do {step++; repaint(); try { Thread.sleep(100); int r = new Random().nextInt(100); // provide some noise to simulate whatever would normally happen while(r-->0) { bb = new byte[1000]; } } catch (InterruptedException e) { e.printStackTrace(); } later = System.currentTimeMillis(); } while(later-now<1000); } public void paint(Graphics g) { int w = getWidth(); int h = getHeight(); g.setColor(0x4f4f4f); g.fillRect(0, 0, w, h); g.setColor(0xffffff); g.drawString(""+playing+" "+step,100,100); if(playing!=null) { Vector<String>m = playing.messages; for(int i=0;i<m.size();i++) { String ss = m.elementAt(i); g.drawString(ss,100, 200+i*40); } }
}
}
@SuppressWarnings("rawtypes") public class Dtest{
private Form current; @SuppressWarnings("unused") private Resources theme;
AudioClip shortcheep = new AudioClip("chirp.wav"); AudioClip longcheep = new AudioClip("droptile.wav"); AudioClip boop = new AudioClip("turnch.wav");
public void init(Object context) { theme = UIManager.initFirstTheme("/theme"); // Pro only feature, uncomment if you have a pro subscription // Log.bindCrashProtection(true); }
public void start() {
if(current != null){
current.show();
}
Form hi = new Form("Hi >>0 World");
hi.setLayout(new BorderLayout());
Window c = new Window();
current = hi;
hi.addComponent(BorderLayout.CENTER,c);
int w = Display.getInstance().getDisplayWidth();
int h = Display.getInstance().getDisplayHeight();
c.setWidth(w);
c.setHeight(h);
hi.show();
Runnable rr = new Runnable () {
public void run() {
System.out.println("running");
while(true)
{
c.play(shortcheep);
c.play(boop);
}}};
new Thread(rr).start();
} public void stop() { current = Display.getInstance().getCurrent(); }
public void destroy() { }
''''