我有 byte[] 数组,其中包含 mp3 文件。有什么方法可以在不创建临时文件的情况下播放 mp3 文件?
谢谢
已编辑:
我试过临时文件的方法:
File tempMp3 = File.createTempFile("test", ".mp3", getCacheDir());
//tempMp3.deleteOnExit();
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(bytes);
fos.close();
mediaPlayer = MediaPlayer.create(getApplicationContext(),Uri.fromFile(tempMp3));
mediaPlayer.start();
而且我仍然收到 NullPointerException。我正在将字节数组(称为字节)写入文件,我正在保留它的引用并使用它。但仍然得到NPE。你知道错在哪里吗?
最佳答案
我假设您使用的是 MediaPlayer
在最新的 API 之前,无法将数据设置为 byte[]
格式,就像 String
(path)、FileDescriptor
或Uri
所以恐怕您将不得不编写一个临时的 File
。
关于Android - 从 byte[] 数组播放 mp3,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10013246/