该应用程序在所有设备上都能完美运行,但在三星galaxy note 3和j5中,它在播放声音时崩溃了



//playSound方法并在的任何地方使用它

public void playSound1() {
mp = MediaPlayer.create(getBaseContext(), (R.raw.clave_pop));  //sound file clave_pop in mp4 format firstly it is in m4a format then produced same problem then i use mp3 file
if (sec_sound && mp != null)  //shared preference of sec_sound
mp.start();
if (vibration)//also vibrate if shared preference of vibrator is true
v.vibrate(700);   //vibrate device
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
// and i am also used this but the same problem is occurred
public void playSound1() {
final MediaPlayer mp = MediaPlayer.create(Anulom_Activity.this, R.raw.clave_pop);  //sound file clave_pop in mp4 format firstly it is in m4a format then produced same problem then i use mp3 file
if (sec_sound && mp != null) //shared preference of sec_sound
mp.start();
if (vibration) //also vibrate if shared preference of vibrator is true
v.vibrate(700);  //device vibrate
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
mp.release();
}
});
}
// but it shows 
/* java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaPlayer.setOnCompletionListener(android.media.MediaPlayer$OnCompletionListener)' on a null object reference
at com.pixelpoint.Anulom_Activity.playSound1(Anulom_Activity.java:727)
at com.pixelpoint.Anulom_Activity$5$1$1.run(Anulom_Activity.java:325)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5910)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1405)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) */
MediaPlayer create (Context context,Uri uri)

为给定的Uri创建MediaPlayer的便利方法。成功后,prepare()将已被调用,并且不能再次呼叫。使用MediaPlayer后,应该调用release()来释放资源。如果不发布,将会有太多MediaPlayer实例导致异常。请注意,由于prepare()在该方法中是自动调用的,因此无法更改音频流类型(请参见setAudioStreamType(int)),音频会话ID(请参阅setAudioSessionId(int))或音频属性(请参阅setAudioAttributes(AudioAttributes)。参数上下文上下文:要使用的上下文uri uri:从中获取数据源的uri退货MediaPlayer MediaPlayer对象,如果创建失败则为null取消选择跟踪

prepare () 

为玩家做好准备播放,同步。设置数据源和显示后曲面,则需要调用prepare()或prepareSync()。对于文件,可以调用prepare(),它会阻塞直到MediaPlayer准备播放。

根据上述文档,您需要调用prepare或prepare async并设置Onpreparelistener。等待回电,然后调用mp.start或设置OnComplement侦听器。

相关内容

最新更新