Skip to content

Instantly share code, notes, and snippets.

View nazmulidris's full-sized avatar
🤙
❯ Write Rust 🦀 for r3bl-open-core ❯ rec 📹 live code vids developerlife YT chan

Nazmul Idris nazmulidris

🤙
❯ Write Rust 🦀 for r3bl-open-core ❯ rec 📹 live code vids developerlife YT chan
View GitHub Profile
private void startUpdatingCallbackWithPosition() {
if (mExecutor == null) {
mExecutor = Executors.newSingleThreadScheduledExecutor();
}
if (mSeekBarPositionUpdateTask == null) {
mSeekBarPositionUpdateTask = new Runnable() {
@Override
public void run() {
updateProgressCallbackTask();
}
@Override
public void initializeProgressCallback() {
final int duration = mMediaPlayer.getDuration();
if (mPlaybackProgressCallback != null) {
mPlaybackProgressCallback.setPlaybackDuration(duration);
mPlaybackProgressCallback.setPlaybackPosition(0);
}
}
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
protected void onStart() {
super.onStart();
mPlayerAdapter.loadMedia(MEDIA_RES_ID);
Log.d(TAG, "onStart: create MediaPlayer");
}
@Override
protected void onStop() {
super.onStop();
<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>
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);
mPauseButton.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View view) {
mPlayerAdapter.pause();
}
});
mPlayButton.setOnClickListener(
new View.OnClickListener() {
@Override
if (isPlaying && !mStarted) {
Intent intent = new Intent(mContext, MusicService.class);
ContextCompat.startForegroundService(mContext, intent);
mContext.startForeground(NOTIFICATION_ID, notification);
mStarted = true;
}
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();
}
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;