I'm having trouble playing sounds with my android device, I have tried to use MediaPlayer:
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.beep1);
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) { mp.release(); }
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
soundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
mp1.start();
}
});
Which got the error:
11-25 00:16:24.803: E/AudioFlinger(134): no more track names available
11-25 00:16:24.803: E/AudioTrack(134): AudioFlinger could not create track, status: -12
11-25 00:16:24.803: E/AudioSink(134): Unable to create audio track
11-25 00:16:24.803: E/MediaPlayer(2538): error (-19, 0)
11-25 00:16:24.813: E/MediaPlayer(2538): Error (-19,0)
I also tried using SoundPool:
final SoundPool soundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
final int sound = soundPool.load(this, R.raw.beep1, 1);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
soundPool.play(sound, 1.0f, 1.0f, 0, 0, 1.0f);
mp1.start();
}
});
Which has similar output:
11-25 00:29:12.163: E/AudioFlinger(134): no more track names available
11-25 00:29:12.163: E/AudioTrack(3355): AudioFlinger could not create track, status: -12
11-25 00:29:12.163: E/SoundPool(3355): Error creating AudioTrack
Am I doing anything wrong with specifying the resouce? I have not declared it anywhere (this code is all that is related to sounds), because I have not seen it mentioned anywhere.
The file beep1.wav is located in res/raw (I created the folder manually and pasted the file there).
EDIT: the full code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button)findViewById(R.id.sound_btn);
final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.beep1);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
mp1.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp1.start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
The problem was with available space on the phone (I included two sound files, one of them was large (5MB) and after deleting it, the sound plays fine).