ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

android – 从onResume的最后一个位置恢复MediaPlayer

2019-07-01 15:14:45  阅读:308  来源: 互联网

标签:android nullpointerexception android-mediaplayer onresume onpause


我正在创建一个视频播放器.我已经实现了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) 

我找不到任何解决方案.任何帮助,将不胜感激.谢谢

解决方法:

您可以使用暂停方法暂停媒体播放器并记住暂停媒体播放器的最后位置,如下所示:

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

然后你可以使用这个从你离开的位置恢复玩家:

Mediaplayer.seekTo(length);
Mediaplayer.start();

标签:android,nullpointerexception,android-mediaplayer,onresume,onpause
来源: https://codeday.me/bug/20190701/1347794.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有