相关文章推荐
无邪的蟠桃  ·  技术周刊 ...·  1 年前    · 
旅途中的保温杯  ·  pandas ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

AudioFlinger could not create track, status: -22 and Sample rate out of range: 441000 mSampleRate 44100

Ask Question

I am making a drum pad app which includes playing of lots (8) of .wav files.I tried soundpool for small .wav files as according to my knowledge soundpool is used for playing small files.But when i am trying to play the above error comes in my way.I am a beginner in android programming. Help is appreciated.

My logcat throws

12-30 17:33:03.825 166-488/? E/AudioFlinger: Sample rate out of range: 441000 mSampleRate 44100
12-30 17:33:03.825 1627-1627/com.govinddixit.drumpadchamp E/AudioTrack: AudioFlinger could not create track, status: -22
12-30 17:33:03.825 1627-1627/com.govinddixit.drumpadchamp E/SoundPool: Error creating AudioTrack

java main activity

package com.govinddixit.drumpadchamp;
import android.media.AudioManager;
import android.media.SoundPool;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
     SoundPool sp;
    private int sound1;
    private int sound2;
    private int sound3;
    private int sound4;
    private int sound5;
    private int sound6;
    private int sound7;
    private int sound8;
    private int sound9;
    private int sound00;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sp = new SoundPool(2, AudioManager.STREAM_MUSIC,0);
        sound1 = sp.load(getApplicationContext(),R.raw.sound1,1);
        sound2 = sp.load(getApplicationContext(),R.raw.sound2,1);
        sound3 = sp.load(getApplicationContext(),R.raw.sound3,1);
        sound4 = sp.load(getApplicationContext(),R.raw.sound4,1);
        sound5 = sp.load(getApplicationContext(),R.raw.sound5,1);
        sound6 = sp.load(getApplicationContext(),R.raw.sound6,1);
        sound7 = sp.load(getApplicationContext(),R.raw.sound7,1);
        sound8 = sp.load(getApplicationContext(),R.raw.sound8,1);
        sound9 = sp.load(getApplicationContext(),R.raw.sound9,1);
        sound00 = sp.load(getApplicationContext(),R.raw.sound00,1);
    public void playsound1 (View v) {
        sp.play(sound1,1.0f,1.0f,0,0,10f);
    public void playsound2 (View v) {
        sp.play(sound2,1.0f,1.0f,0,0,10f);
    public void playsound3 (View v) {
        sp.play(sound3,1.0f,1.0f,0,0,10f);
    public void playsound4 (View v) {
        sp.play(sound4,1.0f,1.0f,0,0,10f);
    public void playsound5 (View v) {
        sp.play(sound5,1.0f,1.0f,0,0,10f);
    public void playsound6 (View v) {
        sp.play(sound6,1.0f,1.0f,0,0,10f);
    public void playsound7 (View v) {
        sp.play(sound7,1.0f,1.0f,0,0,10f);
    public void playsound8 (View v) {
        sp.play(sound8,1.0f,1.0f,0,0,10f);
    public void playsound9 (View v) {
        sp.play(sound9,1.0f,1.0f,0,0,10f);

First of all, increase the maxStreams parameter or the maximum number of sounds that your app can simultaneously play in the excerpt of your code below, to enable playback of multiple streams in case the user of your app press more than 2 pads:

sp = new SoundPool(9, AudioManager.STREAM_MUSIC,0); 

Put 9 instead of 2. Nine (9) is the maxStreams parameter or the number of audio streams that can be played simultaneously in one soundPool. While it's unlikely that you will press the 9 pads at the same time, it is good to set the maxStreams equal to the number of buttons or pads on you app.

However this is not the cause of the error. The error was caused by a very high value of 10f for the play back rate. Range for playback rate is from 0.5 to 2.0, no more no less. The default value of the playback rate should be 1.0f. If you want to change the speed and pitch of the sounds, then you can play around with values from 0.5 to 2.0. See your first method with the correct play rate below:

public void playsound1 (View v) {
        sp.play(sound1, 1.0f, 1.0f, 0, 0, 1.0f);

I had the same problem when I was setting different values that I derived for the play rates that I am using to program and build the chromatic scale of my Strings and Piano musical keyboard app, where the reference key which is C4 octave is not shown on my keyboard layout, and the audio stream for C4 was loaded to B3 key for reference. I managed to solve the problem by using C3 as the reference note and normally applied the frequency ratio factor for G3# which is 1.6667f. I will not discuss the math in detail since the cause of your app's error is simpler and different from what I experienced.

I hope that the solution above solves your problem.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.