我使用android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI intent从SD卡加载音乐文件。
Intent tmpIntent1 = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(tmpIntent1, 0);
在onActivityResult中
Uri mediaPath = Uri.parse(data.getData().toString());
MediaPlayer mp = MediaPlayer.create(this, mediaPath);
mp.start();
现在MediaPlayer以立体声播放音频。有没有办法在应用程序中将选定的音乐/音频文件或立体声输出转换为单声道?
我查找了SoundPool和AudioTrack的应用程序接口,但没有找到如何将mp3文件的音频转换为单声道。
像PowerAMP这样的应用程序有立体声<->单声道开关,当按下这些开关时,会立即将输出的音频转换为单声道信号,然后再转换回来,它们是如何做到的?
发布于 2013-06-29 02:24:21
是否分别加载.wav- files和PCM- data?如果是这样,那么您可以很容易地读取每个通道的每个样本,将它们叠加并除以通道的数量,以获得单声道信号。
如果您以交错带符号短路的形式存储立体声信号,则计算结果单声道信号的代码可能如下所示:
short[] stereoSamples;//get them from somewhere
//output array, which will contain the mono signal
short[] monoSamples= new short[stereoSamples.length/2];
//length of the .wav-file header-> 44 bytes
final int HEADER_LENGTH=22;
//additional counter
int k=0;
for(int i=0; i< monoSamples.length;i++){
//skip the header andsuperpose the samples of the left and right channel
if(k>HEADER_LENGTH){
monoSamples[i]= (short) ((stereoSamples[i*2]+ stereoSamples[(i*2)+1])/2);
}
k++;
}
我希望,我能帮到你。
致以最好的问候,G_J
发布于 2020-10-31 09:49:07
首先,获取一个编码器将您的音频文件转换为短数组(short[])。
然后通过拆分数据流将短数组转换为单声道。
然后对分割后的流进行平均。
short[] stereo = new short[BUF_SIZE];
short[] monoCh1 = new short[BUF_SIZE/2];
short[] monoCh2 = new short[BUF_SIZE/2];
short[] monoAvg = new short[BUF_SIZE/2];
stereo = sampleBuffer.getBuffer(); //get the raw S16 PCM buffer here
for( int i=0 ; i < stereo.length ; i+=2 )
{
monoCh1[i/2] = (short) stereo[i];
monoCh2[i/2] = (short) stereo[i+1];
monoAvg[i/2] = (short) ( ( stereo[i] + stereo[i+1] ) / 2 );
}
现在你拥有了::
来自monoCh1
中通道1(左通道)的PCM单声道数据流
来自monoCh2
中通道2(右通道)的PCM单声道数据流
来自monoAvg
中平均两个通道的PCM单声道流
https://stackoverflow.com/questions/17363705
复制相似问题