录制在AudioTrack上播放的音频?

时间:2014-12-04 14:48:53

标签: java android audio audiorecord audiotrack

我有一个Android应用程序在AudioTrack上播放生成的音调,所以我的问题是:有没有办法记录在该AudioTrack上播放的内容(它已经启动,停止,写入和频繁发布),能够播放它,并将其保存到音乐库?

播放音乐的课程:

package com.example.test;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Handler;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;

public class Afspl extends Activity {

    public DrawView vi;
    private Point size;
    Display disp;
    public int wide, high, cx, cy;
    boolean doPlay = false;
    Thread soundPlayer;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        disp = getWindowManager().getDefaultDisplay();
        try{
            disp.getSize(size);
            wide = size.x;
            high = size.y;
        }catch(Exception e){
            wide = disp.getWidth();
            high = disp.getHeight();
        }

        cx = 0; //tracks mouse x
        cy = 0; //tracks mouse y
        vi = new DrawView(getApplicationContext());
        setContentView(vi);
        soundPlayer = new Thread(new Runnable(){
            public void run(){
                int count = 0;
                while(true){
                    try{
                    Thread.sleep((long)(duration * 10));
                    }catch(Exception e){}
                    if(!doPlay){
                        if(count==1)track.stop();
                        continue;
                    }
                    int note = cy * 24 / high;
                    int freq = (int)(440 * Math.pow(1.059463, note));
                    genTone(freq);
                    if(count==0){
                    handler.post(new Runnable(){
                        public void run(){
                            playSound();
                        }
                    });
                    count++;
                    }else{
                        handler.post(new Runnable(){
                            public void run(){
                                track.release();
                                playSound();
                            }
                        });
                    }
                }
            }
        });
        soundPlayer.start();
    }

    class DrawView extends View{
        Paint paint = new Paint();

        public DrawView(Context context){
            super(context);
        }

        public void onDraw(final Canvas c){
            for(int i=0; i<24; i++){

            }
        }

        public String rgbToString(float r, float g, float b){
            return Integer.toHexString((int)(r * 256)) + Integer.toHexString((int)(g * 256)) + Integer.toHexString((int)(b * 256));
        }

    }

    public boolean onTouchEvent(MotionEvent me){
        int type = me.getActionMasked();
        switch(type){
        case MotionEvent.ACTION_DOWN:
            doPlay = true;
            cx = (int)me.getX();
            cy = (int)me.getY();
            return true;
        case MotionEvent.ACTION_UP:
            doPlay = false;
            return true;
        case MotionEvent.ACTION_MOVE:
            cx = (int)me.getX();
            cy = (int)me.getY();
            return true;
        default:
            return super.onTouchEvent(me);
        }
    }

    public double duration = 1;
    public int sampleRate = 8000;
    public int numSamples = (int)duration * sampleRate;
    public final double[] samples = new double[numSamples];
    public final byte[] generatedSnd = new byte[numSamples * 2];
    public Handler handler = new Handler();
    AudioTrack track;


    public void genTone(int freq){
        for(int i = 0; i<numSamples; i++){
            samples[i] = Math.pow(-1, i / (sampleRate/freq));
        }
        int idx = 0;
        int volume = 32767 * cx/wide;
        for (final double dVal : samples) {
            final short val = (short) ((dVal+1) * volume);

            generatedSnd[idx++] = (byte) (val & 0x00ff);
            generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);
        }
    }


    public void playSound(){
        track = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, AudioFormat.CHANNEL_OUT_MONO, AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length, AudioTrack.MODE_STATIC);
        track.write(generatedSnd, 0, generatedSnd.length);
        track.play();
    }


}

那么你能给我一些代码,让我能够记录在AudioTrack'轨道'上播放的内容,只需修改我已有的代码吗?谢谢(暂时忽略onDraw)。

编辑: 我想这会使问题更具体:如果我将一堆单独的音频样本写入ArrayList,我怎样才能将这些样本写入音乐文件并将其保存到音乐库中?

0 个答案:

没有答案