android音频录制和播放不了,Android音频录制和播放已损坏

我试图录制一个PCM音频文件并播放它。当我回放时,它听起来很慢并且需要比记录更长的时间。我不确定错误是在记录还是播放代码中。任何想法是什么问题?Android音频录制和播放已损坏

这里是记录代码(isRecording标志由停止按钮在GUI线程设置)。

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_URGENT_AUDIO);

int sampleRateInHz = 8000;//8000 44100, 22050 and 11025

int channelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO;

int audioFormat = AudioFormat.ENCODING_PCM_16BIT;

File sd = Environment.getExternalStorageDirectory();

File file = new File(sd, "msg.wav");

if (file.exists())

file.delete();

try {

file.createNewFile();

} catch (IOException e) {

Log.e("create file:", e.toString());

}

try {

OutputStream os = new FileOutputStream(file);

BufferedOutputStream bos = new BufferedOutputStream(os);

DataOutputStream dos = new DataOutputStream(bos);

int bufferSize = AudioRecord.getMinBufferSize(sampleRateInHz,channelConfig, audioFormat);

short[] buffer = new short[bufferSize];

audioRecord = new AudioRecord(MediaRecorder.AudioSource.MIC,

sampleRateInHz,channelConfig, audioFormat,bufferSize);

audioRecord.startRecording();

isRecording = true;

while (isRecording) {

int bufferReadResult = audioRecord.read(buffer, 0, bufferSize);

for (int i = 0; i < bufferReadResult; i++)

{

dos.writeShort(buffer[i]);

}

}

dos.close();

这是播放代码。

File file = new File(SendAlert.voiceFile);

// Get the length of the audio stored in the file (16 bit so 2 bytes per short)

// and create a short array to store the recorded audio.

int musicLength = (int)(file.length()/2);

short[] music = new short[musicLength];

try {

// Create a DataInputStream to read the audio data back from the saved file.

InputStream is = new FileInputStream(file);

BufferedInputStream bis = new BufferedInputStream(is);

DataInputStream dis = new DataInputStream(bis);

// Read the file into the music array.

int i = 0;

while (dis.available() > 0) {

music[i] = dis.readShort();

i++;

}

// Close the input streams.

dis.close();

// Create a new AudioTrack object using the same parameters as the AudioRecord

// object used to create the file.

AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,

8000,

AudioFormat.CHANNEL_CONFIGURATION_MONO,

AudioFormat.ENCODING_PCM_16BIT,

musicLength,

AudioTrack.MODE_STREAM);

// Start playback

audioTrack.play();

// Write the music buffer to the AudioTrack object

audioTrack.write(music, 0, musicLength);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android蓝牙和线同时播放音频是可以实现的,需要使用AudioManager的多媒体音量控制和AudioTrack进行音频播放。 具体实现步骤如下: 1. 获取系统AudioManager实例。 ``` AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE); ``` 2. 将多媒体音量设置为最大值。 ``` audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); ``` 3. 初始化AudioTrack,指定音频采样率、声道数和音频格式。 ``` int sampleRate = 44100; int channelConfig = AudioFormat.CHANNEL_OUT_STEREO; int audioFormat = AudioFormat.ENCODING_PCM_16BIT; int bufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat); AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, bufferSize, AudioTrack.MODE_STREAM); ``` 4. 将音频数据写入AudioTrack。 ``` audioTrack.write(audioData, 0, audioData.length); ``` 5. 启动AudioTrack播放音频。 ``` audioTrack.play(); ``` 同时,如果要实现Android蓝牙音频播放录制,需要使用BluetoothA2dp和BluetoothHeadsetProfile代表蓝牙A2DP和耳机协议,并使用MediaRecorder进行录制。 具体实现步骤如下: 1. 获取系统BluetoothAdapter实例。 ``` BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); ``` 2. 获取蓝牙A2DP和耳机协议的代表实例。 ``` BluetoothProfile a2dpProfile = bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { if (profile == BluetoothProfile.A2DP) { a2dpProfile = (BluetoothA2dp) proxy; } } @Override public void onServiceDisconnected(int profile) { if (profile == BluetoothProfile.A2DP) { a2dpProfile = null; } } }, BluetoothProfile.A2DP); BluetoothProfile headsetProfile = bluetoothAdapter.getProfileProxy(context, new BluetoothProfile.ServiceListener() { @Override public void onServiceConnected(int profile, BluetoothProfile proxy) { if (profile == BluetoothProfile.HEADSET) { headsetProfile = (BluetoothHeadset) proxy; } } @Override public void onServiceDisconnected(int profile) { if (profile == BluetoothProfile.HEADSET) { headsetProfile = null; } } }, BluetoothProfile.HEADSET); ``` 3. 连接蓝牙设备。 ``` BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); a2dpProfile.connect(device); headsetProfile.connect(device); ``` 4. 使用MediaRecorder进行录制。 ``` MediaRecorder mediaRecorder = new MediaRecorder(); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mediaRecorder.setOutputFile(outputFile); mediaRecorder.prepare(); mediaRecorder.start(); ``` 5. 使用AudioTrack进行播放。 ``` int sampleRate = 44100; int channelConfig = AudioFormat.CHANNEL_OUT_STEREO; int audioFormat = AudioFormat.ENCODING_PCM_16BIT; int bufferSize = AudioTrack.getMinBufferSize(sampleRate, channelConfig, audioFormat); AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC, sampleRate, channelConfig, audioFormat, bufferSize, AudioTrack.MODE_STREAM); audioTrack.play(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值