Skip to content

Instantly share code, notes, and snippets.

View nazmulidris's full-sized avatar
🌞

Nazmul Idris nazmulidris

🌞
View GitHub Profile
@nazmulidris
nazmulidris / PlusService.java
Created August 29, 2013 22:04
My goal was to try allow a developer to seamlessly integrate PlusClient into their Activity and Service classes, without really knowing any callbacks or state machines, and just implementing a few methods (only the ones that they need to) from an abstract base class that they extend. However, the methods that they implement closely mirror PlusCl…
public class PlusService
extends SimplePlusIntentService
{
/** default constructor */
public PlusService() {
super(PlusService.class.getSimpleName());
}
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
private static final String CHANNEL_ID = "media_playback_channel";
@RequiresApi(Build.VERSION_CODES.O)
private void createChannel() {
NotificationManager
mNotificationManager =
(NotificationManager) mContext
.getSystemService(Context.NOTIFICATION_SERVICE);
// The id of the channel.
String id = CHANNEL_ID;
import android.support.v4.app.NotificationCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.media.app.NotificationCompat.MediaStyle;
//...
// You only need to create the channel on API 26+ devices
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createChannel();
}
if (isPlaying && !mStarted) {
Intent intent = new Intent(mContext, MusicService.class);
ContextCompat.startForegroundService(mContext, intent);
mContext.startForeground(NOTIFICATION_ID, notification);
mStarted = true;
}
mPauseButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
mPlayerAdapter.pause();
}
});
mPlayButton.setOnClickListener(
new View.OnClickListener() {
@Override
private void initializeMediaPlayer() {
if (mMediaPlayer == null) {
mMediaPlayer = new MediaPlayer();
mMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
stopUpdatingCallbackWithPosition(true);
logToUI("MediaPlayer playback completed");
if (mPlaybackInfoListener != null) {
mPlaybackInfoListener.onStateChanged(PlaybackInfoListener.State.COMPLETED);
<application
android:launchMode="singleTop"
android:theme="@style/AppTheme">
<activity android:name="com.example.android.mediaplayersample.MainActivity"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
@Override
protected void onStart() {
super.onStart();
mPlayerAdapter.loadMedia(MEDIA_RES_ID);
Log.d(TAG, "onStart: create MediaPlayer");
}
@Override
protected void onStop() {
super.onStop();
public interface PlaybackInfoListener {
@IntDef({State.INVALID, State.PLAYING, State.PAUSED, State.RESET, State.COMPLETED})
@Retention(RetentionPolicy.SOURCE)
@interface State {
int INVALID = -1;
int PLAYING = 0;
int PAUSED = 1;
int RESET = 2;
@Override
public void initializeProgressCallback() {
final int duration = mMediaPlayer.getDuration();
if (mPlaybackProgressCallback != null) {
mPlaybackProgressCallback.setPlaybackDuration(duration);
mPlaybackProgressCallback.setPlaybackPosition(0);
}
}