在activity和Fragment之间调用MediaPlayer和AudioData

时间:2016-06-12 23:23:15

标签: android android-fragments android-activity android-mediaplayer

在Activity和Fragment之间使用MediaPlayer时遇到了一些问题。所以,对于今天的项目,我必须播放音频数据。我将音频定义放在片段上,如下所示:

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    //define image for arabic text
    p01 = (ImageView) getActivity().findViewById(R.id.p02_01);
    p02 = (ImageView) getActivity().findViewById(R.id.p02_02);
    p03 = (ImageView) getActivity().findViewById(R.id.p02_03);
    p04 = (ImageView) getActivity().findViewById(R.id.p02_04);
    p05 = (ImageView) getActivity().findViewById(R.id.p02_05);

    //catch total list id and audio id must be exactly same
    totalData = 18;

    //define arabic text list id
    arabic = new ImageView[totalData];
    arabic[0] = p01;
    arabic[1] = p02;
    arabic[2] = p03;
    arabic[3] = p04;
    arabic[4] = p05;

    //set click listener to imageview
    for (int i=0; i<arabic.length;i++){arabic[i].setOnClickListener(this);}

    //define audio id
    track = new int[totalData];
    track[0] = R.raw.a0201_batasa;
    track[1] = R.raw.a0202_bata;
    track[2] = R.raw.a0203_taba;
    track[3] = R.raw.a0204_ata;
    track[4] = R.raw.a0205_tata;
    track[5] = R.raw.a0206_taba;
}

使用OnClick(),我使用其代码从父活动中调用方法。

parent().playAudio(track[0],2);

这是使用MediaPlayer在其父活动中播放音频的方法:

public void playAudio(int tracknumber, int activeFragment){
    activeFragmentID = activeFragment;

    if(isPlaying == 1){
        if(mediaPlayer.isPlaying()){
            mediaPlayer.release();
            isPlaying = 0;
        }
    }

    isPlaying = 1;
    mediaPlayer = MediaPlayer.create(getApplicationContext(), tracknumber);
    mediaPlayer.setOnCompletionListener(this);
    mediaPlayer.start();
}

在此之前,音频播放正确,所以我得救了。现在,我在活动中有两个按钮。如果我按下其中一个按钮,则必须播放相同的音频。所以,从逻辑上讲,我在Fragment创建了一个新方法,就像这样:

public void autoplay(){
        parent().playAudio(track[0], 2);
}

但是,不幸的是,当我尝试从Activity调用该函数时,我遇到了这个错误。

06-12 19:20:01.370 5310-5310/com.client.iqromobile E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.client.iqromobile, PID: 5310
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.client.iqromobile/com.client.iqromobile.Page}: java.lang.NullPointerException
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:136)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:515)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                      Caused by: java.lang.NullPointerException
                                                                         at com.client.iqromobile.Page$override.startAuto(Page.java:183)
                                                                         at com.client.iqromobile.Page$override.access$dispatch(Page.java)
                                                                         at com.client.iqromobile.Page.startAuto(Page.java:0)
                                                                         at com.client.iqromobile.Page$override.autoPlay(Page.java:113)
                                                                         at com.client.iqromobile.Page$override.onCreate(Page.java:97)
                                                                         at com.client.iqromobile.Page$override.access$dispatch(Page.java)
                                                                         at com.client.iqromobile.Page.onCreate(Page.java:0)
                                                                         at android.app.Activity.performCreate(Activity.java:5231)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:136) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                         at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                         at dalvik.system.NativeStart.main(Native Method) 

有没有人可以向我解释为什么?那么,我应该如何在活动和片段之间传递音频数据?谢谢你的回答。

0 个答案:

没有答案