这是用于从其他类动态获取文件名(.mp3)的代码,因为我的assets文件夹中有很多mp3文件:
playAudioButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
FileName audio=new FileName();
String audioName=audio.getAudioName(count).toString();
if(audioName=="NO Audio")
{
Toast.makeText(getApplicationContext(), "No Audio for this page", Toast.LENGTH_SHORT).show();
}
else
{
try {
afd=getAssets().openFd(audioName + ".mp3");
mp = new MediaPlayer();
mp.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(),afd.getLength());
mp.prepareAsync();
mp.start();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
Log Cat:
03-20 11:21:28.726: E/SpannableStringBuilder(4418): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-20 11:21:28.726: E/SpannableStringBuilder(4418): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
03-20 11:22:24.968: D/libEGL(4579): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
03-20 11:22:24.976: D/libEGL(4579): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
03-20 11:22:24.984: D/libEGL(4579): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
03-20 11:22:25.070: D/OpenGLRenderer(4579): Enabling debug mode 0
03-20 11:22:38.773: E/MediaPlayer(4579): start called in state 4
03-20 11:22:38.773: E/MediaPlayer(4579): error (-38, 0)
03-20 11:22:38.773: E/MediaPlayer(4579): Error (-38,0)
需要注意的要点: 1.我在堆栈溢出时检查了类似的线程,答案是使用prepareAsync() 2.我使用了prepareAsync()
提前致谢。
答案 0 :(得分:20)
您还可以使用以下onPrepared()
方法调用start方法,以便在start()
准备好后调用MediaPlayer
。
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
答案 1 :(得分:2)
您可以将转换为URI的文件路径传递给preparedSync/prepare
MediaPlayer
的{{1}}方法。
MediaPlayer.create()
这将准备并开始播放
或者另一种方法是使用MediaPlayer mediaPlayer = MediaPlayer.create(context, URI.parse("file://"+filePath));
的{{1}}然后从准备开始播放
答案 2 :(得分:0)
状态4表示Mediaplayer正在准备状态
我们称其他行为如
Mediaplayer.start()
Mediaplayer.stop()
Mediaplayer.pause()
或任何其他事情。
根据您的代码,Mediaplayer.start();
你需要编写onprepare方法
MediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.start();
}
});
一旦调用了Onprepared()方法,就可以进行进一步的处理
删除
mp.start()
后
mp.prepareAsync();
你会很高兴
答案 3 :(得分:0)
public class BackgroundSound extends AsyncTask<String, Void, Void> {
MediaPlayer mediaPlayerChatAudio;
@Override
protected Void doInBackground(String... params) {
String model = params[0];
final Uri uri = Uri.parse(model);
LogEm.e("EM", "::::::::::URI::::::::::" + model);
if (uri == null || uri == Uri.EMPTY) return null;
if (mediaPlayerChatAudio != null) mediaPlayerChatAudio.stop();
try {
mediaPlayerChatAudio = MediaPlayer.create(TextActivity.this, uri);
mediaPlayerChatAudio.setAudioStreamType(AudioManager.STREAM_MUSIC);
} catch (Exception e) {
// do nothing.
}
if (mediaPlayerChatAudio == null) return null;
mediaPlayerChatAudio.setVolume(1.0f, 1.0f);
mediaPlayerChatAudio.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
mediaPlayerChatAudio.reset();
mediaPlayerChatAudio.release();
mediaPlayerChatAudio = null;
}
});
mediaPlayerChatAudio.start();
return null;
}
}