Play Byte array in MediaPlayer - Android

后端 未结 3 896
醉酒成梦
醉酒成梦 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;
        }
    
    0 讨论(0)
  • 2020-12-31 19:09

    Based on your comments, you are using a WAV file for streaming. Conventional WAV has a header at the beginning of the file and the rest of the file is pure data. If you don't include the header part, then the parameters from it need to be provided to the player the be able to interpret the data (number of channels, samples per second, block size, etc.) As a result, a WAV file alone is not streamable, the parameters must be fed into the player.

    MPEG-4 on the other hand has been specified to be capable of streaming. It contains well identifiable segments that are possible to be played alone, so as long as the data chunk contains a header, the data after it can be playable. In addition to it's good compression ratio, this is one of the reasons why many internet radios use it.

    MediaPlayer in Android is a high level component and the low level parameters that would be required for playing a WAV chunk are not accessible. If you need the source to be WAV and cannot use other streamable formats, then you can try the AudioTrack class or OpenSL ES for even lower level access. For AudioTrack this is a very good tutorial: http://audioprograming.wordpress.com/2012/10/18/a-simple-synth-in-android-step-by-step-guide-using-the-java-sdk/

    0 讨论(0)
  • 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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题