Java 已将blob转换为Mp3,但文件无法播放
这是我将blob转换为mp3的功能:Java 已将blob转换为Mp3,但文件无法播放,java,android,android-mediaplayer,java-io,Java,Android,Android Mediaplayer,Java Io,这是我将blob转换为mp3的功能: private void convertByyeToMP3(byte[] bytearray,String trackName) { try { ContextWrapper c = new ContextWrapper(getApplicationContext()); File directory = new File(c.getFilesDir().getAbsol
private void convertByyeToMP3(byte[] bytearray,String trackName) {
try {
ContextWrapper c = new ContextWrapper(getApplicationContext());
File directory = new File(c.getFilesDir().getAbsolutePath()
+ "/Music");
if (!directory.exists()){
directory.mkdir();
}
File tempMp3 = File.createTempFile(trackName, ".mp3",
directory);
FileOutputStream fos = new FileOutputStream(tempMp3);
fos.write(bytearray);
fos.close();
Log.d("Byte array to mp3 conversion: ", "successfull");
} catch (Exception ex) {
Log.d("In convertToByteToMp3 Function:", ex.toString());
}
}
当我执行此功能时,我可以在我的应用程序文件夹中看到创建的mp3文件,但当我尝试使用自己的代码或ES File Explorer播放它们时,它们都无法播放
这是我用来播放音乐的功能:
private MediaPlayer mp = new MediaPlayer();
private void playSong(String songPath) {
try {
mp.reset();
mp.setDataSource(songPath);
mp.prepare();
mp.start();
} catch (IOException e) {
Log.v(getString(R.string.app_name), e.getMessage());
}
}
我使用以下示例代码播放曲目:
ContextWrapper c = new ContextWrapper(getApplicationContext());
File directory = new File(c.getFilesDir().getAbsolutePath() + "/Music");
playSong(directory.getPath() + File.separator + "kurchina");
这是我读取数据库并发送blob的地方:
cursor = mDbHelper.GetTables();
byte[] blob = null;
DATAS data = new DATAS();
while (cursor.moveToNext()) {
blob = cursor.getBlob(cursor.getColumnIndex("data"));
if (blob != null) {convertByyeToMP3(blob,data_MusicName);}
db.addDATAS(data);
}
FYIs:-已将读写权限添加到清单
-路径和文件名已检查,并且已存在
-blob字节未损坏在您向我们展示的代码或其他地方,可能存在各种各样的错误。因此,您需要自己进行故障排除。有条不紊地
它没有播放,因为音乐文件存储在“我的应用”文件夹中,只能使用根设备访问该文件夹
当我将音乐复制到SD卡时,它们播放得很好,但在使用根nexus 7的应用程序文件夹中,即使使用mp3播放器应用程序,我也无法播放音乐。您如何获取/生成正在编写的字节数组?你怎么知道它没有损坏?您的方法意味着您希望将某种数据“转换”为MP3数据,但我看不到发生“转换”。这些数据的性质是什么?这仍然不能说明数据的性质。数据是如何进入数据库的?它是从哪里来的?您如何知道最初存储在数据库中的数据没有损坏?据我们所知,您只需在数据库中放入随机的字节字符串。谢谢。发现了问题。它是关于权限的。我在回答中解释了这一点。