Play Byte array in MediaPlayer - Android

后端 未结 3 909
醉酒成梦
醉酒成梦 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:04

    After write a byte on file : you can play from this function:

     void playSound(int resid) {
            MediaPlayer eSound = MediaPlayer.create(context, resid);
            Resources res = context.getResources();
            AssetFileDescriptor afd = res.openRawResourceFd(resid);
            eSound.reset();
            eSound.setAudioStreamType(AudioManager.STREAM_SYSTEM);
            try {
                eSound.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(),
                        afd.getLength());
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                eSound.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            eSound.start();
        }
    

    And you can get file information from here:

    byte[] getFileInformation(String filepath) {
            MediaPlayer eSound = MediaPlayer.create(context, Uri.parse(filepath));
            eSound.reset();
            eSound.setAudioStreamType(AudioManager.STREAM_SYSTEM);
            try {
                eSound.setDataSource(filepath);
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                eSound.prepare();
            } catch (IllegalStateException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            int duration = eSound.getDuration() / 1000;
    
            int height = 480;
            int width = 640;
            height = eSound.getVideoHeight();
            width = eSound.getVideoWidth();
    
            eSound.release();
            File f = new File(filepath);
            int size = (int) f.length();
            byte[] b = new byte[16];
            System.arraycopy(convertIntToByte(size), 0, b, 0, 4);
            System.arraycopy(convertIntToByte(duration), 0, b, 4, 4);
            System.arraycopy(convertIntToByte(width), 0, b, 8, 4);
            System.arraycopy(convertIntToByte(height), 0, b, 12, 4);
            return b;
        }
    

提交回复
热议问题