分享
 
 
 

Developers reference guide on creating audio

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

Developers reference guide on creating audio SFX, melodies and vibration on Nokia, Motorola, Sharp, Sagem and Sony mobile devices.

Introduction

How many times have you thought your software works on a specific mobile device, because it works in the PC-emulator, only to find that once you try it on an actual device it crashes without any explanation why!

This guide will help software developers create 100% working FX on Nokia, Motorola, Sharp, Sagem and Sony mobile devices. This practical guide will include full-code for each device.

The most common FX required whilst developing mobile game software is the ability to:

Play melodies

Play wave-audio

Vibrate

Although it is generally possible to create J2ME software that will work on all devices* it is impossible to create generic code that will activate these FX.

*Apart from Nokia full screen mode, Motorola reverse soft-keys and Nokia 3650 alternative key layout.

If the device you need is not listed below, please email me dan@jcodeworks.com and I will consider adding it.

FX Reference Guide

Please note: Although it might be possible to achieve melody, wave-audio and vibrate using different types of file-format I will only list one. The format described will (a) work!!! (b) use the least amount of processor time and (c) use the least amount of resources.

Devices:

Nokia 3650, 6600 and N-Gage

Nokia 7650

Motorola V525 and V600

Sagem myV65 and myV75

Sharp GX10, GX10i and GX20*

Sony-Ericcson T6nn and Z600

*coming soon

Nokia 3650, 6600 and N-Gage

API: javax.microedition.media.*

Restriction: Only play 1 FX at a time.

NOTE: You can add a listener that will inform you of Player events (e.g. Stopped - when Player finishes FX). To do this use: player.addPlayerListener(*class*). The specified *class* must implement PlayerListener.

To catch the Listener events, use the following method inside *class*:

public void playerUpdate(Player player, String event, Object obj) {

//player indicates which Player caused the event

//event informs you of event type

}

Also: I have not tried PlayerListener on Nokia 6600 and N-Gage and so, cannot confirm that it works properly.

- - -

Melody: Available - plays .mid files (format zero)

The great thing about MIDI files is that you can use your favourite professional audio sequencing package to create them. It's the standard of all standards in the music creation world.

To LOAD a MIDI file:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("melody.mid"),"audio/midi");

player.realize();

player.prefetch(); //Remove this line for Nokia 6600 and N-Gage

} catch (Exception e0) {}

To PLAY the melody, ensure that you STOP all other players:

