我正在创建一个视频播放器。我已经实现了onPause方法,用于在按下主页按钮时获取mediaplayer的最后位置,在onResume中我将该位置设置为mediaplayer并调用start。我的问题是,当我在SDK 25上运行应用程序时,它可以完美地运行但是在SDK< 24个应用程序崩溃,在媒体播放器上发出空指针异常
以下用于onCreate方法:
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的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的清单:
<?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)
我找不到任何解决方案。任何帮助,将不胜感激。感谢
答案 0 :(得分:2)
您可以使用暂停方法暂停媒体播放器并记住暂停媒体播放器的最后位置,如下所示:
Mediaplayer.pause();
length=Mediaplayer.getCurrentPosition();
然后你可以使用这个从你离开的位置恢复玩家:
Mediaplayer.seekTo(length);
Mediaplayer.start();