Play Byte array in MediaPlayer - Android

后端 未结 3 910
醉酒成梦
醉酒成梦 2020-12-31 18:53

I am trying to play an audio file with the MediaPlayer. I want to play byte array in MediaPlayer. How can I do this? I\'ve checked this

 public void writeSam         


        
3条回答
  •  春和景丽
    2020-12-31 19:28

    Try this :

    private void playMp3(byte[] mp3SoundByteArray) 
    {
        try 
        {
    
            File path=new File(getCacheDir()+"/musicfile.3gp");
    
            FileOutputStream fos = new FileOutputStream(path);
            fos.write(mp3SoundByteArray);
            fos.close();
    
            MediaPlayer mediaPlayer = new MediaPlayer();
    
            FileInputStream fis = new FileInputStream(path);
            mediaPlayer.setDataSource(getCacheDir()+"/musicfile.3gp");
    
            mediaPlayer.prepare();
            mediaPlayer.start();
        } 
        catch (IOException ex) 
        {
            String s = ex.toString();
            ex.printStackTrace();
        }
    }
    

提交回复
热议问题