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
editormax
Posts: 133
Registered: ‎07-20-2009
My Device: Not Specified

Use Alert.startAudio to play a .mid file

Dear all,

   I would like to play a sound effect on top of the background music, I know Alert.startAudio can work with this. But I don't know how to make a .mid file to short[] for put into the method   Alert.startAudio(shot[] tune, int volumn). Anyone can help?

 

Thanks a lot

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Use Alert.startAudio to play a .mid file

You should probably use Player instead - the Alert  class is a holdover from the days of Blackberries that only had a buzzer.

 

Contributor
sleepymg
Posts: 35
Registered: ‎02-11-2010
My Device: curve 8900

Re: Use Alert.startAudio to play a .mid file

    You can play .mid file using code below:

    byte[] tuneBytes;
    InputStream is;

    try
    {
      is = getClass().getResourceAsStream("/tune.mid");
      tuneBytes= new byte[is.available()];
      is.read(tuneBytes);
      Alert.startMIDI(tuneBytes, false);
    }
    catch (Exception ex)
    {
     System.out.println("Exception playing audio: " + ex.toString());
    }

 

 

 

Developer
editormax
Posts: 133
Registered: ‎07-20-2009
My Device: Not Specified

Re: Use Alert.startAudio to play a .mid file

But If I use two player to play two sound files, only one sound file can be played. Do you know how can I play two sounds file at the same time?

Developer
editormax
Posts: 133
Registered: ‎07-20-2009
My Device: Not Specified

Re: Use Alert.startAudio to play a .mid file

thanks you for telling me how to use Alert to play the .mid file. But I think the play two sound files only work for startAudio(short[] tune, int volume). Do you have any idea can play two sound files at the same time?

Contributor
sleepymg
Posts: 35
Registered: ‎02-11-2010
My Device: curve 8900

Re: Use Alert.startAudio to play a .mid file

Try this:

 

 

// The TUNE (bar 1 and 2 of Islamey by Balakirev).
    final short BFlat = 466; // 466.16
    final short AFlat = 415; // 415.30
    final short A = 440; // 440.00
    final short GFlat = 370; // 369.99
    final short DFlat = 554; // 554.37
    final short C = 523; // 523.25
    final short F = 349; // 349.32
    final short TEMPO = 500; //125;
    final short d16 = 1 * TEMPO; // Duration of a 16th note, arbitrary, in ms.
    final short d8 = d16 << 1; // Duration of an eigth note, arbitrary, in ms.
    final short dpause = 10; // 10 ms pause
    final short pause = 0; // Zero frequency pause

    final short[] TUNE = new short[] { BFlat, d16, pause, dpause, BFlat, d16, pause,
            dpause, BFlat, d16, pause, dpause, BFlat, d16, pause, dpause, A, d16, pause, dpause,
            BFlat, d16, pause, dpause, GFlat, d16, pause, dpause, GFlat, d16, pause, dpause, A,
            d16, pause, dpause, BFlat, d16, pause, dpause, DFlat, d16, pause, dpause, C,
            d16, pause, dpause, // Bar 1
            AFlat, d16, pause, dpause, AFlat, d16, pause, dpause, AFlat, d16, pause, dpause, AFlat,
            d16, pause, dpause, F, d16, pause, dpause, GFlat, d16, pause, dpause, AFlat, d16,
            pause, dpause, BFlat, d16, pause, dpause, AFlat, d16, pause, dpause, F, d8 + d16                                
        };
final short[] TUNE1 = new short[] { A, 150, BFlat, 150, C, 200 };
        
Alert.startAudio( TUNE, 80 );