Audio processing details

Source: Internet
Author: User

/**
* By colico Email: colico@163.com
* Http://blog.csdn.net/colico
* Http://colico.ys168.com
* Note: This is the original version of Xiao O. If you need to reprint it, please attach the above information.
*
*/

This article is about how to handle the sound when porting different models in the same game. Considering the performance requirements, this article has made certain requirements for each type of mobile phones.

Sound processing in s40:

1) Import com. Nokia. Mid. Sound .*;

2)

Sound soundplayer;
Void initsound (){
Soundplayer = new sound (B _main, 1 );
If (m_playsound = 1 ){
Soundplayer. Play (0 );
}
}

3)
Byte [] B _main = {
(Byte) 0x02, (byte) 0x4a, (byte) 0x3a, (byte) 0x40,
(Byte) 0x04, (byte) 0x01, (byte) 0x1f, (byte) 0x1e,
(Byte) 0x54, (byte) 0x88, (byte) 0x38, (byte) 0x84,
(Byte) 0x44, (byte) 0xbc, (byte) 0x4a, (byte) 0xc4,
(Byte) 0xa0, (byte) 0xa9, (byte) 0x0b, (byte) 0x91,
(Byte) 0x27, (byte) 0x22, (byte) 0xa2, (byte) 0xb1,
(Byte) 0x31, (byte) 0x13, (byte) 0x88, (byte) 0x00,
};

4)
Static int m_playsound = 1;

5) Sound Control in the program
M_playsound = (byte) (1-m_playsound );
If (m_playsound = 1 ){
Try {
Soundplayer. Play (0 );
} Catch (exception e ){}
}
If (m_playsound = 0 ){
Try {
Soundplayer. Stop ();
} Catch (exception e ){}
}

//----------------------------------------------------

Use Ott files in Nokia 40 or 60

1) define the data structure
Public class emsound
{
Public int type;
Public byte [] data;

Public emsound (byte [] data, int type)
{
This. type = type;
This. Data = data;
}
}

2)
Import com. Nokia. Mid. UI .*;
Import com. Nokia. Mid. Sound .*;
3)
Sound soundplayer;
Soundlistener = new emsoundlistener ();

Emsound currentsound = NULL;
Boolean soundplaying = false;
Boolean soundenable = true;

Class emsoundlistener
Implements soundlistener {
Public void soundstatechanged (sound, int event ){
Switch (event ){
Case sound. sound_stopped:
Soundplaying = false;
Break;
Case sound. sound_playing:
Soundplaying = true;
}
}
}

Public emsound loadsound (string resfile, int resid ){
Emsound sound;
Try {
Inputstream is = getclass (). getresourceasstream (resfile + "/" + resid +
". Ott ");
Int Len = (INT) is. Skip (10000 );
Is. Close ();
Is = getclass (). getresourceasstream (resfile + "/" + resid + ". Ott ");
Byte [] Barr = new byte [Len];
Is. Read (Barr );
Is. Close ();
Sound = new emsound (Barr, sound. format_tone );
}
Catch (exception ex ){
Sound = NULL;
}
Return sound;
}

Public void playsound (emsound sound, int count ){
If (! Soundenable ){
Return;
}
Try {// colico
If (soundplaying ){
Stopsound ();
}
If (soundplayer = NULL ){
Soundplayer = new sound (sound. Data, sound. type );
Soundplayer. setsoundlistener (soundlistener );
Currentsound = NULL;
}
If (sound! = Currentsound ){
Soundplayer. Release ();
Soundplayer. INIT (sound. Data, sound. type );
Currentsound = sound;
}

Soundplayer. Play (count );
}
Catch (exception ex ){
Soundplaying = false;
}
}

