在MediaPlayer-Android中播放字节数组
我正在尝试使用MediaPlayer播放音频文件。我想在MediaPlayer中播放字节数组。我该怎么做?我查过了 但它不播放音频。它只是在SD卡中生成许多文件。并给出此错误:在MediaPlayer-Android中播放字节数组,android,Android,我正在尝试使用MediaPlayer播放音频文件。我想在MediaPlayer中播放字节数组。我该怎么做?我查过了 但它不播放音频。它只是在SD卡中生成许多文件。并给出此错误: 06-06 11:02:59.191: E/MediaPlayer(1831): Unable to to create media player 06-06 11:02:59.191: W/System.err(1831): java.io.IOException: setDataSourceFD failed.:
06-06 11:02:59.191: E/MediaPlayer(1831): Unable to to create media player
06-06 11:02:59.191: W/System.err(1831): java.io.IOException: setDataSourceFD failed.: status=0x80000000
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(Native Method)
06-06 11:02:59.201: W/System.err(1831): at android.media.MediaPlayer.setDataSource(MediaPlayer.java:749)
06-06 11:02:59.201: W/System.err(1831): at org.vinuxproject.sonic.SonicTest$1.run(SonicTest.java:178)
06-06 11:02:59.201: W/System.err(1831): at java.lang.Thread.run(Thread.java:1096)
请帮帮我。感谢您的任何帮助
谢谢根据您的评论,您正在使用WAV文件进行流媒体传输。通常,文件的开头和其余部分都是纯数据。如果不包括标题部分,则需要向播放机提供其中的参数,以便能够解释数据(通道数、每秒采样数、块大小等)。因此,仅WAV文件是不可流化的,必须将参数输入播放机 另一方面,已指定能够流式传输。它包含可单独播放的可识别段,因此只要数据块包含标题,之后的数据就可以播放。除了它良好的压缩比,这也是许多互联网收音机使用它的原因之一 Android中的MediaPlayer是一个高级组件,播放WAV区块所需的低级参数不可访问。如果您需要的源是WAV,并且不能使用其他,那么您可以尝试该类,或者进行更低级别的访问。
对于AudioTrack,这是一个非常好的教程:在文件中写入一个字节后: 您可以通过此功能进行播放:
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();
}
您可以从这里获取文件信息:
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;
}
试试这个:
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();
}
}
在这里添加您的代码片段,有人会提供帮助。我建议尝试使用setDataSource变量,该变量除了FD之外还需要偏移量和长度。生成的mp3文件是否在音乐应用程序中播放?另外,deleteOnExit在Android/Dalvik中没有用处。我的应用程序中没有文件。请帮助我
samples
数组是否包含mp3编码的数据,或者仅包含原始PCM数据?如果使用createTempFile(String,String,File)
指定目录,这是否有效?外部/内部之间有区别吗?现在在哪里创建临时文件?嗯。。。audioTrack听起来可能是个不错的选择。如果我能帮上忙,请告诉我。wordpress链接已经“死”了,因为博客现在已经关闭了private@ForceGaia我们可以尝试请求访问该博客或尝试查找一些新教程。如果你有任何建议,请随意编辑这篇文章。
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();
}
}