简体   繁体   English

从 iOS 上传的音频无法在 android 上播放,反之亦然

[英]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.

相关问题 将推送通知从Android发送到iOS,反之亦然? - Send push-notification from Android to iOS and vice versa? 我可以将通知从Android发送到iOS,反之亦然吗? - May I send notification from Android to iOS or vice versa? 从android上传的音频文件无法在iOS中使用 - Audio file uploaded from android not working in ios 将 Android 中的音频从 48kHz 重新采样到 44.1kHz,反之亦然——纯 Java 还是 OpenSL ES? - Resampling audio in Android from 48kHz to 44.1kHz and vice versa - pure Java, or OpenSL ES? 单击时将音频按钮从播放更改为暂停,反之亦然 - Changing audio button from play to pause when clicked and vice versa 使用套接字的iOS至Android,反之亦然TCP服务器客户端连接 - IOS to Android and vice versa TCP server client connection using sockets 从服务器向Android设备发送消息,反之亦然 - Send message from Server to Android device and vice versa 沿Z方向动画化android视图(从后到前,反之亦然) - Animating android view in Z direction (from back to forward and vice versa) 使用QuickBlox,视频通话无法从Android接收到iPhone,反之亦然 - Video calling not recieving from android to iphone vice versa using QuickBlox Android Hello World可以从纵向定向到横向,反之则不然 - Android Hello World orients from portrait to landscape but not vice versa
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM