简体   繁体   中英

Application crash with no exception after resuming

I am making simple app to display video and plaing audio playback if activity is in background.
My app works fine if i just turn screen off/on.
It crashes when I resume my app after brawsing another application. Log cat show no error.

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import com.squareup.otto.Subscribe;

public class MainActivity
        extends Activity
        implements android.view.SurfaceHolder.Callback {

    private SurfaceView   surfaceViewFrame;
    private SurfaceHolder holder;
    private Bundle        extras;
    private static final String  TAG = "log_tag";
    private              boolean b   = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        surfaceViewFrame = (SurfaceView) findViewById(R.id.surfaceView);
        surfaceViewFrame.setClickable(false);

        holder = surfaceViewFrame.getHolder();
        holder.addCallback(this);


    }


    @Subscribe
    public void attachPlayer(MediaPlayer player) {
        player.setDisplay(holder);

    }


    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }


    public void surfaceCreated(SurfaceHolder holder) {
        startService(new Intent(this, MediaPlayerService.class));

    }


    public void surfaceDestroyed(SurfaceHolder holder) {

    }


    @Override
    protected void onStart() {
        super.onStart();
        BusProvider.getInstance().register(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        BusProvider.getInstance().unregister(this);
    }

Service

import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.IBinder;
import android.util.Log;

import java.io.IOException;

public class MediaPlayerService
        extends Service
        implements MediaPlayer.OnPreparedListener,
                   MediaPlayer.OnCompletionListener,

                   MediaPlayer.OnSeekCompleteListener {

    private static final String TAG = "MediaService";

    private static final String ACTION_PLAY = "com.example.action.PLAY";
    private MediaPlayer player;
    public String[]
            video_path =
            {"https://ellovidsout.s3.amazonaws.com/1265/9/1422967594.mp4.m3u8", "https://ellovidsout.s3.amazonaws.com/1260/9/1422887544.mp4.m3u8"};


    @Override
    public void onCreate() {
        super.onCreate();
        player = new MediaPlayer();
        player.setOnPreparedListener(this);
        player.setOnCompletionListener(this);

        player.setOnSeekCompleteListener(this);
        player.setScreenOnWhilePlaying(true);

    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {


        playVideo();

        Notification note = new Notification(
                R.drawable.ic_launcher, "Can you hear the music?", System.currentTimeMillis()
        );
        Intent i = new Intent(this, MediaPlayerService.class);
        i.setFlags(
                Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP
        );
        PendingIntent pi = PendingIntent.getActivity(
                this, 0, i, 0
        );
        note.setLatestEventInfo(
                this, "Fake Player", "Now Playing: \"Ummmm, Nothing\"", pi
        );
        note.flags |= Notification.FLAG_NO_CLEAR;
        startForeground(1337, note);
        return super.onStartCommand(intent, flags, startId);
    }


    @Override
    public IBinder onBind(Intent intent) {

        return null;
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        player.stop();
        player.release();
        player = null;
    }


    private Thread playback = new Thread(
            new Runnable() {

                public void run() {
                    try {

                        try {
                            player.setDataSource(
                                    MediaPlayerService.this, Uri.parse(
                                            video_path[0]
                                    )
                            );
                            player.prepareAsync();
                        } catch (IOException e) {
                            Log.e(TAG, "Error while playing video: " + e.getMessage());
                        }

                    } catch (IllegalArgumentException e) {
                        Log.e(TAG, "Error while playing video: " + e.getMessage());
                    }
                }
            }
    );


    private void playVideo() {
        if (player!=null&&!playback.isAlive()) playback.start();
    }


    public void onPrepared(MediaPlayer mp) {
        if (!player.isPlaying()) {
            player.start();
            BusProvider.getInstance()
                    .post(player);
        }

    }


    public void onCompletion(MediaPlayer mp) {
        try {
            player.setDataSource(
                    MediaPlayerService.this, Uri.parse(
                            video_path[0]
                    )
            );
        } catch (IOException e) {
            Log.e(TAG, "Error while playing video: " + e.getMessage());

        }
        player.prepareAsync();

    }


    public void onSeekComplete(MediaPlayer mp) {

    }
}
  1. you missed in onStop:

    @Override protected void onStop() { holder.removeCallback(this); super.onStop(); }

  2. please add the crash report

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