MediaPlayer onCompleteListener不会在服务中的后台应用程序上触发

时间:2015-09-30 07:29:05

标签: android

我在服务中实现了MediaPlayer以在后台播放文件。我开始foregroundNotification来实现这一目标。我遇到的唯一问题是,当一首歌完成时,onCompleteListener方法不会触发它应该如何,但是当我在应用中它会触发时。

public class PlayerService extends Service implements MediaPlayer.OnPreparedListener,
        MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener,
        MediaPlayer.OnBufferingUpdateListener{

    private static final String ACTION_PLAY = "action.PLAY";
    private static final String ACTION_PAUSE = "action.PAUSE";
    private static final String ACTION_RESUME = "action.RESUME";
    private static final String ACTION_SEEK = "action.SEEK_TO";
    private static final String ACTION_NEXT = "action.NEXT";
    private static final String ACTION_PREVIOUS = "action.PREVIOUS";
    private static final String ACTION_REPEAT = "action.REPEAT";
    private MediaPlayer mediaPlayer = null;
    private String curentURL;
    private boolean repeat = false;
    private int currentPosition;

    private final BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Log.i("TEST", "Primesc comanda prin BroadcastReceiver " + intent.getAction());
            String action = intent.getAction();
            if(action.equals(ACTION_PLAY)){
                curentURL = intent.getStringExtra("musicURL");
                currentPosition = intent.getIntExtra("musicPosition", 0);
                try {
                    if(mediaPlayer.isPlaying()) {
                        mediaPlayer.stop();
                    }
                    mediaPlayer.reset();
                    mediaPlayer.setDataSource(intent.getStringExtra("musicURL"));
                    mediaPlayer.prepareAsync();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            else if(action.equals(ACTION_PAUSE)){
                if(mediaPlayer.isPlaying())
                    mediaPlayer.pause();
            } else if(action.equals(ACTION_RESUME)){
                if(!mediaPlayer.isPlaying())
                    mediaPlayer.start();
            } else if(action.equals(ACTION_SEEK)){
                mediaPlayer.seekTo(intent.getIntExtra("seekProgress", 0) * 100);
                Log.i("TEST","Dau seek la "+intent.getIntExtra("seekProgress", 0));
            } else if(action.equals(ACTION_NEXT)){
                Intent nextIntent = new Intent("action.activity.NEXT");
                intent.putExtra("nextPosition", currentPosition+1);
                sendBroadcast(nextIntent);
            } else if(action.equals(ACTION_PREVIOUS)){
                Intent previousIntent = new Intent("action.activity.PREVIOUS");
                intent.putExtra("previousPosition", currentPosition-1);
                sendBroadcast(previousIntent);
            } else if(action.equals(ACTION_REPEAT)) {
                repeat = intent.getBooleanExtra("repeatStatus", false);
            }
        }
    };

    @Override
    public void onCreate() {
        mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mediaPlayer.setOnPreparedListener(this);
        mediaPlayer.setOnCompletionListener(this);
        mediaPlayer.setOnErrorListener(this);
        mediaPlayer.setOnBufferingUpdateListener(this);

        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_PLAY);
        filter.addAction(ACTION_PAUSE);
        filter.addAction(ACTION_NEXT);
        filter.addAction(ACTION_RESUME);
        filter.addAction(ACTION_PREVIOUS);
        filter.addAction(ACTION_SEEK);
        filter.addAction(ACTION_REPEAT);
        registerReceiver(receiver, filter);

        Notification notification = new Notification(R.drawable.playlisticon, getText(R.string.ticker_text),
                System.currentTimeMillis());
        Intent notificationIntent = new Intent(this, musicPlayer.class);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
        notification.setLatestEventInfo(this, getText(R.string.notification_title),
                getText(R.string.notification_message), pendingIntent);
        startForeground(1337, notification);

        Log.i("TEST","onCreate se apeleaza");
    }

    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
    }

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

        Log.i("TEST","A pornit service-ul");

        return START_STICKY;
    }

    @Override
    public IBinder onBind(Intent intent) {
        Log.i("TEST", "Se face onBind");
        return null;
    }

    @Override
    public void onPrepared(MediaPlayer mp) {
        mp.start();

        Intent intent = new Intent("action.activity.UPDATE_UI");
        intent.putExtra("duration", mp.getDuration());
        sendBroadcast(intent);
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {

        Log.i("TEST", "Eroare incerc din nou "+what+" "+extra);

        try {
            if(mediaPlayer.isPlaying()) {
                mediaPlayer.stop();
            }
            mediaPlayer.reset();
            mediaPlayer.setDataSource(curentURL);
            mediaPlayer.prepareAsync();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return true;
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        Log.i("TEST", "Action onComplete");

        if(repeat) {
            Intent intent = new Intent("action.activity.REPEAT");
            intent.putExtra("currentPosition", currentPosition);
            sendBroadcast(intent);
        } else {
            Intent intent = new Intent("action.activity.NEXT");
            intent.putExtra("nextPosition", currentPosition+1);
            sendBroadcast(intent);
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mp, int percent) {

        Intent intent = new Intent("action.activity.UPDATE_BUFFER");
        intent.putExtra("progress", percent);
        intent.putExtra("seekBarProgress", mp.getCurrentPosition() / 100);
        intent.putExtra("mediaCurentPosition", mp.getCurrentPosition());
        sendBroadcast(intent);

        Log.i("TEST","Current Position = "+mp.getCurrentPosition());

    }

您可以看到我的服务implements MediaPlayer的某些方法并在onCreate

中启动它们

0 个答案:

没有答案