Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Help to solve inconsistent Audio Lag...

I'm having some very strange problems with certain devices. I use tiny MP3 files for key beeps. On most devices, they work great and with no delay. On some devices, e.g. 8230, 8220, there is a significant delay before the keys beep. It does not seem to be related to CDMA/GSM as some posts suggest. Id does it on 8230 hardware as well as the simulator.

 

Other strange byproducts... I use a TimerTask to update a timer on the screen. On these devices, the screen update thread pauses while the beeping is going on. On other devices, everything animates smoothly. It's like the beep is blocking execution... I tried running it as a separate thread but that made no difference.

 

I also tried prefetching the stream but it just blows up. Can you not do that to a stream?

 

I'm trying to play an mp3 that's in the res directory compiled into my class. What it he most efficient way of doing this>

 

I've tried all sorts of workarounds. But I think I need help. My beeping code is as follows:

 

Thanks in advance

 

import java.io.IOException;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;

import net.rim.device.api.system.Alert;
import net.rim.device.api.ui.UiApplication;

public class Beeper {

	static Player _beep1, _beep2, _beep3;
	static Options options = Options.getInstance();
	private int _currentVolume;
	UiApplication theApp = UiApplication.getUiApplication();
	
	Beeper(){
		try {
			Class cl = Class.forName("path.to.my.classApplication");
			_beep1 = Manager.createPlayer(cl.getResourceAsStream("/beep.mp3"),"audio/mpeg");
			_beep2 = Manager.createPlayer(cl.getResourceAsStream("/beep-beep.mp3"),"audio/mpeg");
			_beep3 = Manager.createPlayer(cl.getResourceAsStream("/beep-alarm.mp3"),"audio/mpeg");

			_beep1.realize();
			_beep2.realize();
			_beep3.realize();
			
		} catch (IOException ioe) {
			System.out.println("IOE: "+ioe.toString());
		} catch (Exception e){
			System.out.println("E: "+e.toString());
		}
		_currentVolume = options.getVolume();
		setVolume(_currentVolume);

	}
	static private Beeper _instance = new Beeper(); 
	static public Beeper getInstance() {
		return _instance;
	}
    private void setVolume(int level) {
        VolumeControl vol1 = (VolumeControl) _beep1.getControl("VolumeControl");
        VolumeControl vol2 = (VolumeControl) _beep2.getControl("VolumeControl");
        VolumeControl vol3 = (VolumeControl) _beep3.getControl("VolumeControl");
        vol1.setLevel(level);
        vol2.setLevel(level);
        vol3.setLevel(level);
    }
    
    public void volumeUp() {
        _currentVolume += 10;
        if (_currentVolume > 100) {
            _currentVolume = 100;
        }
        setVolume(_currentVolume);
        options.setVolume(_currentVolume);
    }
    public void volumeDown() {
        _currentVolume -= 10;
        if (_currentVolume < 0) {
            _currentVolume = 0;
        }
       setVolume(_currentVolume);
       options.setVolume(_currentVolume);
    }
    
	void beep(int beeps){
		if (options.getBeepMode()==0){
			//Alert.startAudio(new short[]{5000,10}, 100);
			
			try {
				switch(beeps){
				case 1:
					_beep1.start();
					break;
				case 2:
					_beep2.start();
					break;
				case 3:
					_beep3.start();
					break;
				}
			} catch (MediaException e) {
				// TODO Auto-generated catch block
				switch(beeps){
				case 1:
					Alert.startAudio(new short[]{5000,10}, 100);
					break;
				case 2:
					Alert.startAudio(new short[]{5000,10,5,10,5000,10}, 100);
					break;
				case 3:
					Alert.startAudio(new short[]{5000,2000}, 100);
					break;
				}
				e.printStackTrace();
			}
		}
	}

}