Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/news-android.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid-Development <david-dev@live.de>2017-02-01 02:32:22 +0300
committerDavid-Development <david-dev@live.de>2017-02-01 02:32:22 +0300
commit1ad975f3d574dfa04fb4f4a960b701e99119e9f1 (patch)
tree5297dfd35a96de60fc532683e8fdbdd6c9978764 /News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/services/PodcastPlaybackService.java
parent95147eeea88874ab140d0d4447fab136d33121a1 (diff)
Fix #549
Diffstat (limited to 'News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/services/PodcastPlaybackService.java')
-rw-r--r--News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/services/PodcastPlaybackService.java351
1 files changed, 77 insertions, 274 deletions
diff --git a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/services/PodcastPlaybackService.java b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/services/PodcastPlaybackService.java
index 4bbc45f3..7476d19d 100644
--- a/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/services/PodcastPlaybackService.java
+++ b/News-Android-App/src/main/java/de/luhmer/owncloudnewsreader/services/PodcastPlaybackService.java
@@ -2,47 +2,47 @@ package de.luhmer.owncloudnewsreader.services;
import android.app.Service;
import android.content.Intent;
-import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
-import android.speech.tts.TextToSpeech;
-import android.speech.tts.UtteranceProgressListener;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
-import android.view.SurfaceHolder;
import android.view.View;
-import android.widget.Toast;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
-import java.io.IOException;
-import java.util.HashMap;
-
-import de.luhmer.owncloudnewsreader.R;
import de.luhmer.owncloudnewsreader.events.podcast.NewPodcastPlaybackListener;
import de.luhmer.owncloudnewsreader.events.podcast.PodcastCompletedEvent;
import de.luhmer.owncloudnewsreader.events.podcast.RegisterVideoOutput;
+import de.luhmer.owncloudnewsreader.events.podcast.RegisterYoutubeOutput;
import de.luhmer.owncloudnewsreader.events.podcast.TogglePlayerStateEvent;
import de.luhmer.owncloudnewsreader.events.podcast.UpdatePodcastStatusEvent;
import de.luhmer.owncloudnewsreader.events.podcast.WindPodcast;
+import de.luhmer.owncloudnewsreader.model.MediaItem;
import de.luhmer.owncloudnewsreader.model.PodcastItem;
import de.luhmer.owncloudnewsreader.model.TTSItem;
+import de.luhmer.owncloudnewsreader.services.podcast.MediaPlayerPlaybackService;
+import de.luhmer.owncloudnewsreader.services.podcast.PlaybackService;
+import de.luhmer.owncloudnewsreader.services.podcast.TTSPlaybackService;
+import de.luhmer.owncloudnewsreader.services.podcast.YoutubePlaybackService;
import de.luhmer.owncloudnewsreader.view.PodcastNotification;
-public class PodcastPlaybackService extends Service implements TextToSpeech.OnInitListener {
+public class PodcastPlaybackService extends Service {
// Binder given to clients
private final IBinder mBinder = new LocalBinder();
- public PodcastItem getCurrentlyPlayingPodcast() {
- return mCurrentlyPlayingPodcast;
+ public MediaItem getCurrentlyPlayingPodcast() {
+ if(mPlaybackService != null) {
+ return mPlaybackService.getMediaItem();
+ }
+ return null;
}
public boolean isActive() {
- return mCurrentlyPlayingPodcast != null || mCurrentlyPlayingTTS != null;
+ return mPlaybackService != null;
}
/**
@@ -75,72 +75,33 @@ public class PodcastPlaybackService extends Service implements TextToSpeech.OnIn
return super.onUnbind(intent);
}
- public static final String PODCAST_ITEM = "PODCAST_ITEM";
- public static final String TTS_ITEM = "TTS_ITEM";
-
- private PodcastItem mCurrentlyPlayingPodcast;
- private TTSItem mCurrentlyPlayingTTS;
+ public static final String MEDIA_ITEM = "MediaItem";
private static final String TAG = "PodcastPlaybackService";
private PodcastNotification podcastNotification;
private EventBus eventBus;
private Handler mHandler;
- private MediaPlayer mMediaPlayer;
-
- private TextToSpeech ttsController;
- private String mediaTitle;
- private PlaybackType mPlaybackType;
+ private PlaybackService mPlaybackService;
private View parentResizableView;
- private enum PlaybackType { PODCAST, TTS }
+
@Override
public void onCreate() {
Log.v(TAG, "onCreate PodcastPlaybackService");
- mediaTitle = getString(R.string.no_podcast_selected);
-
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
- mMediaPlayer = new MediaPlayer();
+
podcastNotification = new PodcastNotification(this);
mHandler = new Handler();
eventBus = EventBus.getDefault();
-
eventBus.register(this);
-
- mMediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
- @Override
- public boolean onError(MediaPlayer mediaPlayer, int i, int i2) {
- isPreparing = false;
- Toast.makeText(PodcastPlaybackService.this, "Failed to open podcast", Toast.LENGTH_LONG).show();
- return false;
- }
- });
-
- mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
- @Override
- public void onPrepared(MediaPlayer mediaPlayer) {
- play();
- isPreparing = false;
- canCallGetDuration = true;
- }
- });
-
- mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
- @Override
- public void onCompletion(MediaPlayer mediaPlayer) {
- pause();//Send the over signal
- podcastCompleted();
- }
- });
-
-
eventBus.post(new PodcastPlaybackServiceStarted());
mHandler.postDelayed(mUpdateTimeTask, 0);
@@ -166,118 +127,51 @@ public class PodcastPlaybackService extends Service implements TextToSpeech.OnIn
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
- if (intent.hasExtra(PODCAST_ITEM)) {
- openFile((PodcastItem) intent.getSerializableExtra(PODCAST_ITEM));
- } else if(intent.hasExtra(TTS_ITEM)) {
- openTtsFeed((TTSItem) intent.getSerializableExtra(TTS_ITEM));
+ if(mPlaybackService != null) {
+ mPlaybackService.destroy();
+ mPlaybackService = null;
}
- }
-
- return Service.START_STICKY;
- }
-
-
- public static final int delay = 500; //In milliseconds
-
- private boolean canCallGetDuration = false;//Otherwise the player would call getDuration all the time without loading a media file
- private boolean isPreparing = false;
- private boolean isVideoFile = false;
-
- public void openTtsFeed(TTSItem textToSpeechItem) {
- this.mCurrentlyPlayingTTS = textToSpeechItem;
- this.mCurrentlyPlayingPodcast = null;
- this.mPlaybackType = PlaybackType.TTS;
- this.isVideoFile = false;
+ MediaItem mediaItem = (MediaItem) intent.getSerializableExtra(MEDIA_ITEM);
- try {
- if(mMediaPlayer.isPlaying())
- pause();
-
- this.mediaTitle = textToSpeechItem.title;
-
- isPreparing = true;
- mHandler.postDelayed(mUpdateTimeTask, 0);
-
- if(ttsController == null) {
- ttsController = new TextToSpeech(this, this);
- ttsController.setOnUtteranceProgressListener(new UtteranceProgressListener() {
- @Override
- public void onDone(String utteranceId) {
- podcastCompleted();
- }
-
- @Override public void onStart(String utteranceId) {}
- @Override public void onError(String utteranceId) {}
- });
+ if (mediaItem instanceof PodcastItem) {
+ if(((PodcastItem)mediaItem).isYoutubeVideo()) {
+ mPlaybackService = new YoutubePlaybackService(this, podcastStatusListener, mediaItem);
+ } else {
+ mPlaybackService = new MediaPlayerPlaybackService(this, podcastStatusListener, mediaItem);
+ }
+ } else if(mediaItem instanceof TTSItem) {
+ mPlaybackService = new TTSPlaybackService(this, podcastStatusListener, mediaItem);
}
- else
- onInit(TextToSpeech.SUCCESS);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- private void podcastCompleted() {
- Log.d(TAG, "Podcast completed, cleaning up");
- mHandler.removeCallbacks(mUpdateTimeTask);
- podcastNotification.cancel();
- mCurrentlyPlayingPodcast = null;
- mCurrentlyPlayingTTS = null;
+ sendMediaStatus();
+ podcastNotification.podcastChanged();
+ }
- EventBus.getDefault().post(new PodcastCompletedEvent());
+ return Service.START_STICKY;
}
- @Override
- public void onInit(int status) {
- if (status == TextToSpeech.SUCCESS) {
- /*
- int result = ttsController.setLanguage(Locale.US);
-
- if (result == TextToSpeech.LANG_MISSING_DATA
- || result == TextToSpeech.LANG_NOT_SUPPORTED) {
- Log.e("TTS", "This Language is not supported");
- } else {
- ttsController.speak(text, TextToSpeech.QUEUE_FLUSH, null);
- }*/
-
- HashMap<String,String> ttsParams = new HashMap<>();
- ttsParams.put(TextToSpeech.Engine.KEY_PARAM_UTTERANCE_ID,"dummyId");
- ttsController.speak(mCurrentlyPlayingTTS.text, TextToSpeech.QUEUE_FLUSH, ttsParams);
-
- isPreparing = false;
-
- } else {
- Log.e("TTS", "Initilization Failed!");
- ttsController = null;
+ private PlaybackService.PodcastStatusListener podcastStatusListener = new PlaybackService.PodcastStatusListener() {
+ @Override
+ public void podcastStatusUpdated() {
+ sendMediaStatus();
}
- }
-
- public void openFile(PodcastItem podcastItem) {
- this.mPlaybackType = PlaybackType.PODCAST;
- this.mCurrentlyPlayingPodcast = podcastItem;
- this.mCurrentlyPlayingTTS = null;
-
- this.isVideoFile = podcastItem.isVideoPodcast;
- try {
- if(mMediaPlayer.isPlaying())
- pause();
- this.mediaTitle = podcastItem.title;
+ @Override
+ public void podcastCompleted() {
+ Log.d(TAG, "Podcast completed, cleaning up");
+ mHandler.removeCallbacks(mUpdateTimeTask);
+ podcastNotification.cancel();
- isPreparing = true;
- mHandler.postDelayed(mUpdateTimeTask, 0);
+ mPlaybackService.destroy();
+ mPlaybackService = null;
- mMediaPlayer.reset();
- mMediaPlayer.setDataSource(podcastItem.link);
- mMediaPlayer.prepareAsync();
- } catch (IOException e) {
- e.printStackTrace();
- isPreparing = false;
+ EventBus.getDefault().post(new PodcastCompletedEvent());
}
+ };
+
+ public static final int delay = 500; //In milliseconds
- podcastNotification.podcastChanged();
- }
/**
* Background Runnable thread
@@ -302,45 +196,30 @@ public class PodcastPlaybackService extends Service implements TextToSpeech.OnIn
}
private boolean isPlaying() {
- return (mPlaybackType == PlaybackType.PODCAST && mMediaPlayer.isPlaying()) || //If podcast is running
- mPlaybackType == PlaybackType.TTS && ttsController.isSpeaking(); // or if tts is running
+ return (mPlaybackService != null && mPlaybackService.getStatus() == PlaybackService.Status.PLAYING);
}
@Subscribe
public void onEvent(WindPodcast event) {
- if(mMediaPlayer != null) {
- double totalDuration = mMediaPlayer.getDuration();
- int position = (int)((totalDuration / 100d) * event.toPositionInPercent);
- mMediaPlayer.seekTo(position);
+ if(mPlaybackService != null) {
+ mPlaybackService.seekTo(event.toPositionInPercent);
}
}
- /*
- public void onEventBackgroundThread(OpenPodcastEvent event) {
- this.isVideoFile = event.isVideoFile;
- openFile(event.pathToFile, event.mediaTitle);
- }
- */
-
@Subscribe
public void onEvent(RegisterVideoOutput videoOutput) {
- if(mMediaPlayer != null) {
- if(videoOutput.surfaceView == null) {
- mMediaPlayer.setDisplay(null);
- Log.d(TAG, "Disable Screen output!");
+ if(mPlaybackService != null && mPlaybackService instanceof MediaPlayerPlaybackService) {
+ ((MediaPlayerPlaybackService) mPlaybackService).setVideoView(videoOutput.surfaceView, videoOutput.parentResizableView);
+ }
+ }
- mMediaPlayer.setScreenOnWhilePlaying(false);
+ @Subscribe
+ public void onEvent(RegisterYoutubeOutput videoOutput) {
+ if(mPlaybackService != null && mPlaybackService instanceof YoutubePlaybackService) {
+ if(videoOutput.youTubePlayer == null) {
+ mPlaybackService.destroy();
} else {
- if(videoOutput.surfaceView.getHolder() != mSurfaceHolder) {
- parentResizableView = videoOutput.parentResizableView;
-
- videoOutput.surfaceView.getHolder().addCallback(mSHCallback);
- //videoOutput.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); //holder.setType(SurfaceHolder.SURFACE_TYPE_GPU);
-
- populateVideo();
-
- Log.d(TAG, "Enable Screen output!");
- }
+ ((YoutubePlaybackService) mPlaybackService).setYoutubePlayer(videoOutput.youTubePlayer, videoOutput.wasRestored);
}
}
}
@@ -351,59 +230,18 @@ public class PodcastPlaybackService extends Service implements TextToSpeech.OnIn
}
-
-
public void play() {
- if(mPlaybackType == PlaybackType.PODCAST) {
- try {
- int progress = mMediaPlayer.getCurrentPosition() / mMediaPlayer.getDuration();
- if (progress >= 1) {
- mMediaPlayer.seekTo(0);
- }
- } catch (Exception ex) {
- ex.printStackTrace();
- }
-
- mMediaPlayer.start();
- } else {
- onInit(TextToSpeech.SUCCESS);//restart last tts
+ if(mPlaybackService != null) {
+ mPlaybackService.play();
}
mHandler.removeCallbacks(mUpdateTimeTask);
mHandler.postDelayed(mUpdateTimeTask, 0);
-
- populateVideo();
- }
-
- private void populateVideo() {
- double videoHeightRel = (double) mSurfaceWidth / (double) mMediaPlayer.getVideoWidth();
- int videoHeight = (int) (mMediaPlayer.getVideoHeight() * videoHeightRel);
-
- if (mSurfaceWidth != 0 && videoHeight != 0 && mSurfaceHolder != null) {
- //mSurfaceHolder.setFixedSize(mSurfaceWidth, videoHeight);
-
- parentResizableView.getLayoutParams().height = videoHeight;
- parentResizableView.setLayoutParams(parentResizableView.getLayoutParams());
- }
}
public void pause() {
- if(mPlaybackType == PlaybackType.PODCAST) {
- if (mMediaPlayer.isPlaying())
- mMediaPlayer.pause();
- }
-
- if(mPlaybackType == PlaybackType.TTS) {
- if (ttsController.isSpeaking()) {
- ttsController.stop();
-
- //The tts service needs a few ms's to end playing. So wait 100ms
- try {
- Thread.sleep(100);
- } catch (Exception ex) {
- ex.printStackTrace();
- }
- }
+ if(mPlaybackService != null) {
+ mPlaybackService.pause();
}
mHandler.removeCallbacks(mUpdateTimeTask);
@@ -411,25 +249,20 @@ public class PodcastPlaybackService extends Service implements TextToSpeech.OnIn
}
public void sendMediaStatus() {
- long totalDuration = 0;
- long currentDuration = 0;
-
- if(mPlaybackType == PlaybackType.PODCAST) {
- if (!isPreparing && canCallGetDuration) {
- totalDuration = mMediaPlayer.getDuration();
- currentDuration = mMediaPlayer.getCurrentPosition();
- }
-
- long currentRssItemId = -1;
- if (mCurrentlyPlayingPodcast != null)
- currentRssItemId = mCurrentlyPlayingPodcast.itemId;
+ UpdatePodcastStatusEvent audioPodcastEvent;
- UpdatePodcastStatusEvent audioPodcastEvent = new UpdatePodcastStatusEvent(currentDuration, totalDuration, mMediaPlayer.isPlaying(), mediaTitle, isPreparing, canCallGetDuration, isVideoFile, currentRssItemId);
- eventBus.post(audioPodcastEvent);
- } else if(mPlaybackType == PlaybackType.TTS) {
- UpdatePodcastStatusEvent audioPodcastEvent = new UpdatePodcastStatusEvent(0, 0, ttsController.isSpeaking(), mediaTitle, isPreparing, true, false, mCurrentlyPlayingTTS.itemId);
- eventBus.post(audioPodcastEvent);
+ if(mPlaybackService == null) {
+ audioPodcastEvent = new UpdatePodcastStatusEvent(0, 0, PlaybackService.Status.NOT_INITIALIZED, "", PlaybackService.VideoType.None, -1);
+ } else {
+ audioPodcastEvent = new UpdatePodcastStatusEvent(
+ mPlaybackService.getCurrentDuration(),
+ mPlaybackService.getTotalDuration(),
+ mPlaybackService.getStatus(),
+ mPlaybackService.getMediaItem().title,
+ mPlaybackService.getVideoType(),
+ mPlaybackService.getMediaItem().itemId);
}
+ eventBus.post(audioPodcastEvent);
}
@@ -451,34 +284,4 @@ public class PodcastPlaybackService extends Service implements TextToSpeech.OnIn
super.onCallStateChanged(state, incomingNumber);
}
};
-
-
-
-
- int mSurfaceWidth;
- int mSurfaceHeight;
- SurfaceHolder mSurfaceHolder;
- SurfaceHolder.Callback mSHCallback = new SurfaceHolder.Callback()
- {
- public void surfaceChanged(SurfaceHolder holder, int format, int surfaceWidth, int surfaceHeight)
- {
- mSurfaceWidth = surfaceWidth;
- mSurfaceHeight = surfaceHeight;
- }
-
- public void surfaceCreated(SurfaceHolder holder)
- {
- mSurfaceHolder = holder;
- mMediaPlayer.setDisplay(mSurfaceHolder);
-
- mMediaPlayer.setScreenOnWhilePlaying(true);
-
- Log.d(TAG, "surfaceCreated");
- }
-
- public void surfaceDestroyed(SurfaceHolder holder)
- {
- Log.d(TAG, "surfaceDestroyed");
- }
- };
}