大家好,我们正在测试我的基本媒体播放器,它是为应用启动声音创建的。媒体是从我的firebase数据库中包含的URL中提取的。媒体播放器可以在早期的模拟器上运行,但不能在任何真实设备上运行..
错误代码是在状态0中调用的MediaPlayer启动(错误-38,0) 香港专业教育学院读它是因为在播放媒体之前,onprepare还没有准备好。所以我添加了一个onPreparelistener,它仍然给出了同样的错误。任何帮助的人请
public class harropMediaplayer {
MediaPlayer player;
String media;
Context c;
public harropMediaplayer(String media,Context c){
this.media = media;
this.c = c;
Log.i("Sound: ","Initalized");
}
public void volumeSetting(){
SharedPreferences sharedpreferences = PreferenceManager.getDefaultSharedPreferences(c);
boolean bnSoundMute = sharedpreferences.getBoolean("soundMute",false);
if(bnSoundMute==true){
mutevol();
Log.i("sound","muted");
}else{
volmax();
Log.i("sound","max vol");
}
}
public void plysound() {
player = new MediaPlayer();
Log.i("Url", media);
try {
player.setDataSource(media);
} catch (IOException e) {
e.printStackTrace();
}
catch (IllegalStateException o){
o.printStackTrace();
}
try {
player.prepare();
volumeSetting();
player.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp) {
if (!mp.isPlaying()){
mp.start();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
Log.i("Sound playing", "Ok");
}
public void stopplying(){
player.stop();
}
public void volmax(){
player = App_Objects.mp;
player.setVolume(1,1);
}
public void mutevol(){
player = App_Objects.mp;
player.setVolume(0,0);
}
答案 0 :(得分:1)
onPrepareListener与准备不一样,你只需要调用:
mediaPlayer.prepare();
答案 1 :(得分:0)
您需要在启动之前准备好媒体播放器。使用mediaplayer.prepare()
在这种情况下,一旦设置了源 呼叫 Player.prepare(); Player.start();