Android设备不支持.wav格式

时间:2015-02-10 05:50:51

标签: android

这是我的示例项目,我想开发示例鼓应用,当按下八个单独的按钮时播放八个声音文件。编码工作正常,但音调不清楚,如何获得原始音调? ,我的代码中有错误吗?

DrumsActivity.java

public class DrumsActivity extends Activity
{
private AudioTrackSoundPlayer soundPlayer = null;
private ArrayList<View> buttons = null;
private boolean[] buttonStates = null;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    buttons = new ArrayList<View>();
    buttons = new ArrayList<View>();
    buttons.add(findViewById(R.id.c));
    buttons.add(findViewById(R.id.d));
    buttons.add(findViewById(R.id.f));
    buttons.add(findViewById(R.id.g));
    buttons.add(findViewById(R.id.c1));
    buttons.add(findViewById(R.id.d1));
    buttons.add(findViewById(R.id.f1));
    buttons.add(findViewById(R.id.g1));

    buttonStates = new boolean[buttons.size()];
    soundPlayer = new AudioTrackSoundPlayer(this);
}

@Override
public boolean onTouchEvent(MotionEvent event)
{
    boolean[] newButtonStates = new boolean[8];

    int action = event.getAction();
    boolean isDownAction = (action & 0x5) == 0x5 || action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE;

    for (int touchIndex = 0; touchIndex < event.getPointerCount(); touchIndex++)
    {
        // find button for location
        int x = (int) event.getX(touchIndex);
        int y = (int) event.getY(touchIndex);

        for (int buttonIndex = 0; buttonIndex < buttons.size(); buttonIndex++)
        {
            View button = buttons.get(buttonIndex);
            int[] location = new int[2];
            button.getLocationOnScreen(location);
            int buttonX = location[0];
            int buttonY = location[1];

            Rect rect = new Rect(buttonX, buttonY, buttonX + button.getWidth(), buttonY + button.getHeight());
            if (rect.contains(x, y))
            {
                newButtonStates[buttonIndex] = isDownAction;
                break;
            }
        }
    }

    for (int index = 0; index < newButtonStates.length; index++)
    {
        if (buttonStates[index] != newButtonStates[index])
        {
            buttonStates[index] = newButtonStates[index];
            View button = buttons.get(index);
            toggleButtonSound(button, newButtonStates[index]);
        }
    }

    return true;
}

private void toggleButtonSound(View button, boolean down)
{
    String note = null;

    switch (button.getId())
    {
        case R.id.c:
            note = "crash";
            break;

        case R.id.d:
            note = "hithat";
            break;

        case R.id.f:
            note = "snare";
            break;

        case R.id.g:
            note = "giant";
            break;

        case R.id.c1:
            note = "c";
            break;

        case R.id.d1:
            note = "d";
            break;

        case R.id.f1:
            note = "f";
            break;

        case R.id.g1:
            note = "tom";
            break;
    }

    if (down)
    {
        if (!soundPlayer.isNotePlaying(note)) 
        {
            soundPlayer.playNote(note);
        }
    }
    else
    {
        soundPlayer.stopNote(note);
    }
}

}

AudioTrackSoundPlay.java

public class AudioTrackSoundPlayer

{
private HashMap<String, PlayThread> threadMap = null;
private Context context;

public AudioTrackSoundPlayer(Context context)
{
    this.context = context;
    threadMap = new HashMap<String, AudioTrackSoundPlayer.PlayThread>();
}
public void playNote(String note)
{
    if (!isNotePlaying(note))
    {
        PlayThread thread = new PlayThread(note);
        thread.start();
        threadMap.put(note, thread);
    }
}
public void stopNote(String note)
{
    PlayThread thread = threadMap.get(note);
    if (thread != null)
    {
        thread.requestStop();
        threadMap.remove(note);
    }
}
public boolean isNotePlaying(String note)
{
    return threadMap.containsKey(note);
}

private class PlayThread extends Thread
{
    String note;
    boolean stop = false;
    AudioTrack audioTrack = null;

    public PlayThread(String note)
    {
        super();
        this.note = note;
    }

    public void run()
    {
        try
        {
            String path = note + ".wav";

            AssetManager assetManager = context.getAssets();
            AssetFileDescriptor ad = assetManager.openFd(path);
            long fileSize = ad.getLength();
            int bufferSize = 4096;
            byte[] buffer = new byte[bufferSize];

            audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, 22050, AudioFormat.CHANNEL_CONFIGURATION_MONO, AudioFormat.ENCODING_PCM_16BIT,bufferSize, AudioTrack.MODE_STREAM);
            //audioTrack = new AudioTrack(android.media.MediaPlayer);
            audioTrack.play();

            InputStream audioStream = null;

            int headerOffset = 0x2C;
            long bytesWritten = 0;
            int bytesRead = 0;

            while (!stop)
            {
                audioStream = assetManager.open(path);
                bytesWritten = 0;
                bytesRead = 0;
                audioStream.read(buffer, 0, headerOffset);
                while (!stop && bytesWritten < fileSize - headerOffset)
                {
                    bytesRead = audioStream.read(buffer, 0, bufferSize);
                    bytesWritten += audioTrack.write(buffer, 0, bytesRead);
                }
            }

            audioTrack.stop();
            audioTrack.release();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }

    }

    public synchronized void requestStop()
    {
        stop = true;
    }
}

}

0 个答案:

没有答案