4

I am creating a video player. I have implemented onPause method for getting last position of mediaplayer when home button is pressed and in onResume I am setting that location to mediaplayer and calling start. My question is that when I run the application on SDK 25 it works perfect but on SDK < 24 app crashes giving null pointer exception on media player

Following is used in onCreate Method:

mMediaPlayer.setOnVideoSizeChangedListener(this);


try {
    mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
    mMediaPlayer.setDataSource(this, Uri.parse(path));
    mMediaPlayer.setOnPreparedListener(this);
    mMediaPlayer.setOnCompletionListener(this);

} catch (IllegalArgumentException e) {
    e.printStackTrace();
} catch (SecurityException e) {
    e.printStackTrace();
} catch (IllegalStateException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

MediaPlayerActivity's onPause:

@Override
    protected void onPause() {
        super.onPause();
        position = mMediaPlayer.getCurrentPosition();
        resumedActivity = true;
        mMediaPlayer.pause();
        if(servicerunning) {
            startnotification();
        }
    }

onResume:

 @Override
    protected void onResume() {
        super.onResume();

        if(resumedActivity && !mMediaPlayer.isPlaying()) {
            mMediaPlayer.prepareAsync();
            mMediaPlayer.seekTo((int) position);
            mMediaPlayer.start();
            mPauseButton.setImageResource(R.drawable.ic_media_pause);
            if(servicerunning){
                videoService.removenotification();
            }

        }
    }




 @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {

        resetPlayer();
        return false;
    }

Android's Manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.fahadali.playerproject">

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WAKE_LOCK"/>



    <application
        android:allowBackup="true"
        android:hardwareAccelerated="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:largeHeap="true"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <service android:enabled="true" android:name=".MusicService" >

            </service>

        <service android:enabled="true" android:name=".VideoService" >

        </service>
        <receiver android:name=".AlarmReceiver"/>


        <activity
            android:name=".MainActivity"
            android:configChanges="orientation|screenSize"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="standard"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".FilesActivity"
            android:label="@string/title_activity_files"
            android:theme="@style/AppTheme.NoActionBar" />
        <activity
            android:name=".MediaPlayerActivity"
            android:configChanges="orientation|screenSize|keyboardHidden"
            android:label="@string/title_activity_media_player"
            android:theme="@style/AppTheme.NoActionBar"
            android:launchMode="singleTask">
            android:screenOrientation="portrait">
        </activity>
        <activity
            android:name=".PermissionsActivity"
            android:label="@string/title_activity_permissions"
            android:theme="@style/AppTheme.NoActionBar">

        </activity>
    </application>

</manifest>

Logcat:

02-01 18:34:36.465 17654-17654/com.example.fahadali.playerproject E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                    Process: com.example.fahadali.playerproject, PID: 17654
                                                                                    java.lang.RuntimeException: Unable to resume activity {com.example.fahadali.playerproject/com.example.fahadali.playerproject.MediaPlayerActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
                                                                                        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3194)
                                                                                        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3225)
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1506)
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                        at android.os.Looper.loop(Looper.java:160)
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5541)
                                                                                        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:964)
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759)
                                                                                     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
                                                                                        at com.example.fahadali.playerproject.MediaPlayerActivity.onResume(MediaPlayerActivity.java:1348)
                                                                                        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1257)
                                                                                        at android.app.Activity.performResume(Activity.java:6179)
                                                                                        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3183)
                                                                                        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3225) 
                                                                                        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1506) 
                                                                                        at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                        at android.os.Looper.loop(Looper.java:160) 
                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5541) 
                                                                                        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:964) 
                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:759) 

I can't find any solution. What can I try next?

halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

2

you can pause media player by using pause method and remember last position where you pause the media player like this:

Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();

and then you can resume player from the position you left by using this:

Mediaplayer.seekTo(length);
Mediaplayer.start();
Aj 27
  • 2,316
  • 21
  • 29
  • I want to resume the player when user resumes the app after pressing homebutton. Above code is running on SDK 24 and 25 but on SDK < 24 it gives nullpointerexception on mediaplayer – user8324061 Feb 01 '18 at 13:16