android开发中怎样实现混音
答案:2 悬赏:30 手机版
解决时间 2021-02-14 05:59
- 提问者网友:寂寞撕碎了回忆
- 2021-02-13 21:38
android开发中怎样实现混音
最佳答案
- 五星知识达人网友:鱼忧
- 2021-02-13 22:06
android开发中怎样实现混音,实例代码如下:
[mw_shl_code=java,true]public class MixRunnable implements Runnable {
private MixRecorder context;
private static class RecorderParameter {
// 音频获取源
private static int audioSource = MediaRecorder.AudioSource.MIC;
// 设置音频采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025
private static final int sampleRateInHz = 44100;
// 设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
private static final int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
// 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
private static final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
// 缓冲区字节大小
private static int bufferSizeInBytes;
}
// 设置运行状态
private boolean isRunning = true;
// AudioRecord对象
private static AudioRecord recorder;
// 设置MediaPlayer对象
private static MediaPlayer mediaPlayer;
// 伴奏文件
private FileInputStream accompany;
// 原唱文件
private FileInputStream original;
// 得分
private int score;
private boolean isFirst = true;
public MixRunnable(MixRecorder context, String accompany, String original) throws FileNotFoundException {
this.context = context;
this.accompany = new FileInputStream(accompany);
this.original = new FileInputStream(original);
creatAudioRecord();
mediaPlayer = new MediaPlayer();
}
@Override
public void run() {
try {
// MediaPlayer准备
mediaPlayer.reset();
mediaPlayer.setDataSource("/sdcard/111.wav");
// mediaPlayer.setDataSource(accompany.getFD());
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
isRunning = false;
}
});
mediaPlayer.prepare();
// 跳过头
accompany.read(new byte[44]);
original.read(new byte[44]);
FileOutputStream fos = new FileOutputStream(new File("/sdcard/love.raw"));
// 开始读
byte[] sourceReader = new byte[RecorderParameter.bufferSizeInBytes * 2];
short[] sourceShortArray;
short[] audioReader = new short[sourceReader.length / 4];
mediaPlayer.start();
recorder.startRecording();
while (isRunning) {
int sourceReadSize = accompany.read(sourceReader, 0, sourceReader.length);
if (sourceReadSize < 0) {
isRunning = false;
continue;
}
sourceShortArray = byteToShortArray(sourceReader, sourceReadSize / 2);
recorder.read(audioReader, 0, audioReader.length);
short[] oneSecond = mixVoice(sourceShortArray, audioReader, sourceReadSize / 2);
byte[] outStream = new byte[oneSecond.length * 2];
for (int i = 0; i < oneSecond.length; i++) {
byte[] b = shortToByteArray(oneSecond);
outStream[2 * i] = b[0];
outStream[2 * i + 1] = b[1];
}
[mw_shl_code=java,true]public class MixRunnable implements Runnable {
private MixRecorder context;
private static class RecorderParameter {
// 音频获取源
private static int audioSource = MediaRecorder.AudioSource.MIC;
// 设置音频采样率,44100是目前的标准,但是某些设备仍然支持22050,16000,11025
private static final int sampleRateInHz = 44100;
// 设置音频的录制的声道CHANNEL_IN_STEREO为双声道,CHANNEL_CONFIGURATION_MONO为单声道
private static final int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;
// 音频数据格式:PCM 16位每个样本。保证设备支持。PCM 8位每个样本。不一定能得到设备支持。
private static final int audioFormat = AudioFormat.ENCODING_PCM_16BIT;
// 缓冲区字节大小
private static int bufferSizeInBytes;
}
// 设置运行状态
private boolean isRunning = true;
// AudioRecord对象
private static AudioRecord recorder;
// 设置MediaPlayer对象
private static MediaPlayer mediaPlayer;
// 伴奏文件
private FileInputStream accompany;
// 原唱文件
private FileInputStream original;
// 得分
private int score;
private boolean isFirst = true;
public MixRunnable(MixRecorder context, String accompany, String original) throws FileNotFoundException {
this.context = context;
this.accompany = new FileInputStream(accompany);
this.original = new FileInputStream(original);
creatAudioRecord();
mediaPlayer = new MediaPlayer();
}
@Override
public void run() {
try {
// MediaPlayer准备
mediaPlayer.reset();
mediaPlayer.setDataSource("/sdcard/111.wav");
// mediaPlayer.setDataSource(accompany.getFD());
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
isRunning = false;
}
});
mediaPlayer.prepare();
// 跳过头
accompany.read(new byte[44]);
original.read(new byte[44]);
FileOutputStream fos = new FileOutputStream(new File("/sdcard/love.raw"));
// 开始读
byte[] sourceReader = new byte[RecorderParameter.bufferSizeInBytes * 2];
short[] sourceShortArray;
short[] audioReader = new short[sourceReader.length / 4];
mediaPlayer.start();
recorder.startRecording();
while (isRunning) {
int sourceReadSize = accompany.read(sourceReader, 0, sourceReader.length);
if (sourceReadSize < 0) {
isRunning = false;
continue;
}
sourceShortArray = byteToShortArray(sourceReader, sourceReadSize / 2);
recorder.read(audioReader, 0, audioReader.length);
short[] oneSecond = mixVoice(sourceShortArray, audioReader, sourceReadSize / 2);
byte[] outStream = new byte[oneSecond.length * 2];
for (int i = 0; i < oneSecond.length; i++) {
byte[] b = shortToByteArray(oneSecond);
outStream[2 * i] = b[0];
outStream[2 * i + 1] = b[1];
}
全部回答
- 1楼网友:酒者煙囻
- 2021-02-13 22:58
音频混音的原理: 量化的语音信号的叠加等价于空气中声波的叠加。
下面是代码,可以参照来学习:
public void mixaudios(file[] rawaudiofiles){
final int filesize = rawaudiofiles.length;
fileinputstream[] audiofilestreams = new fileinputstream[filesize];
file audiofile = null;
fileinputstream inputstream; byte[][] allaudiobytes = new byte[filesize][]; boolean[] streamdonearray = new boolean[filesize]; byte[] buffer = new byte[512]; int offset;
try {
for (int fileindex = 0; fileindex < filesize; ++fileindex) {
audiofile = rawaudiofiles[fileindex];
audiofilestreams[fileindex] = new fileinputstream(audiofile);
} while(true){
for(int streamindex = 0 ; streamindex < filesize ; ++streamindex){
inputstream = audiofilestreams[streamindex]; if(!streamdonearray[streamindex] && (offset = inputstream.read(buffer)) != -1){
allaudiobytes[streamindex] = arrays.copyof(buffer,buffer.length);
}else{
streamdonearray[streamindex] = true;
allaudiobytes[streamindex] = new byte[512];
}
}
byte[] mixbytes = mixrawaudiobytes(allaudiobytes);
//mixbytes 就是混合后的数据
boolean done = true; for(boolean streamend : streamdonearray){ if(!streamend){
done = false;
}
}
if(done){ break;
}
}
} catch (ioexception e) {
e.printstacktrace(); if(monaudiomixlistener != null)
monaudiomixlistener.onmixerror(1);
}finally{ try { for(fileinputstream in : audiofilestreams){ if(in != null)
in.close();
}
} catch (ioexception e) {
e.printstacktrace();
}
}
}byte[] averagemix(byte[][] bmulroadaudioes) {
if (bmulroadaudioes == null || bmulroadaudioes.length == 0) return null; byte[] realmixaudio = bmulroadaudioes[0];
if(bmulroadaudioes.length == 1) return realmixaudio;
for(int rw = 0 ; rw < bmulroadaudioes.length ; ++rw){ if(bmulroadaudioes[rw].length != realmixaudio.length){
log.e("app", "column of the road of audio + " + rw +" is diffrent."); return null;
}
}
int row = bmulroadaudioes.length; int coloum = realmixaudio.length / 2; short[][] smulroadaudioes = new short[row][coloum]; for (int r = 0; r < row; ++r) { for (int c = 0; c < coloum; ++c) {
smulroadaudioes[r][c] = (short) ((bmulroadaudioes[r][c * 2] & 0xff) | (bmulroadaudioes[r][c * 2 + 1] & 0xff) << 8);
}
} short[] smixaudio = new short[coloum]; int mixval; int sr = 0; for (int sc = 0; sc < coloum; ++sc) {
mixval = 0;
sr = 0; for (; sr < row; ++sr) {
mixval += smulroadaudioes[sr][sc];
}
smixaudio[sc] = (short) (mixval / row);
} for (sr = 0; sr < coloum; ++sr) {
realmixaudio[sr * 2] = (byte) (smixaudio[sr] & 0x00ff);
realmixaudio[sr * 2 + 1] = (byte) ((smixaudio[sr] & 0xff00) >> 8);
} return realmixaudio;
}
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