This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class PlusService | |
extends SimplePlusIntentService | |
{ | |
/** default constructor */ | |
public PlusService() { | |
super(PlusService.class.getSimpleName()); | |
} | |
//XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (isPlaying && !mStarted) { | |
Intent intent = new Intent(mContext, MusicService.class); | |
ContextCompat.startForegroundService(mContext, intent); | |
mContext.startForeground(NOTIFICATION_ID, notification); | |
mStarted = true; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mPauseButton.setOnClickListener( | |
new View.OnClickListener() { | |
@Override | |
public void onClick(View view) { | |
mPlayerAdapter.pause(); | |
} | |
}); | |
mPlayButton.setOnClickListener( | |
new View.OnClickListener() { | |
@Override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
protected void onStart() { | |
super.onStart(); | |
mPlayerAdapter.loadMedia(MEDIA_RES_ID); | |
Log.d(TAG, "onStart: create MediaPlayer"); | |
} | |
@Override | |
protected void onStop() { | |
super.onStop(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void initializeProgressCallback() { | |
final int duration = mMediaPlayer.getDuration(); | |
if (mPlaybackProgressCallback != null) { | |
mPlaybackProgressCallback.setPlaybackDuration(duration); | |
mPlaybackProgressCallback.setPlaybackPosition(0); | |
} | |
} |
OlderNewer