try {

player.stop();

catch (Exception e1) {}

… and then play your required melody with:

try {

player.prefetch(); //Remove this line for Nokia 3650

player.setMediaTime(0); //Remove this for Nokia 6600 and N-Gage

player.setLoopCount(n); //n=number of times. -1=forever.

player.start();

catch (Exception e2) {}

Note: Playing a melody does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

To clean-up a melody resource:

if (player != null) {

try {

player.close();

} catch (Exception e3) {}

player = null;

}

- - -

Wave Audio: Available - plays .amr files (8 bit, rate can vary depending on quality required)

AMR files are a lot smaller than .WAV files. To create AMR files use the MIKSOFT Mobile AMR converter. Check it out at www.miksoft.8m.com

To LOAD a wave-audio:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.amr"),"audio/amr");

player.realize();

player.prefetch();

} catch (Exception e0) {}

To PLAY the audio, ensure that you STOP all other players:

try {

player.stop();

catch (Exception e1) {}

… and then play your required wave-audio with:

try {

player.setMediaTime(0);

player.start();

catch (Exception e2) {}

Note: Calling the start() method does seem to take some time before calling back. So you will need to place it in a separate thread to the main game code.

To clean-up a wave-audio resource:

if (player != null) {

try {

player.close();

} catch (Exception e3) {}

player = null;

}

- - -

Vibrate: Not available.

Nokia 7650

API: com.nokia.mid.sound.*

Restriction: Only play 1 FX at a time.

NOTE: There does NOT seem to be any Sound listeners implemented on this device, so you will need to create your own means of determining if the Sound has finished using an elapsed time clock counter.

- - -

Melody: Not Available.

- - -

Wave Audio: Available – plays .WAV files (8bit, 8khz)

WARNING: I have found that short wave-audio (e.g. under .7 of a second) do not play properly e.g. produce clicking noises or don't play at all.

To LOAD a wave-audio:

import com.Nokia.sound.*;

. . . .

byte[] bytes = readDataFromJar("beep.wav");

Sound sound;

if (bytes != null)

sound = new Sound(bytes, Sound.FORMAT_WAV);

. . . .

//utility method used by code above…

public byte[] readDataFromJar(String name) {

//get data from JAR resource

ByteArrayOutputStream bout;

InputStream in;

byte[] res = null;

try {

in = *canvas ref*.getClass().getResourceAsStream(name);

bout = new ByteArrayOutputStream();

for (int ret = in.read(); ret >= 0; ret = in.read())

bout.write(ret);

res = bout.toByteArray();

} catch (Exception e) {}

return res;

}

To PLAY the audio, ensure that you STOP all other wave-audio:

if (sound != null)

sound.stop();

… and then play your required wave-audio with:

if (sound != null)

sound.play(n); //n = number of repeats, e.g. 1

Note: Calling the play() method does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

To clean-up a wave-audio resource:

if (sound != null) {

sound.stop();

sound = null;

}

BUG NOTIFICATION: Some older 7650 firmware versions (version number unknown) crashes the application if you use the command 'setGain'. Once the application terminates, the device will display an error notification: jes-8e-javax.microedition.lcdui0@ 1...CMdaAudioPlayerU 2 (or similar - depending on firmware version). The application will crash even if you enclose the command with a try/catch exception handler.

- - -

Vibrate: Not available.

Motorola V525 and V600

API: Use javax.microedition.media.* for melody and wave-audio.

Use the MIDP2 display.vibrate command for vibration.

Restriction: Only play 1 FX at a time.

NOTE: You can add a listener that will inform you of Player events (e.g. Stopped - when Player finishes FX). To do this use: player.addPlayerListener(*class*). The specified *class* must implement PlayerListener.

To catch the Listener events, use the following method inside *class*:

public void playerUpdate(Player player, String event, Object obj) {

//player indicates which Player caused the event

//event informs you of event type

}

- - -

Melody: Available - plays .mid files (format zero)

The great thing about MIDI files is that you can use your favourite professional audio sequencing package to create them. It's the standard of all standards in the music creation world.

To LOAD a MIDI file:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("melody.mid"),"audio/midi");

player.realize();

} catch (Exception e0) {}

To PLAY the melody, ensure that you STOP and DEALLOCATE all other players:

try {

player.stop();

catch (Exception e1) {}

try {

player.deallocate();

catch (Exception e2) {}

… and then play your required melody with:

try {

player.prefetch();

catch (Exception e3) {}

try {

player.setMediaTime(0);

catch (Exception e4) {}

player.setLoopCount(n); //n=number of times. -1=forever.

try {

player.start();

catch (Exception e5) {}

Note: Calling the start() method does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

To clean-up a melody resource:

if (player != null) {

try {

player.close();

} catch (Exception e6) {}

player = null;

}

- - -

Wave Audio: Available - plays .amr files (8 bit, rate can vary depending on quality required)

AMR files are a lot smaller than .WAV files. To create AMR files use the MIKSOFT Mobile AMR converter. Check it out at www.miksoft.8m.com

To LOAD a wave-audio:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.amr"),"audio/amr");

player.realize();

player.prefetch();

} catch (Exception e0) {}

To PLAY the wave-audio, ensure that you STOP and DEALLOCATE all other players:

try {

player.stop();

catch (Exception e1) {}

try {

player.deallocate();

catch (Exception e2) {}

… and then play your required wave-audio with:

try {

player.prefetch();

catch (Exception e3) {}

try {

player.setMediaTime(0);

catch (Exception e4) {}

try {

player.start();

catch (Exception e5) {}

Note: Calling the start() method does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

To clean-up a wave-audio resource:

if (player != null) {

try {

player.close();

} catch (Exception e6) {}

player = null;

}

- - -

Vibrate: Available.

Use the MIDP2 built-in display class command:

*display*.vibrate(duration); //duration is in milliseconds

Sagem myV65 and myV75

API: javax.microedition.media.*

Restriction: Only play 1 FX at a time.

NOTE: There does NOT seem to be any Player listeners implemented on this device, so you will need to create your own means of determining if the Player has finished using an elapsed time clock counter.

- - -

Melody: ***To be fully investigated***

Although it is possible to play MIDI files it is NOT 100% compatible. For example: I have not been able to play drum tracks. I need to investigate this in more depth and I will then write up my discoveries.

- - -

Wave Audio: Available – plays .WAV files (8bit, 8khz)

WARNING: The Sagem is extremely fussy about wave-audio. You must only have 1 Player pre-fetched at any one time.

To LOAD a wave-audio:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.wav"),"audio/x-wav");

player.realize();

} catch (Exception e0) {}

To PLAY the audio, ensure that you STOP and DEALLOCATE all other players:

try {

player.stop();

catch (Exception e1) {}

try {

player.deallocate();

catch (Exception e2) {}

… and then play your required wave-audio with:

try {

player.prefetch();

catch (Exception e3) {}

try {

player.setMediaTime(0);

catch (Exception e4) {}

try {

player.start();

catch (Exception e5) {}

Note: Calling the stop(), deallocate(), prefetch() and start() methods does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

To clean-up a wave-audio resource:

if (player != null) {

try {

player.close();

} catch (Exception e5) {}

player = null;

}

As I mentioned earlier, the Sagem device is extremely fussy about wave-audio, and so I have tried to catch every possible problem with an exception. Remove the try/catches if you dare, but don't be too surprised if you start getting unexpected crashes!

- - -

Vibrate: Not available.

Sony-Ericsson T6nn and Z600

API: javax.microedition.media.*

Restriction: Only play 1 FX at a time.

NOTE: You can add a listener that will inform you of Player events (e.g. Stopped - when Player finishes FX). To do this use: player.addPlayerListener(*class*). The specified *class* must implement PlayerListener.

To catch the Listener events, use the following method inside *class*:

public void playerUpdate(Player player, String event, Object obj) {

//player indicates which Player caused the event

//event informs you of event type

}

- - -

Melody: Available - plays .mid files (format zero)

The great thing about MIDI files is that you can use your favourite professional audio sequencing package to create them. It's the standard of all standards in the music creation world.

To LOAD a MIDI file:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("melody.mid"),"audio/midi");

player.realize();

} catch (Exception e0) {}

To PLAY the melody, ensure that you STOP all other players:

try {

if (player.getState() != Player.CLOSED)

player.stop();

catch (Exception e1) {}

… and then play your required melody with:

try {

player.prefetch();

player.setLoopCount(n); //n=number of times. -1=forever.

player.start();

catch (Exception e2) {}

Note: Calling the prefetch() method does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

To clean-up a melody resource:

if (player != null) {

try {

player.close();

} catch (Exception e3) {}

player = null;

}

- - -

Wave Audio: Available - plays .amr files (8 bit, rate can vary depending on quality required)

AMR files are a lot smaller than .WAV files. To create AMR files use the MIKSOFT Mobile AMR converter. Check it out at www.miksoft.8m.com

Note: Many thanks to David Harries for revising the wave audio code.

To LOAD a wave-audio:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer(*canvas ref*.getClass().getResourceAsStream("beep.amr"),"audio/amr");

player.realize();

} catch (Exception e0) {}

player.prefetch();

To PLAY the audio, ensure that you STOP all other players:

try {

if (player.getState() != Player.PREFETCHED)

player.stop();

catch (Exception e1) {}

… and then play your required wave-audio with:

try {

player.setLoopCount(1);

player.start();

catch (Exception e2) {}

Note: The prefetch() method does seem to take some time before calling back. So if you need to prefetch on the fly in a tight game loop you will need to place it in a separate thread to the main game code.

To clean-up a wave-audio resource:

if (player != null) {

try {

player.close();

} catch (Exception e3) {}

player = null;

}

- - -

Vibrate: Available - using .imy file

To LOAD a Vibrate player:

import javax.microedition.media.*;

. . . .

Player player;

try {

player = Manager.createPlayer( *canvas ref*.getClass().getResourceAsStream("vibrate.imy"),"audio/iMelody");

player.realize();

player.prefetch();

} catch (Exception e0) {}

The VIBRATE.IMY is a text file with the following lines of code (to change the vibration delay alter the settings in the MELODY line):

BEGIN:IMELODY

VERSION:1.2

FORMAT:CLASS1.0

BEAT:200

STYLE:S1

MELODY:vibeonr5vibeoff

END:IMELODY

To START the vibration, ensure that you STOP all other players:

try {

if (player.getState() != Player.CLOSED)

player.stop();

catch (Exception e1) {}

… and then START the vibration with:

try {

player.start();

catch (Exception e2) {}

Note: Calling the start() method does NOT seem to cause any serious delays. So you will NOT need to place it in a separate thread to the main game code.

To clean-up a vibration resource:

if (player != null) {

try {

player.close();

} catch (Exception e3) {}

player = null;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有