Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- E/MediaPlayer: error (100, 0)
- 01-20 16:39:44.028 E/MediaPlayer: Error (100,0)
- 01-20 16:39:44.028 E/MediaPlayer: stop called in state 0
- 01-20 16:39:44.028 E/MediaPlayer: error (-38, 0)
- E/AndroidRuntime: FATAL EXCEPTION: main
- Process: com.sterchodevlab.android.eplog, PID: 5949
- java.lang.RuntimeException: Error receiving broadcast Intent { act=com.sterchodevlab.android.eplog.PLAY flg=0x10 } in com.sterchodevlab.android.eplog.services.PlayService$6@fc0fe14
- at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:891)
- at android.os.Handler.handleCallback(Handler.java:739)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:148)
- at android.app.ActivityThread.main(ActivityThread.java:5417)
- at java.lang.reflect.Method.invoke(Native Method)
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.media.MediaPlayer.isPlaying()' on a null object reference
- at com.sterchodevlab.android.eplog.services.PlayService.playMusic(PlayService.java:157)
- at com.sterchodevlab.android.eplog.services.PlayService.access$600(PlayService.java:29)
- at com.sterchodevlab.android.eplog.services.PlayService$6.onReceive(PlayService.java:501)
- at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:881)
- at android.os.Handler.handleCallback(Handler.java:739)
- at android.os.Handler.dispatchMessage(Handler.java:95)
- at android.os.Looper.loop(Looper.java:148)
- package com.sterchodevlab.android.eplog.services;
- import android.app.Service;
- import android.content.BroadcastReceiver;
- import android.content.Context;
- import android.content.Intent;
- import android.content.IntentFilter;
- import android.media.AudioManager;
- import android.media.MediaPlayer;
- import android.os.Binder;
- import android.os.IBinder;
- import android.telephony.PhoneStateListener;
- import android.telephony.TelephonyManager;
- import android.util.Log;
- import com.sterchodevlab.android.eplog.broadcasts.ReceiverRegister;
- import com.sterchodevlab.android.eplog.intent_actions.MediaAction;
- import com.sterchodevlab.android.eplog.models.MusicItem;
- import com.sterchodevlab.android.eplog.preferences.AppPreferences;
- import com.sterchodevlab.android.eplog.preferences.MediaButtonsSetter;
- import com.sterchodevlab.android.eplog.preferences.PrefKeys;
- import com.sterchodevlab.android.eplog.random.CustomRandomEngine;
- import com.sterchodevlab.android.eplog.sort.SortMusicList;
- import com.sterchodevlab.android.eplog.storage.StorageUtil;
- import java.io.IOException;
- import java.util.ArrayList;
- public class PlayService extends Service implements MediaPlayer.OnErrorListener,
- MediaPlayer.OnCompletionListener,MediaPlayer.OnInfoListener,
- MediaPlayer.OnSeekCompleteListener,MediaPlayer.OnPreparedListener,
- MediaPlayer.OnBufferingUpdateListener,AudioManager.OnAudioFocusChangeListener,
- ReceiverRegister {
- public MediaPlayer backgroundPlayer;
- public AudioManager manager;
- private ArrayList<MusicItem> audioList;
- private ArrayList<MusicItem> shuffleAudioList;
- private int audioIndex = -1;
- private MusicItem activeAudio;
- private boolean isCall = false;
- private PhoneStateListener phoneStateListener;
- private TelephonyManager telephonyManager;
- private final IBinder mBinder = new ServiceBinder();
- private int length = 0;
- public class ServiceBinder extends Binder {
- public PlayService getService()
- {
- return PlayService.this;
- }
- }
- public PlayService() {
- }
- @Override
- public void onCreate() {
- super.onCreate();
- Log.i("on_create","OnCreate called");
- callListener();
- registerCustomReceivers();
- }
- public void initMP(){
- Log.i("init","InitMediaPlayer");
- if(backgroundPlayer==null)
- backgroundPlayer = new MediaPlayer();
- backgroundPlayer.setOnCompletionListener(this);
- backgroundPlayer.setOnErrorListener(this);
- backgroundPlayer.setOnPreparedListener(this);
- backgroundPlayer.setOnBufferingUpdateListener(this);
- backgroundPlayer.setOnSeekCompleteListener(this);
- backgroundPlayer.setOnInfoListener(this);
- backgroundPlayer.reset();
- backgroundPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
- try {
- backgroundPlayer.setDataSource(activeAudio.getImage().toString());
- } catch (IOException e) {
- e.printStackTrace();
- stopSelf();
- }
- backgroundPlayer.prepareAsync();
- }
- @Override
- public int onStartCommand( Intent intent, int flags, int startId) {
- Log.i("startCommand","onStartCommand");
- StorageUtil storage = new StorageUtil(getApplicationContext());
- audioList = storage.loadAudio();
- audioIndex = storage.loadAudioIndex();
- if (audioIndex != -1 && audioIndex < audioList.size()) {
- activeAudio = audioList.get(audioIndex);
- } else {
- stopSelf();
- }
- if (requestAudioFocus() == false) {
- stopSelf();
- }
- try{
- initMP();
- }catch (NullPointerException e){
- e.printStackTrace();
- stopSelf();
- }
- return super.onStartCommand(intent, flags, startId);
- }
- public void onPause(){
- Log.i("on_pause","OnPause");
- if(backgroundPlayer==null) return; //Костиль!!!!!!
- if (backgroundPlayer.isPlaying()) {
- backgroundPlayer.pause();
- length = backgroundPlayer.getCurrentPosition();
- }
- }
- public void resumeMusic()
- {
- Log.i("resume","ResumeMusic");
- if(backgroundPlayer==null) initMP();
- if (backgroundPlayer.isPlaying() == false) {
- backgroundPlayer.seekTo(length);
- backgroundPlayer.start();
- }
- }
- private void playMusic() {
- Log.i("play","PlayMusic");
- if (!backgroundPlayer.isPlaying()) {
- requestAudioFocus();
- backgroundPlayer.start();
- }
- }
- public void stopMusic()
- {
- Log.i("stop","StopMusic");
- if (backgroundPlayer == null) return;
- if(backgroundPlayer.isPlaying())
- {
- backgroundPlayer.stop();
- backgroundPlayer.release();
- backgroundPlayer = null;
- }
- }
- public boolean playOrNot(){
- return backgroundPlayer.isPlaying();
- }
- private void skipToNext() {
- if(new MediaButtonsSetter(getApplicationContext()).get(PrefKeys.SHUFFLE_KEY)){
- audioIndex= CustomRandomEngine.nextRandom(audioList.size());
- audioIndex--;
- }
- if (audioIndex == audioList.size() - 1) {
- //if last in playlist
- audioIndex = 0;
- activeAudio = audioList.get(audioIndex);
- } else {
- //get next in playlist
- activeAudio = audioList.get(++audioIndex);
- }
- //Update stored index
- new StorageUtil(getApplicationContext()).storeAudioIndex(audioIndex);
- stopMusic();
- //stopSelf();
- initMP();
- }
- private void skipToPrevious() {
- if (audioIndex == 0) {
- //if first in playlist
- //set index to the last of audioList
- audioIndex = audioList.size() - 1;
- activeAudio = audioList.get(audioIndex);
- } else {
- //get previous in playlist
- activeAudio = audioList.get(--audioIndex);
- }
- //Update stored index
- new StorageUtil(getApplicationContext()).storeAudioIndex(audioIndex);
- stopMusic();
- //stopSelf();
- initMP();
- }
- private void refreshAudioIndex(){
- for(int i=0;i<audioList.size();i++){
- if(activeAudio==audioList.get(i)){
- audioIndex=i;
- new StorageUtil(getApplicationContext()).storeAudioIndex(audioIndex);
- }
- }
- }
- public boolean requestAudioFocus() {
- Log.i("request","RequestAudioFocus");
- manager = (AudioManager) getSystemService(AUDIO_SERVICE);
- int result = manager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
- if (result == AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
- return true;
- }
- return false;
- }
- private boolean removeAudioFocus() {
- Log.i("remove","RemoveAudioFocus");
- if(manager!=null){
- if(AudioManager.AUDIOFOCUS_REQUEST_GRANTED ==
- manager.abandonAudioFocus(this)){
- return true;
- }
- }
- return false;
- }
- @Override
- public void onDestroy() {
- Log.i("on_destroy","OnDestroy");
- super.onDestroy();
- if(backgroundPlayer != null)
- {
- try{
- backgroundPlayer.stop();
- backgroundPlayer.release();
- }finally {
- backgroundPlayer = null;
- }
- }
- removeAudioFocus();
- if (phoneStateListener != null) {
- telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
- }
- unregisterReceiver(changeSourceofSound);
- unregisterReceiver(playNewAudio);
- unregisterReceiver(playNext);
- unregisterReceiver(playPrev);
- unregisterReceiver(play);
- unregisterReceiver(pause);
- unregisterReceiver(replay);
- unregisterReceiver(listUpdated);
- }
- @Override
- public IBinder onBind(Intent intent) {
- return mBinder;
- }
- @Override
- public boolean onUnbind(Intent intent) {
- return super.onUnbind(intent);
- }
- @Override
- public boolean onError(MediaPlayer mp, int what, int extra) {
- if(backgroundPlayer != null)
- {
- try{
- backgroundPlayer.stop();
- backgroundPlayer.release();
- }finally {
- backgroundPlayer = null;
- }
- }
- return false;
- }
- @Override
- public void onBufferingUpdate(MediaPlayer mp, int percent) {
- }
- @Override
- public void onCompletion(MediaPlayer mp) {
- MediaButtonsSetter checker=new MediaButtonsSetter(getApplicationContext());
- if(checker.get(PrefKeys.REPEAT_ONCE_KEY)){
- sendBroadcast(new Intent(MediaAction.REPLAY));
- checker.set(PrefKeys.REPEAT_ONCE_KEY,false);
- sendBroadcast(new Intent(MediaAction.UPDATE_BOTTOM_BUTTONS));
- }else if(checker.get(PrefKeys.REPEAT_KEY)){
- sendBroadcast(new Intent(MediaAction.REPLAY));
- }else{
- sendBroadcast(new Intent(MediaAction.COMPLETION));
- sendBroadcast(new Intent(MediaAction.PLAY_NEXT));
- }
- }
- @Override
- public void onAudioFocusChange(int focusChange) {
- Log.i("audiofocus_change","OnAudioFocusChange");
- switch (focusChange) {
- case AudioManager.AUDIOFOCUS_GAIN:
- Log.i("audiofocus_gain","AUDIOFOCUS_GAIN");
- if (backgroundPlayer == null) return;
- backgroundPlayer.setVolume(1.0f, 1.0f);
- break;
- case AudioManager.AUDIOFOCUS_LOSS:
- Log.i("audiofocus_loss","AUDIOFOCUS_LOSS");
- if(backgroundPlayer==null) return;
- if (backgroundPlayer.isPlaying()){
- sendBroadcast(new Intent(MediaAction.PAUSE));
- }
- break;
- case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
- Log.i("audiofocus_loss_trans","AUDIOFOCUS_LOSS_TRANSIENT");
- if (backgroundPlayer.isPlaying()){
- sendBroadcast(new Intent(MediaAction.PAUSE));
- }
- break;
- case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
- Log.i("audiofocus_loss_trans","AUDIOFOCUS_LOSS_TRANSIENT");
- if (backgroundPlayer.isPlaying()) backgroundPlayer.setVolume(0.1f, 0.1f);
- break;
- }
- }
- @Override
- public boolean onInfo(MediaPlayer mp, int what, int extra) {
- return false;
- }
- @Override
- public void onPrepared(MediaPlayer mp) {
- sendBroadcast(new Intent(MediaAction.PREPARED));
- sendBroadcast(new Intent(MediaAction.UPDATE_SCREEN_DATA));
- new MediaButtonsSetter(getApplicationContext()).set(PrefKeys.IS_PLAYING,true);
- sendBroadcast(new Intent(MediaAction.UPDATE_PLAY_PAUSE_BUTTON));
- playMusic();
- }
- @Override
- public void onSeekComplete(MediaPlayer mp) {
- }
- private void callListener(){
- telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- phoneStateListener = new PhoneStateListener() {
- @Override
- public void onCallStateChanged(int state, String incomingNumber) {
- switch (state) {
- case TelephonyManager.CALL_STATE_OFFHOOK:
- case TelephonyManager.CALL_STATE_RINGING:
- if (backgroundPlayer != null) {
- sendBroadcast(new Intent(MediaAction.PAUSE));
- isCall = true;
- }
- break;
- case TelephonyManager.CALL_STATE_IDLE:
- if (backgroundPlayer != null) {
- if (isCall) {
- isCall = false;
- boolean pausedUser=new MediaButtonsSetter(getApplicationContext())
- .get(PrefKeys.PAUSED_OF_USER);
- boolean preferenceCall= AppPreferences.getDefault(getApplicationContext()
- ,PrefKeys.RESTORE_AFTER_CALL);
- if(preferenceCall && !pausedUser){
- sendBroadcast(new Intent(MediaAction.PLAY));
- }
- }
- }
- break;
- }
- }
- };
- telephonyManager.listen(phoneStateListener,
- PhoneStateListener.LISTEN_CALL_STATE);
- }
- private void shuffleList(){
- shuffleAudioList= SortMusicList.shuffle(audioList);
- }
- private BroadcastReceiver changeSourceofSound=new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- sendBroadcast(new Intent(MediaAction.PAUSE));
- }
- };
- private BroadcastReceiver playNewAudio = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- //Get the new media index form SharedPreferences
- Log.i("tag","Тут");
- audioIndex = new StorageUtil(getApplicationContext()).loadAudioIndex();
- if (audioIndex != -1 && audioIndex < audioList.size()) {
- //index is in a valid range
- activeAudio = audioList.get(audioIndex);
- } else {
- stopSelf();
- }
- initMP();
- }
- };
- ///The Broadcast receivers initialization
- //The receiver created to play next track in playlist
- private BroadcastReceiver playNext = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- skipToNext();
- sendBroadcast(new Intent(MediaAction.UPDATE_SCREEN_DATA));
- }
- };
- //The receiver created to play previous track in playlist
- private BroadcastReceiver playPrev = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- skipToPrevious();
- sendBroadcast(new Intent(MediaAction.UPDATE_SCREEN_DATA));
- }
- };
- private BroadcastReceiver play = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- MediaButtonsSetter state= new MediaButtonsSetter(getApplicationContext());
- playMusic();
- //resumeMusic();
- state.set(PrefKeys.IS_PLAYING,true);
- state.set(PrefKeys.PAUSED_OF_USER,false);
- sendBroadcast(new Intent(MediaAction.UPDATE_PLAY_PAUSE_BUTTON));
- }
- };
- private BroadcastReceiver pause = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- MediaButtonsSetter state= new MediaButtonsSetter(getApplicationContext());
- onPause();
- state.set(PrefKeys.IS_PLAYING,false);
- sendBroadcast(new Intent(MediaAction.UPDATE_PLAY_PAUSE_BUTTON));
- }
- };
- private BroadcastReceiver replay = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- backgroundPlayer.pause();
- length=0;
- resumeMusic();
- MediaButtonsSetter setter=new MediaButtonsSetter(getApplicationContext());
- setter.set(PrefKeys.IS_PLAYING,true);
- sendBroadcast(new Intent(MediaAction.UPDATE_PLAY_PAUSE_BUTTON));
- }
- };
- private BroadcastReceiver listUpdated = new BroadcastReceiver() {
- @Override
- public void onReceive(Context context, Intent intent) {
- // audioList.clear();
- audioList=new StorageUtil(getApplicationContext()).loadAudio();
- refreshAudioIndex();
- }
- };
- /// Register receivers
- @Override
- public void registerCustomReceivers() {
- registerReceiver(changeSourceofSound, new IntentFilter(AudioManager.ACTION_AUDIO_BECOMING_NOISY));
- registerReceiver(playNewAudio, new IntentFilter(MediaAction.PLAY_NEW_AUDIO));
- registerReceiver(playNext, new IntentFilter(MediaAction.PLAY_NEXT));
- registerReceiver(playPrev, new IntentFilter(MediaAction.PLAY_PREV));
- registerReceiver(play, new IntentFilter(MediaAction.PLAY));
- registerReceiver(pause, new IntentFilter(MediaAction.PAUSE));
- registerReceiver(replay, new IntentFilter(MediaAction.REPLAY));
- registerReceiver(listUpdated, new IntentFilter(MediaAction.LIST_UPDATED));
- }
- }
Add Comment
Please, Sign In to add comment