我正在创建一个声卡,点击大约30种不同的声音后它就会停止工作;我相信android的内存不足。以下是我的代码。我怎样才能实现.release(),这样当声音完成播放时它会被释放?如果两件事同时发挥,我真的不在乎;这些剪辑是可能的。我只想得到我的代码集。
public class soundPageOne extends Activity {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final MediaPlayer pg1 = MediaPlayer.create(this, R.raw.peter1);
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pg1.start();
}
});
我做了很多搜索,但由于我缺乏java / android知识,我无法得到任何工作。在此先感谢,如果有人需要更多代码,请告诉我。
答案 0 :(得分:2)
我发表评论,但我会发一个答案来表明我的意思......
您的想法是,您可以使用一定数量的MediaPlayer
个实例。这样你永远不会超过最大实例数。数组应该是您希望能够听到的并发声音数量的长度。如果声音是本地文件,那么准备声音所需的时间几乎可以忽略不计,因此在单击处理程序中调用create
不会导致可怕的性能。我想,每个按钮都与特定资源相关联,因此我设置了一个帮助方法,以相同的方式为每个按钮创建和播放声音。
public class soundPageOne extends Activity {
private MediaPlayer[] mPlayers = new MediaPlayer[2];
private int mNextPlayer = 0;
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
Button playSound1 = (Button)this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSound(R.raw.peter1);
}
});
}
public void onDestroy() {
super.onDestroy(); // <---------------------- This needed to be there
for (int i = 0; i < mPlayers.length; ++i)
if (mPlayers[i] != null)
try {
mPlayers[i].release();
mPlayers[i] = null;
}
catch (Exception ex) {
// handle...
}
}
private void startSound(int id) {
try {
if (mPlayers[mNextPlayer] != null) {
mPlayers[mNextPlayer].release();
mPlayers[mNextPlayer] = null;
}
mPlayers[mNextPlayer] = MediaPlayer.create(this, id);
mPlayers[mNextPlayer].start();
}
catch (Exception ex) {
// handle
}
finally {
++mNextPlayer;
mNextPlayer %= mPlayers.length;
}
}
}
答案 1 :(得分:1)
创建一个类,用AudioPlayer
变量说SoundPool
。设置构造函数以初始化AudioPlayer
对象并创建Play
方法。 SoundPool可以更好地播放多次播放的短音,并且不需要您释放。
public class AudioPlayer {
private SoundPool sPool = new SoundPool(Integer.MAX_VALUE, AudioManager.STREAM_MUSIC, 0);
public AudioPlayer(Context c, int id){
sounds.put("1",sPool.load(c, id, 1));
}
public void play(Context c) {
sPool.play("1", 1, 1, 1, 0, 1f);
}
}
所以你的课应该是
public class soundPageOne extends Activity {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final AudioPlayer ap = new AudioPlayer(this, R.raw.sound);
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ap.play();
}
});
答案 2 :(得分:1)
您可以使用MediaPlayer.OnCompletionListener吗?
类似的东西:
public class soundPageOne extends Activity implements MediaPlayer.OnCompletionListener {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final MediaPlayer pg1 = MediaPlayer.create(this, R.raw.peter1);
//***set the listener here***
pg1.setOnCompletionListener(this);
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pg1.start();
}
});
}
//***this code will be executed once the sound finishes playing***
@Override
public void onCompletion(MediaPlayer mp) {
//log messages, other things can go here
mp.release();
}
答案 3 :(得分:0)
尝试这样的事情
您的活动类:
public class soundPageOne extends Activity {
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
setContentView(R.layout.soundsone);
final AudioPlayer pg1 = new AudioPlayer();
Button playSound1 = (Button) this.findViewById(R.id.peter1Button);
playSound1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
pg1.play(this, R.raw.sound);
}
});
}
这是另一个Java类:
public class AudioPlayer {
private MediaPlayer mPlayer;
public void stop() {
if (mPlayer != null) {
mPlayer.release();
mPlayer = null;
}
}
public void play(Context c, int sound) {
stop();
mPlayer = MediaPlayer.create(c, sound);
mPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
stop();
}
});
mPlayer.start();
}
public boolean isPlaying() {
return mPlayer != null;
}
}