예제 #1
0
    @Override
    public void onAudioFocusChange(int i) {

      if (musicFocusable == null) {
        return;
      }

      switch (i) {
        case AudioManager.AUDIOFOCUS_GAIN:
          Logger.debug(TAG, ">>>" + "onAudioFocusChange AUDIOFOCUS_GAIN");
          musicFocusable.onGainedAudioFocus();
          break;

        case AudioManager.AUDIOFOCUS_LOSS:
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
          Logger.debug(TAG, ">>>" + "onAudioFocusChange AUDIOFOCUS_LOSS");
          musicFocusable.onLostAudioFocus(false);
          break;
        case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK:
          Logger.debug(TAG, ">>>" + "onAudioFocusChange AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK");
          musicFocusable.onLostAudioFocus(true);
          break;
        default:
      }
    }
  @Override
  public void onReceive(Context context, Intent intent) {
    Logger.debug(TAG, ">>>" + "onReceive>>>>>>>>>>>>");

    // get data from daily hello chao
    //        AppAPI.getInstance().getHcDaily();
  }
예제 #3
0
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    String action = intent.getAction();
    Logger.debug(TAG, ">>>" + "onStartCommand action:" + action);

    if (ACTION_TOGGLE_PLAYBACK.equals(action)) {

    } else if (ACTION_PAUSE.equals(action)) {

    } else if (ACTION_PLAY.equals(action)) {

    } else if (ACTION_REWIND.equals(action)) {

    } else if (ACTION_SKIP.equals(action)) {

    } else if (ACTION_STOP.equals(action)) {

    } else if (ACTION_URL.equals(action)) {
      //

    }

    return START_STICKY; // we started the service, but we don't want it restart if it was killed by
                         // system,
  }
예제 #4
0
  public void playNextSong(MusicItem musicItem) {
    String url = musicItem.audio;
    Logger.debug(TAG, ">>>" + "playNextSong:" + url);
    if (musicPlayBackCallback != null) {
      musicPlayBackCallback.onPreparing(currentItem);
    }
    mState = State.Stopped;

    relaxResource(false);
    try {
      setupMediaPlayerIfNeeded();

      mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
      mediaPlayer.setDataSource(url);
      isStreaming = url.startsWith("http:") || url.startsWith("https:");

      songTitle = url;

      mState = State.Preparing;
      setupAsForeGround(musicItem);

      /** @see onPreparedListener */
      mediaPlayer.prepareAsync();

      if (isStreaming) {
        wifiLock.acquire();
      } else if (wifiLock.isHeld()) {
        wifiLock.release();
      }

    } catch (Exception e) {
      e.printStackTrace();
    }
  }
예제 #5
0
 private void tryToGetAudioFocus() {
   Logger.debug(TAG, ">>>" + "tryToGetAudioFocus");
   if (audioFocus != AudioFocus.Focused
       && audioFocusHelper != null
       && audioFocusHelper.requestFocus()) {
     audioFocus = AudioFocus.Focused;
   }
 }
예제 #6
0
 public void processPauseRequest() {
   Logger.debug(TAG, ">>>" + "processPauseRequest");
   if (mState == State.Playing) {
     mState = State.Paused;
     mediaPlayer.pause();
     relaxResource(false);
   }
 }
예제 #7
0
  public void processAddRequest(MusicItem musicItem) {
    Logger.debug(TAG, ">>>" + "processAddRequest");
    listMusic.clear();
    currentPos = 0;
    this.currentItem = musicItem;

    tryToGetAudioFocus();
    playNextSong(musicItem);
  }
예제 #8
0
 @Override
 public void onPrepared(MediaPlayer mediaPlayer) {
   Logger.debug(TAG, ">>>" + "onPrepared");
   if (musicPlayBackCallback != null) {
     musicPlayBackCallback.onPlaying(currentItem);
   }
   mState = State.Playing;
   updateNotification(currentItem);
   configAndStartMediaPlayer();
 }
예제 #9
0
 public void processPlayRequest() {
   Logger.debug(TAG, ">>>" + "processPlayRequest");
   tryToGetAudioFocus();
   if (mState == State.Stopped) {
     if (!listMusic.isEmpty() && currentItem != null) {
       currentItem = listMusic.get(currentPos);
       playNextSong(currentItem);
     }
   } else if (mState == State.Paused) {
     mState = State.Playing;
     setupAsForeGround(currentItem);
     configAndStartMediaPlayer();
   }
 }
예제 #10
0
  @Override
  public void onCreate() {
    super.onCreate();
    Logger.debug(TAG, ">>>" + "onCreate");
    audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    notificationManagerCompat = NotificationManagerCompat.from(getApplicationContext());

    audioFocusHelper = new AudioFocusHelper(getApplicationContext(), musicFocusable);
    componentName = new ComponentName(this, MusicIntentReceiver.class);

    wifiLock =
        ((WifiManager) getSystemService(WIFI_SERVICE))
            .createWifiLock(WifiManager.WIFI_MODE_FULL, "myLock");
  }
예제 #11
0
  private void setupMediaPlayerIfNeeded() {
    Logger.debug(TAG, ">>>" + "setupMediaPlayerIfNeeded");
    if (mediaPlayer == null) {
      mediaPlayer = new MediaPlayer();
      mediaPlayer.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);

      mediaPlayer.setOnPreparedListener(onPreparedListener);
      mediaPlayer.setOnCompletionListener(onCompletionListener);
      mediaPlayer.setOnErrorListener(onErrorListener);

    } else {
      mediaPlayer.reset();
    }
  }
  @Override
  protected void initLayout(View view) {

    getAActivity().setSupportActionBar(toolbar);
    getAActivity().getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getAActivity().getSupportActionBar().setTitle("Practice");
    collapsingToolbarLayout.setTitle("My Practice");
    LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
    linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

    recyclerView.setLayoutManager(linearLayoutManager);
    recyclerView.setHasFixedSize(true);
    list.clear();
    list.addAll(ResourceManager.getInstance().getListHelloChaoDaily());
    Logger.debug(TAG, ">>>" + "size:" + list.size());
    adapterFull = new AdapterFull(getContext(), list);
    recyclerView.setAdapter(adapterFull);
  }
예제 #13
0
 private void updateNotification(MusicItem musicItem) {
   Logger.debug(TAG, ">>>" + "updateNotification:" + musicItem);
   if (musicItem == null) {
     return;
   }
   Intent intent = new Intent(getApplicationContext(), ScrollingActivity.class);
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
   PendingIntent pi =
       PendingIntent.getActivity(
           getApplicationContext(), 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
   Notification builder =
       new NotificationCompat.Builder(getApplicationContext())
           .setContentText(musicItem.text)
           .setOngoing(false)
           .setContentIntent(pi)
           .setTicker(musicItem.text)
           .setContentTitle("Daily Practice!")
           .setSmallIcon(R.mipmap.ic_launcher)
           .build();
   notificationManagerCompat.notify(12, builder);
 }
예제 #14
0
 @Override
 public IBinder onBind(Intent intent) {
   Logger.debug(TAG, ">>>" + "onBind");
   return localBinder;
 }
예제 #15
0
 public MusicService() {
   Logger.debug(TAG, ">>>" + "MusicService");
 }