Sound [] soundplayers;
Public void playsound (emsound sound [], int LOC)
{
If (! Soundenable) {return ;}

Try {
If (soundplaying) stopsound ();
If (soundplayers = NULL ){
Soundplayers = new sound [sound. Length];
System. Out. println ("sounds = NULL ");
For (INT I = 0; I <sound. length; I ++ ){
Soundplayers [I] = new sound (sound [I]. Data, sound [I]. type );
Soundplayers [I]. setsoundlistener (soundlistener );
Soundplayers [I]. INIT (sound [I]. Data, sound [I]. type );
}
}

Long Now = system. currenttimemillis ();
Soundplayers [loc]. Play (1 );
System. Out. println ("playing sounds ");
System. Out. println ("playing sounds time" + (system. currenttimemillis ()-Now ));
} Catch (exception ex ){
Soundplaying = false;
}
}

Public void stopsound (){
If (! Soundenable ){
Return;
}
If (soundplayer! = NULL) {// colico
Soundplayer. Stop ();
}
}

Public Boolean issoundplaying (){
Return soundplaying;
}

Public Boolean issoundenable (){
Return soundenable;
}

Public void setsoundenable (Boolean e ){
If (! E ){
Stopsound ();
}
Soundenable = E;
}

In v300
1 ).
Public class emsound
{
Public string type;
Public byte [] data;

Public emsound (byte [] data, string type)
{
This. type = type;
This. Data = data;
}

}

2 ).

Import javax. microedition. Media. player;
Import javax. microedition. Media. playerlistener;
Import javax. microedition. Media. Manager;
Import javax. microedition. Media. Control .*;

3). // sound soundplayer;
Playerlistener soundlistener = new emsoundlistener ();
Player soundplayer;
Emsound currentsound = NULL;
Boolean soundplaying = false;
Boolean soundenable = true;

Class emsoundlistener
Implements playerlistener {

Public void playerupdate (player, string event, object eventdata) {// soundstatechanged (INT event)
If (event = playerlistener. Stopped ){
Soundplaying = false;
}
If (event = playerlistener. Started ){
Soundplaying = true;
}
}
}

Public emsound loadsound (string resfile, int resid ){
Emsound sound;
Try {
Inputstream is = getclass (). getresourceasstream (resfile + "/" + resid +
". Mid ");
Int Len = (INT) is. Skip (10000 );
Is. Close ();
Is = getclass (). getresourceasstream (resfile + "/" + resid + ". Mid ");
Byte [] Barr = new byte [Len];
Is. Read (Barr );
Is. Close ();
Sound = new emsound (Barr, "audio/Midi ");
}
Catch (exception ex ){
Sound = NULL;
}
Return sound;
}

Public void playsound (emsound sound, int count ){
If (! Soundenable ){
Return;
}
Try {
If (soundplaying ){
Stopsound ();
}
If (soundplayer = NULL ){
Soundplayer = manager. createplayer (New bytearrayinputstream (sound. Data ),
Sound. type );
Soundplayer. addplayerlistener (soundlistener );
Currentsound = NULL;
}
If (sound! = Currentsound ){
Soundplayer. Close ();
Soundplayer = manager. createplayer (New bytearrayinputstream (sound. Data ),
Sound. type );
Currentsound = sound;
}
Soundplayer. Start ();
}
Catch (exception ex ){
Soundplaying = false;
System. Out. println (ex. tostring ());
}
}

Public void stopsound (){
If (! Soundenable ){
Return;
}
If (soundplayer! = NULL ){
Try {
Soundplayer. Stop ();
}
Catch (exception e ){
System. Out. Print (E. tostring ());
}
}
}

Public Boolean issoundplaying (){
Return soundplaying;
}

Public Boolean issoundenable (){
Return soundenable;
}

3. Read the mid File

1)
Import javax. microedition. Media .*;

2)

Player player;
Void initsound (){
Try {
Player = manager. createplayer (getstream ("/sound/B _main.mid "),
"Audio/Midi ");
Player. Realize ();
Player. setloopcount (100000 );
}
Catch (exception e ){
E. printstacktrace ();
}

}

3) // sound control in the program

M_playsound = (byte) (1-m_playsound );
If (m_playsound = 1 ){
Try {
Player. Start ();
}
Catch (exception e ){}
}
If (m_playsound = 0 ){
Try {
Player. Stop ();

}

Catch (exception e ){}
}

/// --------------- End

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.