在Android中播放时录制声音并改变音高?

我正在做一个项目来录制声音并以不同的调制方式播放.我搜索了整个网络,但我找不到解决方案.我已经经历了this example,但它没有给出解决方案.任何人都可以建议一个想法或示例代码来调制Android中的声音文件?

解决方法:

经过大量的试验和错误后,我才找到了答案.

我发布了他们两个的工作代码.

从麦克风录制音频.

import java.io.File;

 import java.io.IOException;

import android.app.Activity;

import android.content.ContentResolver;

import android.content.ContentValues;

import android.content.Intent;

import android.media.MediaRecorder;

import android.net.Uri;

import android.os.Bundle;

import android.os.Environment;

import android.provider.MediaStore;

import android.util.Log;

import android.view.View;

import android.widget.Toast;

public class SoundRecordingActivity extends Activity {

    MediaRecorder recorder;
    File audiofile = null;
    private static final String TAG = "SoundRecordingActivity";
    private View startButton;
    private View stopButton;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        startButton = findViewById(R.id.start);
        stopButton = findViewById(R.id.stop);
    }

    public void startRecording(View view) throws IOException {

        startButton.setEnabled(false);
        stopButton.setEnabled(true);

        File sampleDir = Environment.getExternalStorageDirectory();
        try {
            audiofile = File.createTempFile("sound", ".m4a", sampleDir);
        } catch (IOException e) {
            Log.e(TAG, "sdcard access error");
            return;
        }
        recorder = new MediaRecorder();
        recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        recorder.setOutputFile(audiofile.getAbsolutePath());
        recorder.prepare();
        recorder.start();
    }

    public void stopRecording(View view) {
        startButton.setEnabled(true);
        stopButton.setEnabled(false);
        recorder.stop();
        recorder.release();
        addRecordingToMediaLibrary();
    }

    protected void addRecordingToMediaLibrary() {
        ContentValues values = new ContentValues(4);
        long current = System.currentTimeMillis();
        values.put(MediaStore.Audio.Media.TITLE, "audio" + audiofile.getName());
        values.put(MediaStore.Audio.Media.DATE_ADDED, (int) (current / 1000));
        values.put(MediaStore.Audio.Media.MIME_TYPE, "audio/3gpp");
        values.put(MediaStore.Audio.Media.DATA, audiofile.getAbsolutePath());
        ContentResolver contentResolver = getContentResolver();

        Uri base = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
        Uri newUri = contentResolver.insert(base, values);

        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
        Toast.makeText(this, "Added File " + newUri, Toast.LENGTH_LONG).show();
    }
}

改变音高并播放.

在尝试了所有内容之后,我决定使用SoundPool来改变音高,播放速率为2.0会使声音以高于其原始频率的频率播放,而播放速率为0.5会使其以原始频率的一半播放.播放速率范围为0.5到2.0.但它确实适用于较低且高于0.5和2.0的频率.

我发布了我的工作代码,

但是因为它仅用于演示目的,在这里你需要手动改变“播放速率”,每次安装应用程序时,例如:“sp.play(爆炸,1,1,0,0,1.5f)”这里“1.5 f“是播放速率.可以轻松创建EditView,或类似的设置运行时播放速率的值.

在这个应用程序中,您只需点击应用程序的屏幕即可以设置的播放速率播放音乐.

import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class SoundPoolActivity extends Activity implements OnClickListener {

    SoundPool sp;
    int explosion = 0;

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

       View v = new View(this);
       v.setOnClickListener(this);
       setContentView(v);
       sp = new SoundPool(1,AudioManager.STREAM_MUSIC,0);
       //explosion = sp.load(this, R.raw.hh,0);
       explosion = sp.load("/sdcard/hh.m4a",0);


    }


    public void onClick(View v){
         if (explosion!=0){

             sp.play(explosion, 1,1,0,0,2.3f);
         }

    }
}
上一篇:PX4混控器定义


下一篇:[LeetCode] 849. Maximize Distance to Closest Person_Medium tag: BFS, array