Developers reference guide on creating audio

原创 2004年08月31日 23:17:00

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

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;
}

SpriteKit Programming Guide手游开发之(FAKE STICK HERO)

课程背景: 使用O-C语言可快速进行开发 SpriteKit游戏引擎是iOS专用的2D游戏引擎 手游开发最虐心游戏之(FAKE STICK HERO) 核心内容: SpriteKit游戏项目的创建 SpriteKit中场景设计工具、物理引擎 课程等级:中级课程 适应群体: 1.熟悉O-C语言的开发者 2.有经验的iOS开发者
  • 2015年03月23日 16:44

Wiley.World.of.Warcraft.Programming.A.Guide.and.Reference.for.Creating.WoW.Addons.Second.Edition

  • 2010年09月30日 12:34
  • 8.66MB
  • 下载

spring OAuth 2 Developers Guide 英译汉

Introduction This is the user guide for the support for OAuth 2.0. For OAuth 1.0, everything is d...
  • shunzi1046
  • shunzi1046
  • 2017-05-02 17:16:00
  • 585

Flex 3 Developer’s Guide

  • 2010年03月02日 22:27
  • 13.88MB
  • 下载

ObjectARX .NET Developer's Guide

  • 2011年04月27日 12:01
  • 625KB
  • 下载

Apache Spark 2.X for Java Developers epub

  • 2017年09月30日 14:57
  • 3.43MB
  • 下载

Android_Developer_Guide 中文

  • 2013年05月25日 12:03
  • 1024KB
  • 下载

Flex中的嵌入资源(Embedding Assets)——Swingguy摘译自Flex 3 Developer's Guide,转载请注明

Flex中的嵌入资源(Embedding Assets)——Swingguy摘译自Flex 3 Developers Guide,转载请注明2007-10-27 17:18 ...
  • shangmw
  • shangmw
  • 2008-07-09 12:34:00
  • 405

《OpenOffice.org Developer's Guide》笔记之First Step

OpenOffice开发指南官方网站Scalcs例子Programming With UNOUNO([ju:nou])是Universal Network Objects的简写,is the base...
  • andrew_85
  • andrew_85
  • 2010-03-23 18:47:00
  • 1307

Wireshark Developer's Guide

  • 2009年11月22日 11:14
  • 212KB
  • 下载
收藏助手
不良信息举报
您举报文章:Developers reference guide on creating audio
举报原因:
原因补充:

(最多只允许输入30个字)