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

如何在Eclipse IDE for C/C++ Developers中正确编译GTK程序?(已解决)

出现错误信息如下,请问大牛该如何解决? 00:52:56 **** Incremental Build of configuration Debug for project Again **** ma...
  • acmerhlj
  • acmerhlj
  • 2014年10月01日 01:03
  • 1769

【APIs — A Strategy Guide】第一章 API的机遇

API是商业成功中的重要元素,并且正变得越来越重要。诸如Google、Facebook、Apple和twitter这些先驱公司已经向公众展现出很好的技术方案,用以转变现有商业模式并创建新的行业。这些公...
  • huawei_eSDK
  • huawei_eSDK
  • 2016年06月02日 09:30
  • 2410

android Audio设置音量流程及其binder通讯原理

Audio java部分代码流程(4.1.2 version):     在frameworks/base/media/java/android/media中:     IAudioService.a...
  • lushengchu2003
  • lushengchu2003
  • 2013年09月03日 10:11
  • 3625

CM的元数据恢复到数据后,启动server报错

启动server的时候报错如下: 2017-02-07 23:41:46,502 INFO main:com.cloudera.enterprise.dbutil.DbUtil: DB Schema...
  • BrotherDong90
  • BrotherDong90
  • 2017年02月08日 00:07
  • 1078

matlab中guide的搭建与进阶

matlab作为一款科学计算软件,拥有众多强大的函数库以及丰富的工具箱,比如神经网络工具箱,机器学习工具箱。simulink是强大的模块组建工具,guide则可以搭建用户友好界面。虽然python开放...
  • awakeljw
  • awakeljw
  • 2017年05月02日 16:16
  • 1553

android developers api guides 文档学习笔记(目录)

Android Developers GuideIntroduction to AndroidApplication FundamentalsDevice CompatibilitySystem Pe...
  • cizeal
  • cizeal
  • 2015年07月31日 17:31
  • 724

Spring 循环引用 ——理解singleton与prototype初始化的区别

spring的循环引用解决机制 以及singleton与prototype能否实现循环引用的原因。
  • u010723709
  • u010723709
  • 2015年08月02日 09:36
  • 5516

vxworks 用户开发手册 概述

Wind River VxWorks Platforms是一个完善的开发环境,包含了完整的开发工具。当然具体每个工具都有相应的用户操作手册,里面包含了详细的使用介绍。我把相关手册的介绍看了一遍,并总结...
  • u013026227
  • u013026227
  • 2017年07月03日 19:53
  • 668

Matlab GUIDE 控件使用总结

创建Matlab GUI界面通常有两种方式:    1,使用 .m 文件直接动态添加控件     2. 使用 GUIDE 快速的生成GUI界面 一:创建GUI 1.在...
  • shengmingqijiquan
  • shengmingqijiquan
  • 2016年04月25日 17:05
  • 3661

android activity生命周期的透彻理解

说来惭愧,虽然已经做了一年多的android开发,但是最近被人问起activity的生命周期的时候,却感觉自己并不能很自信很确定的回答对方的问题,对activity的生命周期的理解还不透彻啊。   ...
  • ldpxxx
  • ldpxxx
  • 2014年04月04日 22:20
  • 725
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Developers reference guide on creating audio
举报原因:
原因补充:

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