你如何在MediaPlayer上玩Android InputStream?

时间:2022-11-20 20:29:04

So I have a small audio file in my assets folder and I wanted to open a InputStream to write to a buffer, then write to a temporary File, then I open up the MediaPlayer to play that temporary File. Problem is, when the media player hits mp.Prepare(), it doesn't play and never reaches the toast. Has anyone ever done this before?

所以我的资源文件夹中有一个小的音频文件,我想打开一个InputStream写入缓冲区,然后写入临时文件,然后打开MediaPlayer播放该临时文件。问题是,当媒体播放器点击mp.Prepare()时,它不播放并且永远不会到达吐司。有没有人曾经这样做过?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    InputStream str;

    try {

        str = this.getAssets().open("onestop.mid");
        Toast.makeText(this, "Successful Input Stream Opened.", Toast.LENGTH_SHORT).show();
        takeInputStream(str);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}//end on create

public void takeInputStream(InputStream stream) throws IOException
{
    //fileBeingBuffered = (FileInputStream) stream;
    //Toast.makeText(this, "sucessful stream conversion.", Toast.LENGTH_SHORT).show();
    try
    {
        convertedFile = File.createTempFile("convertedFile", ".dat", getDir("filez", 0));
        Toast.makeText(this, "Successful file and folder creation.", Toast.LENGTH_SHORT).show();

        out = new FileOutputStream(convertedFile);
        Toast.makeText(this, "Success out set as output stream.", Toast.LENGTH_SHORT).show();

        //RIGHT AROUND HERE -----------

        byte buffer[] = new byte[16384];
        int length = 0;
        while ( (length = stream.read(buffer)) != -1 ) 
        {
          out.write(buffer,0, length);
        }

        //stream.read(buffer);
        Toast.makeText(this, "Success buffer is filled.", Toast.LENGTH_SHORT).show();
        out.close();

        playFile();
    }catch(Exception e)
    {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    }//end catch
}//end grabBuffer

public void playFile()
{
    try {
        String path = convertedFile.getAbsolutePath();
        mp = new MediaPlayer();
        mp.setDataSource(path);
        Toast.makeText(this, "Success, Path has been set", Toast.LENGTH_SHORT).show();

        mp.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mp.prepare();
        Toast.makeText(this, "Media Player prepared", Toast.LENGTH_SHORT).show();

        mp.start();
        Toast.makeText(this, "Media Player playing", Toast.LENGTH_SHORT).show();
    } catch (IllegalArgumentException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    } catch (IllegalStateException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    } catch (IOException e) {
        Log.e(TAG, e.toString());
        e.printStackTrace();
    }

}//end playFile

2 个解决方案

#1


12  

Fixed it. Turns out that after writing the buffer in the temporary file created by "File," you can then open that file using a FileInputStream, then proceed to play it shown below. Thanks for all your help guys.

修复。事实证明,在“File”创建的临时文件中写入缓冲区后,您可以使用FileInputStream打开该文件,然后继续播放如下所示。谢谢你所有的帮助。

mp = new MediaPlayer();

FileInputStream fis = new FileInputStream(convertedFile);
mp.setDataSource(fis.getFD());

Toast.makeText(this, "Success, Path has been set", Toast.LENGTH_SHORT).show();

mp.prepare();
mp.start();

#2


1  

This is the code that worked for me

这是适合我的代码

//preserved to stop previous actions 
MediaPlayer lastmp;

public void playSound(String file) {
    try {
        if (lastmp!=null) lastmp.stop();
        MediaPlayer mp = new MediaPlayer();
        lastmp = mp;
        AssetFileDescriptor descriptor;

        AssetManager assetManager = act.getAssets();

        descriptor =  assetManager.openFd(fileName);
        mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();
        mp.prepare();
        mp.start();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

the file shoud be on the assets folder

该文件应该在assets文件夹中

#1


12  

Fixed it. Turns out that after writing the buffer in the temporary file created by "File," you can then open that file using a FileInputStream, then proceed to play it shown below. Thanks for all your help guys.

修复。事实证明,在“File”创建的临时文件中写入缓冲区后,您可以使用FileInputStream打开该文件,然后继续播放如下所示。谢谢你所有的帮助。

mp = new MediaPlayer();

FileInputStream fis = new FileInputStream(convertedFile);
mp.setDataSource(fis.getFD());

Toast.makeText(this, "Success, Path has been set", Toast.LENGTH_SHORT).show();

mp.prepare();
mp.start();

#2


1  

This is the code that worked for me

这是适合我的代码

//preserved to stop previous actions 
MediaPlayer lastmp;

public void playSound(String file) {
    try {
        if (lastmp!=null) lastmp.stop();
        MediaPlayer mp = new MediaPlayer();
        lastmp = mp;
        AssetFileDescriptor descriptor;

        AssetManager assetManager = act.getAssets();

        descriptor =  assetManager.openFd(fileName);
        mp.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
        descriptor.close();
        mp.prepare();
        mp.start();

    } catch (Exception e) {
        e.printStackTrace();
    }
}

the file shoud be on the assets folder

该文件应该在assets文件夹中