我在使用Android设备播放声音时遇到问题,我尝试使用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();
}
});
出现错误:
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)
我也尝试过使用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();
}
});
具有类似的输出:
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
我是否在指定资源方面做错了什么?我没有在任何地方声明它(这段代码与声音有关),因为我没有在任何地方看到它。
文件beep1.wav位于res / raw(我手动创建文件夹并将文件粘贴到那里)。
编辑:完整代码:
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;
}
}
答案 0 :(得分:1)
问题在于手机上的可用空间(我包括两个声音文件,其中一个很大(5MB),删除后声音很好)。