[英]Audio uploaded from iOS not playing on android and vice versa
I am building a chat app in which users can upload audio too on Firebase Storage.我正在构建一个聊天应用程序,用户也可以在 Firebase 存储上上传音频。 But the audios uploaded from android are only playable on android and the audios uploaded from iOS are only playable on iOS.
但是从android上传的音频只能在android上播放,而从iOS上传的音频只能在Z1BDF6059919204EBZCBDF播放。 On the Android side, I am getting this exception while playing iOS audio:
在 Android 方面,我在播放 iOS 音频时遇到此异常:
java.io.IOException: Prepare failed.: status=0x1 java.io.IOException:准备失败。:状态= 0x1
This is the code I am using to upload audio from the android side:这是我用来从 android 端上传音频的代码:
private void startRecording() {
try {
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "AroundEU_media_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName,
".3gp",
storageDir
);
fileName = image.getAbsolutePath();
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
recorder.setOutputFile(fileName);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
try {
recorder.prepare();
} catch (IOException e) {
Log.e("Recording_Tag", "prepare() failed");
}
recorder.start();
}catch (Exception e){
Toast.makeText(this, ""+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void stopRecording() {
try {
if (recorder!=null){
recorder.stop();
recorder.release();
recorder = null;
uploadAudio();
}
}catch (Exception e){
Toast.makeText(this, "Tap and hold to record !", Toast.LENGTH_SHORT).show();
}
}
And the code for playing audio on android side is: android端播放音频的代码为:
try {
Uri uri = Uri.parse(messageModelList.get(position).getAudio());
MediaPlayer player = new MediaPlayer();
player.setAudioStreamType(AudioManager.STREAM_MUSIC);
player.setDataSource(context, uri);
player.prepare();
player.start();
} catch(Exception e) {
System.out.println(e.toString());
}
I have searched a lot but could not find a proper solution.我进行了很多搜索,但找不到合适的解决方案。 So please help me out to solve this problem.
所以请帮我解决这个问题。
You seem to be playing the audio from a URL.您似乎正在播放来自 URL 的音频。 For playing audio from a URL it takes some time to prepare but you playing that immediately that causing the problem.
要从 URL 播放音频,需要一些时间来准备,但您会立即播放导致问题的原因。 You need to prepare it asynchronously and play it when it's prepared.
您需要异步准备它并在准备好时播放它。
Here's an example of how to do it.这是一个如何做到这一点的例子。
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setOnPreparedListener(player -> {
player.start();
});
try {
mediaPlayer.setDataSource(/*Audio Url*/);
mediaPlayer.prepareAsync();
} catch (Exception exception) {
Timber.d("Error -> %s", exception.getMessage());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.