android - 你如何在 MediaPlayer 上播放 Android InputStream?

标签 android file inputstream fileoutputstream

所以我的 Assets 文件夹中有一个小音频文件,我想打开一个 InputStream 写入缓冲区,然后写入一个临时文件,然后我打开 MediaPlayer 播放那个临时文件。问题是,当媒体播放器点击 mp.Prepare() 时,它不会播放并且永远不会到达 toast。以前有人做过吗?

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

最佳答案

修复它。事实证明,在"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();

关于android - 你如何在 MediaPlayer 上播放 Android InputStream?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5747060/

相关文章:

java - 为什么重新启动具有限制 UserManager.DISALLOW_USB_FILE_TRANSFER 使用的应用程序的设备会导致该设备无法使用?

android - 如何正确保存到图片文件夹

java - Robolectric @config 指定 list 路径时,抛出空指针异常

Android:SearchView:如何为建议列表中的每个项目设置单独的图标?

python - Pandas 使用格式更新 Excel 文件

python - 压缩文件路径中的所有文件

Java:使用 File.renameTo() 使用浏览器将文本文件打开为 html 文件

java - InputStreamReader.markSupported 为 false

c++ - 线程库的奇怪行为

java - 每次使用后都应该关闭流吗?