简体   繁体   中英

Calling pause on MediaPlayer from another class - Android java

I have a class named NowPlayingActivity and VideosActivity. Both are handled by a TabHost. At the start of the app, mediaplayer from NowPlayingActivity will play a song automatically. When I go to videos activity, there would be a list of videos available to play. What I want is to stop the song playing from NowPlayingActivity when I clicked an item from videosactivity so the song will stop at video play.

public void onItemClick(AdapterView parent, View v, int position, long id)
{
    NowPlayingActivity npa = new NowPlayingActivity();
    npa.pause();
    System.gc();
    videoColumnIndex = videoCursor.getColumnIndexOrThrow(
            MediaStore.Video.Media.DATA);
    videoCursor.moveToPosition(position);
    String filename = videoCursor.getString(videoColumnIndex);
    Log.i("FileName: ", filename);

    Intent intent = new Intent(Intent.ACTION_VIEW);
    Uri data = Uri.parse(filename);
    intent.setDataAndType(data, "video/*"); 
    startActivity(intent);
}

This is my code that starts and intent to play a selected video from the list, and play it on the gallery.

I tried creating a method pause from the NowPlayingActivity then call it on item click from videos activity.

public void pause(){

    if(mp.isPlaying()){
        if(mp!=null){
            mp.pause();
        }
    }
}

I don't know if this is possible but i can't think of any way but this.

EDIT:

Logcat
07-14 06:18:32.526: E/AndroidRuntime(979): FATAL EXCEPTION: main
07-14 06:18:32.526: E/AndroidRuntime(979): java.lang.NullPointerException
07-14 06:18:32.526: E/AndroidRuntime(979):  at com.example.prismmediaplayer.NowPlayingActivity.pause(NowPlayingActivity.java:410)
07-14 06:18:32.526: E/AndroidRuntime(979):  at com.example.prismmediaplayer.VideosActivity$1.onItemClick(VideosActivity.java:59)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.widget.AdapterView.performItemClick(AdapterView.java:298)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.widget.AbsListView.performItemClick(AbsListView.java:1086)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.widget.AbsListView$PerformClick.run(AbsListView.java:2855)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.widget.AbsListView$1.run(AbsListView.java:3529)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.os.Handler.handleCallback(Handler.java:615)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.os.Handler.dispatchMessage(Handler.java:92)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.os.Looper.loop(Looper.java:137)
07-14 06:18:32.526: E/AndroidRuntime(979):  at android.app.ActivityThread.main(ActivityThread.java:4745)
07-14 06:18:32.526: E/AndroidRuntime(979):  at java.lang.reflect.Method.invokeNative(Native Method)
07-14 06:18:32.526: E/AndroidRuntime(979):  at java.lang.reflect.Method.invoke(Method.java:511)
07-14 06:18:32.526: E/AndroidRuntime(979):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-14 06:18:32.526: E/AndroidRuntime(979):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-14 06:18:32.526: E/AndroidRuntime(979):  at dalvik.system.NativeStart.main(Native Method)

Put the MediaPlayer in your host activity. Then you can call your actions from either NowPlayingActivity or VideosActivity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM