首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何播放Spring引导应用程序中的音频blob

如何播放Spring引导应用程序中的音频blob
EN

Stack Overflow用户
提问于 2022-05-13 22:43:30
回答 1查看 248关注 0票数 0

我正在以角存储音频blob (录音声音),然后调用spring引导API将其存储在蔚蓝blob存储中,如下所示:

代码语言:javascript
运行
复制
  submit(){
        const blob = this.audioRecording.getblob();
        this.blobOutgoing = new FormData(); 
        this.blobOutgoing.append('file', blob);
        this.blobOutgoing.append('name', this.name);
        this.blobOutgoing.append('email', this.email);
        this.blobOutgoing.append('uid', this.uid);
        this.pronunciationAPIService.saveEmployeeNameAlternate(this.blobOutgoing)
        .subscribe((response: any) => {
               console.log(response);
        })

  }

 public  void insertEmpoyeeRecord(Employee employee) {

        try {
            Statement stmt = getStatement();
            System.out.println("Connected to the YugabyteDB Cluster successfully.");
            // stmt.execute("DROP TABLE IF EXISTS employee");
            /*stmt.execute("CREATE TABLE IF NOT EXISTS employee" +
                    "  (id int primary key, name varchar, age int, language text)");*/
            // System.out.println("Created table employee");

            String insertStr = "INSERT INTO employees.employees  VALUES ('"+employee.getUid()+"','"+employee.getEmail()+"','"+employee.getName()+"','"+employee.getUid()+"')";
            String deleteStr = "DELETE FROM employees.employees WHERE email='"+employee.getEmail()+"' or uid='"+employee.getUid()+"'";
            stmt.execute(deleteStr);
            stmt.execute(insertStr);
           ----------> blobService.uploadFile(employee.getMultipartFile(), employee.getUid());   <----------------------------------
            System.out.println("EXEC: " + insertStr);

            ResultSet rs = stmt.executeQuery("select * from employees.employees");
            while (rs.next()) {
                System.out.println(String.format("Query returned: uid = %s, email = %s, name = %s, blob = %s",
                        rs.getString("uid"), rs.getString("email"), rs.getString("name"), rs.getString("audio")));
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

public void uploadFile(MultipartFile multipartFile, String audioFilenameRequest){
   // String localFolderPath = "C:\\Users\\erman\\Downloads\\audiofolder\\";
    try {
        byte[] bytes = multipartFile.getBytes();
        System.out.println("lenght:: " + bytes.length);
        String audioFileName = audioFilenameRequest;

        CloudBlobContainer containerReference = getCloudBlobContainer();

        //Getting a blob reference

        CloudBlockBlob blockBlobReference = containerReference.getBlockBlobReference(audioFileName);


        //Creating blob and uploading file to it
        //System.out.println("Uploading the sample file, Absolute path: "+sourceFile.getAbsolutePath() );
        blockBlobReference.uploadFromByteArray(bytes,0,bytes.length);
        System.out.println("upload to Azure cloud blob is done!!!!");
        // blockBlobReference.upload

       /* Path path = Paths.get(localFolderPath + multipartFile.getOriginalFilename());
        Files.write(path,bytes);*/

    } catch (IOException e) {
        e.printStackTrace();
    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (StorageException e) {
        e.printStackTrace();
    }
}

然后,我尝试通过调用另一个Spring引导API从角检索:

代码语言:javascript
运行
复制
  playAudioFromBlob(){
    this.pronunciationAPIService
    .pronounceName(this.employee)
     .subscribe((response: Array<Employee>) => {
     console.log(response);
   response.forEach( (employee) => {
    let blob = new Blob(employee.blobByte, {type: "audio/webm"});
    console.log(employee.blob);
    const audioURL = URL.createObjectURL(blob);
    let audio = new Audio(audioURL)
    audio.controls = true;
     audio.play();
    })
   });
  }

public List<Employee> searchEmployeeByUid(String uid){
    Employee employee = null;
    try {
        System.out.println("Connected to the YugabyteDB Cluster successfully.");
        Statement stmt = getStatement();
        String selectStr = "SELECT uid,email,name,audio FROM employees.employees WHERE uid='"+uid+"'";

        stmt.execute(selectStr);
        System.out.println("EXEC: " + selectStr);

        ResultSet rs = stmt.executeQuery(selectStr);

        while (rs.next()) {
            employee = new Employee();
            employee.setUid(rs.getString("uid"));
            employee.setEmail(rs.getString("email"));
            employee.setName(rs.getString("name"));
            employee.setBlob(rs.getString("audio"));
        }
    } catch (SQLException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

    byte[] blob = blobService.downloadFile(employee.getBlob());
    employee.setBlobByte(blob);
    return  employee;
}

public byte[] downloadFile(String audioFileName) {
    File downloadedFile = null;
    byte[] audioByteArray = new byte[472179];
    try {
        // byte[] bytes = multipartFile.getBytes();
        // String audioFileName = multipartFile.getOriginalFilename();

        CloudBlobContainer containerReference = getCloudBlobContainer();

        //Getting a blob reference

        CloudBlockBlob blockBlobReference = containerReference.getBlockBlobReference(audioFileName);
       // downloadedFile = new File(audioFileName);
        //byte [] b = new byte[472179];
        blockBlobReference.downloadToByteArray(audioByteArray,0);
        System.out.println("download from Azure cloud blob is done!!!!:: Size : " + audioByteArray.length);



    } catch (URISyntaxException e) {
        e.printStackTrace();
    } catch (InvalidKeyException e) {
        e.printStackTrace();
    } catch (StorageException e) {
        e.printStackTrace();
    }

    return audioByteArray;
}

public class Employee {

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getUid() {
        return uid;
    }

    public void setUid(String uid) {
        this.uid = uid;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getBlob() {
        return blob;
    }

    public void setBlob(String blob) {
        this.blob = blob;
    }

    public MultipartFile getMultipartFile() {
        return blobOutgoing;
    }

    public void setMultipartFile(MultipartFile multipartFile) {
        this.blobOutgoing = multipartFile;
    }

    private String name;
    private String uid;
    private String email;
    private String blob;
    private MultipartFile blobOutgoing;

    public byte[] getBlobByte() {
        return blobByte;
    }

    public void setBlobByte(byte[] blobByte) {
        this.blobByte = blobByte;
    }

    private byte[] blobByte;
}

问题是当将byte[]流按角度转换为blob时,我得到了错误:

代码语言:javascript
运行
复制
Failed to construct 'Blob': The provided value cannot be converted to a sequence

我认为我得到了这个问题,因为我没有正确地写作或阅读博客。我用ng-录音机作角录音。ng-音频记录器在音频webm中构建blob

更新:也许一个更简单的问题是,如何以角度回放多部分文件中的byte[]流?

EN

回答 1

Stack Overflow用户

发布于 2022-06-03 00:48:37

初始参数(参数)必须在序列中显示。

代码语言:javascript
运行
复制
let blob = new Blob(employee.blobByte, {type: "audio/webm"});  console.log(employee.blob);

将此替换为下面一个。

代码语言:javascript
运行
复制
let blob=new Blob([employee.blobByte] ,{type: "audio/webm"});  console.log(employee.blob);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72236035

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档