gpt4 book ai didi

android - 如何在Android中播放Base64编码的音频?

转载 作者:行者123 更新时间:2023-12-03 01:50:20 26 4
gpt4 key购买 nike

到目前为止,我一直在尝试:

  • 具有音频文件的Base64编码的字符串。

  • 现在在这里,我试图像这样玩。
    public void PlayAudio(String base64EncodedString){
    try
    {
    String url = "data:audio/mp3;base64,"+base64EncodedString;
    Uri uri = Uri.parse(url);
    mediaPlayer.setDataSource(MainActivity.this, uri);
    /*mediaPlayer.prepare();*/
    mediaPlayer.start();
    }
    catch(Exception ex){
    System.out.print(ex.getMessage());
    }
    }


    private void playMp3(byte[] mp3SoundByteArray) {
    try {
    // create temp file that will hold byte array
    File tempMp3 = File.createTempFile("kurchina", "mp3", getCacheDir());
    tempMp3.deleteOnExit();
    FileOutputStream fos = new FileOutputStream(tempMp3);
    fos.write(mp3SoundByteArray);
    fos.close();

    // resetting mediaplayer instance to evade problems
    mediaPlayer.reset();

    // In case you run into issues with threading consider new instance like:
    // MediaPlayer mediaPlayer = new MediaPlayer();

    // Tried passing path directly, but kept getting
    // "Prepare failed.: status=0x1"
    // so using file descriptor instead
    FileInputStream fis = new FileInputStream(tempMp3);
    mediaPlayer.setDataSource(fis.getFD());

    //mediaPlayer.prepare();
    mediaPlayer.start();
    } catch (IOException ex) {
    String s = ex.toString();
    ex.printStackTrace();
    }
    }

    但是音频没有播放。

    愿有人在这方面帮助我。

    最佳答案

    实际上,您需要在播放之前解码Base64编码的String。请检查here的解码过程

    关于android - 如何在Android中播放Base64编码的音频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40276643/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com