java - Could not find class 'android.media.AudioAttributes$Builder' -
so i'm implementing playing sounds game i'm working on. game supports api 8 latest 21. i'm using soundpool play , handle sounds seems api 21, have set audioattributes soundpool.
i getting following error:
05-15 13:56:48.202 26245-26245/thedronegame.group08.surrey.ac.uk.thedronegame e/dalvikvm﹕ not find class 'android.media.audioattributes$builder', referenced method thedronegame.group08.surrey.ac.uk.thedronegame.sound.initializerecentapisoundpool
sound class
<pre>package thedronegame.group08.surrey.ac.uk.thedronegame; import android.annotation.targetapi; import android.content.context; import android.media.audioattributes; import android.media.audiomanager; import android.media.soundpool; import android.media.mediaplayer; import android.os.build; import android.util.log; /** * created michael on 19/03/15. */ public class sound { /** * sound pool */ private soundpool soundpool = null; /** * current sound. */ private int sound = 0; /** * false boolean. */ private boolean loaded = false; /** * context. */ private context context = null; /** * audio manager. */ private audiomanager audiomanager = null; /** * literal volume. */ private float literalvolume = 0; /** * maximum volume. */ private float maximumvolume = 0; /** * volume. */ private float volume = 0; /** * constructor creating new sound object */ public sound(context context) { this.context = context; this.audiomanager = (audiomanager) context.getsystemservice(context.audio_service); // set literal volume audio manager. this.literalvolume = (float) audiomanager.getstreamvolume(audiomanager.stream_music); // set maximum volume audio manager. this.maximumvolume = (float) audiomanager.getstreammaxvolume(audiomanager.stream_music); // set volume gamesound pool. this.volume = literalvolume / maximumvolume; } /**
* initialize soundpool later api versions */ @targetapi(build.version_codes.lollipop) private void initializerecentapisoundpool() { // create audioattributes. audioattributes attributes = new audioattributes.builder() .setusage(audioattributes.usage_game) .setcontenttype(audioattributes.content_type_sonification) .build(); this.soundpool = new soundpool.builder() .setaudioattributes(attributes) .setmaxstreams(7) .build(); } /**
* intialize soundpool older api
versions */ @suppresswarnings("deprecation") private void initializedeprecatedapisoundpool() { // initialize soundpool. this.soundpool = new soundpool(1, audiomanager.stream_music,0); } /**
* load sounds soundpool.
*/ private void loadintosoundpool() { //todo: finish loadintosoundpool() method // loads sounds array // sound 0. this.soundpool.load(this.context, r.raw.blip_select2, 0); // sound 1. //this.soundpool.load(context, r.raw.sound, 1); } /** * set initial soundpool.
* call method differs dependent on api version.
*/ public void setinitialsoundpool() { // initialize soundpool, call specific dependent on sdk version if (build.version.sdk_int >= build.version_codes.lollipop) { initializerecentapisoundpool(); } else { initializedeprecatedapisoundpool(); } this.soundpool.setonloadcompletelistener(new soundpool.onloadcompletelistener() { @override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { loaded = true; soundpool.load(context, r.raw.blip_select2, 0); } }); // load sounds sound pool resources. //this.loadintosoundpool(); } /** * plays sound * @param id - sound id * @param context - context */ public void playsound(int id, final context context) { // create audio manager using context. soundpool.play(id, this.volume, this.volume, 1, 0, 1f); // play gamesound gamesound pool defined volumes. log.e("soundpool", "game gamesound played"); } }</code>
does guys meet issue?any appreciated.
thank david wasser :)
i share final implementation works api 21 , lower.
as found when building soundpool api 21 + have first create audioattributes:
log.d("sound", "initialize audio attributes."); // initialize audioattributes. audioattributes attributes = new audioattributes.builder() .setusage(audioattributes.usage_game) .setcontenttype(audioattributes.content_type_sonification) .build();
following building audioattributes, initialise soundpool sound.builder:
log.d("sound", "set audioattributes soundpool."); // set audioattributes soundpool , specify maximum number of streams. soundpool = new soundpool.builder() .setaudioattributes(attributes) .setmaxstreams(7) .build();
please note david wasser pointed out, audioattributes must in separate class of default class handing sound audioattributes not known api 21 - compatible devices , report in error. created class api 21 soundpool , audioattributes implementation:
soundrecent.java
import android.annotation.targetapi; import android.media.audioattributes; import android.media.soundpool; import android.os.build; import android.util.log; /** * created michaelstokes on 17/05/15. */ public class soundrecent { /**
* initialize soundpool later api versions */ @targetapi(build.version_codes.lollipop) public soundpool initializerecentapisoundpool(soundpool soundpool) { log.d("sound", "initialize audio attributes."); // initialize audioattributes. audioattributes attributes = new audioattributes.builder() .setusage(audioattributes.usage_game) .setcontenttype(audioattributes.content_type_sonification) .build(); log.d("sound", "set audioattributes soundpool."); // set audioattributes soundpool , specify maximum number of streams. soundpool = new soundpool.builder() .setaudioattributes(attributes) .setmaxstreams(7) .build(); return soundpool; } }
**for original soundclass checking of devices api version , appropriate initialiser called: **
package thedronegame.group08.surrey.ac.uk.thedronegame; import android.annotation.targetapi; import android.content.context; import android.media.audiomanager; import android.media.soundpool; import android.os.build; import android.util.log; /** * created michael on 19/03/15. */ public class sound { /** * sound pool */ private soundpool soundpool = null; /** * current sound. */ private int sound = 0; /** * false boolean. */ private boolean loaded = false; /** * context. */ private context context = null; /** * audio manager. */ private audiomanager audiomanager = null; /** * literal volume. */ private float literalvolume = 0; /** * maximum volume. */ private float maximumvolume = 0; /** * volume. */ private float volume = 0; /** * constructor creating new sound object */ public sound(context context) { this.context = context; this.audiomanager = (audiomanager) context.getsystemservice(context.audio_service); // set literal volume audio manager. this.literalvolume = (float) audiomanager.getstreamvolume(audiomanager.stream_music); // set maximum volume audio manager. this.maximumvolume = (float) audiomanager.getstreammaxvolume(audiomanager.stream_music); // set volume gamesound pool. this.volume = literalvolume / maximumvolume; } /**
* initialize soundpool later api versions */ @targetapi(build.version_codes.lollipop) private void initializerecentapisoundpool() { log.d("soundpool", "initialize recent api sound pool"); this.soundpool = new soundrecent().initializerecentapisoundpool(this.soundpool); } /**
* intialize soundpool older api
versions */ @suppresswarnings("deprecation") private void initializedeprecatedapisoundpool() { // initialize soundpool. this.soundpool = new soundpool(1, audiomanager.stream_music, 0); } /**
* load sounds soundpool.
*/ private void addsoundstosoundpool() { log.d("sound", "load sounds."); int soundid = 0; // load sound 1 soundid = soundpool.load(context, r.raw.blip_select2, 1); log.d("sound", "sound " + soundid + " loaded."); // load sound 2 soundid = soundpool.load(context, r.raw.pickup_coin3, 1); log.d("sound", "sound " + soundid + " loaded."); // load sound 3 soundid = soundpool.load(context, r.raw.explosion2, 1); log.d("sound", "sound " + soundid + " loaded."); // load sound 4 soundid = soundpool.load(context, r.raw.powerup8, 1); log.d("sound", "sound " + soundid + " loaded."); // load sound 5 soundid = soundpool.load(context, r.raw.powerup15, 1); log.d("sound", "sound " + soundid + " loaded."); // load sound 6 soundid = soundpool.load(context, r.raw.jump9, 1); log.d("sound", "sound " + soundid + " loaded."); // load sound 7 soundid = soundpool.load(context, r.raw.powerup18, 1); log.d("sound", "sound " + soundid + " loaded."); } /** * set initial soundpool.
* call method differs dependent on api version.
*/ public void setinitialsoundpool() { log.d("sound", "initialize recent or deprecated api soundpool."); // initialize soundpool, call specific dependent on sdk version if (build.version.sdk_int >= build.version_codes.lollipop) { log.d("sound", "initialize recent api soundpool."); initializerecentapisoundpool(); } else { log.d("sound", "initialize old api soundpool."); initializedeprecatedapisoundpool(); } this.soundpool.setonloadcompletelistener(new soundpool.onloadcompletelistener() { @override public void onloadcomplete(soundpool soundpool, int sampleid, int status) { loaded = true; } }); // add , load sounds soundpool. this.addsoundstosoundpool(); } /** * plays sound * @param id - sound id * @param context - context */ public void playsound(int id, final context context) { log.d("sound", "play gamesound " + id + 1 + "."); soundpool.play(id + 1, this.volume, this.volume, 1, 0, 1f); log.d("sound", "gamesound " + id + 1 + " played"); } }
Comments
Post a Comment