【发布时间】:2015-05-22 19:26:15
【问题描述】:
所以我目前正在为我正在开发的游戏实现播放声音。现在游戏支持 API 8 到最新的 21。我正在使用 SoundPool 播放和处理声音,但似乎使用 API 21,您必须为 SoundPool 设置 AudioAttributes。
我目前收到以下错误:
05-15 13:56:48.202 26245-26245/thedronegame.group08.surrey.ac.uk.thedronegame E/dalvikvm﹕ 找不到类“android.media.AudioAttributes$Builder”, 从方法引用 thedronegame.group08.surrey.ac.uk.thedronegame.Sound.initializeRecentAPISoundPool
声音类
<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 by Michael on 19/03/15.
*/
public class Sound {
/**
* The sound pool
*/
private SoundPool soundPool = null;
/**
* The current Sound.
*/
private int sound = 0;
/**
* false Boolean.
*/
private boolean loaded = false;
/**
* The 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;
/**
* A constructor for creating a new Sound object
*/
public Sound(Context context) {
this.context = context;
this.audioManager = (AudioManager) context.getSystemService(context.AUDIO_SERVICE);
// Set Literal Volume for Audio Manager.
this.literalVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
// Set Maximum Volume for Audio Manager.
this.maximumVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
// Set volume for GameSound Pool.
this.volume = literalVolume / maximumVolume;
}
/**
* Initialize the SoundPool for 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 for older API
versions
*/
@SuppressWarnings("deprecation")
private void initializeDeprecatedAPISoundPool() {
// Initialize SoundPool.
this.soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC,0);
}
/**
* Load Sounds into the SoundPool.
*/
private void loadIntoSoundPool() {
//todo: finish loadIntoSoundPool() method
// Loads all sounds from array
// Sound 0.
this.soundPool.load(this.context, R.raw.blip_select2, 0);
// Sound 1.
//this.soundPool.load(context, R.raw.sound, 1);
}
/**
* Set the initial SoundPool.
* Call to 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 into sound pool from resources.
//this.loadIntoSoundPool();
}
/**
* Plays the sound
* @param id - the sound id
* @param context - the 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 from GameSound Pool with defined Volumes.
Log.e("SoundPool", "Game GameSound Played");
}
}</code>
有没有人遇到这个问题?任何帮助将不胜感激。
【问题讨论】:
-
有点题外话:我看到这是一个学校项目,您的教授是否要求您的全局变量使用 javadoc?如果没有,请摆脱那些。现在他们没有提供比你的声明已经显示的更多的信息。如果是,您应该改进它们。
标签: java android api audio classloader