Skip to content

Commit fc3eb4e

Browse files
author
Paul Ruiz
committed
added media style notifications
1 parent 651c552 commit fc3eb4e

File tree

27 files changed

+663
-8
lines changed

27 files changed

+663
-8
lines changed

Chromecast/app/src/main/java/com/ptrprograms/chromecast/MainActivity.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,10 @@ private void initCastClientListener() {
7676
mCastClientListener = new Cast.Listener() {
7777
@Override
7878
public void onApplicationStatusChanged() {
79-
if( mApiClient != null ) {
80-
//Log.e(TAG, "onApplicationStatusChanged: " + Cast.CastApi.getApplicationStatus(mApiClient));
81-
}
8279
}
8380

8481
@Override
8582
public void onVolumeChanged() {
86-
if( mApiClient != null ) {
87-
//Log.e( TAG, "onVolumeChanged: " + Cast.CastApi.getVolume( mApiClient ) );
88-
}
8983
}
9084

9185
@Override
@@ -102,14 +96,12 @@ private void initRemoteMediaPlayer() {
10296
public void onStatusUpdated() {
10397
MediaStatus mediaStatus = mRemoteMediaPlayer.getMediaStatus();
10498
mIsPlaying = mediaStatus.getPlayerState() == MediaStatus.PLAYER_STATE_PLAYING;
105-
//Handle any app logic related to UI for the app state
10699
}
107100
});
108101

109102
mRemoteMediaPlayer.setOnMetadataUpdatedListener( new RemoteMediaPlayer.OnMetadataUpdatedListener() {
110103
@Override
111104
public void onMetadataUpdated() {
112-
//Can get media info and metadata from the remote media player
113105
}
114106
});
115107
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.gradle
2+
/local.properties
3+
/.idea/workspace.xml
4+
.DS_Store
5+
/build
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
apply plugin: 'com.android.application'
2+
3+
android {
4+
compileSdkVersion 'android-L'
5+
buildToolsVersion "20.0.0"
6+
7+
defaultConfig {
8+
applicationId "com.ptrprograms.mediasessionwithmediastylenotification"
9+
minSdkVersion 'L'
10+
targetSdkVersion 'L'
11+
versionCode 1
12+
versionName "1.0"
13+
}
14+
buildTypes {
15+
release {
16+
runProguard false
17+
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18+
}
19+
}
20+
}
21+
22+
dependencies {
23+
compile fileTree(dir: 'libs', include: ['*.jar'])
24+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Add project specific ProGuard rules here.
2+
# By default, the flags in this file are appended to flags specified
3+
# in /Applications/Android Studio.app/sdk/tools/proguard/proguard-android.txt
4+
# You can edit the include path and order by changing the proguardFiles
5+
# directive in build.gradle.
6+
#
7+
# For more details, see
8+
# http://developer.android.com/guide/developing/tools/proguard.html
9+
10+
# Add any project specific keep options here:
11+
12+
# If your project uses WebView with JS, uncomment the following
13+
# and specify the fully qualified class name to the JavaScript interface
14+
# class:
15+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16+
# public *;
17+
#}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.ptrprograms.mediasessionwithmediastylenotification;
2+
3+
import android.app.Application;
4+
import android.test.ApplicationTestCase;
5+
6+
/**
7+
* <a href="http://d.android.com/tools/testing/testing_android.html">Testing Fundamentals</a>
8+
*/
9+
public class ApplicationTest extends ApplicationTestCase<Application> {
10+
public ApplicationTest() {
11+
super(Application.class);
12+
}
13+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.ptrprograms.mediasessionwithmediastylenotification" >
4+
5+
<permission android:name="android.permission.MEDIA_CONTENT_CONTROL" />
6+
7+
<application
8+
android:allowBackup="true"
9+
android:icon="@drawable/ic_launcher"
10+
android:label="@string/app_name"
11+
android:theme="@style/AppTheme" >
12+
<activity
13+
android:name=".MainActivity"
14+
android:label="@string/app_name" >
15+
<intent-filter>
16+
<action android:name="android.intent.action.MAIN" />
17+
18+
<category android:name="android.intent.category.LAUNCHER" />
19+
</intent-filter>
20+
</activity>
21+
22+
<service android:name=".MediaPlayerService" />
23+
</application>
24+
25+
</manifest>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.ptrprograms.mediasessionwithmediastylenotification;
2+
3+
import android.app.Activity;
4+
import android.app.Notification;
5+
import android.app.NotificationManager;
6+
import android.app.PendingIntent;
7+
import android.content.Context;
8+
import android.content.Intent;
9+
import android.graphics.BitmapFactory;
10+
import android.media.MediaSyncEvent;
11+
import android.media.Rating;
12+
import android.media.session.MediaController;
13+
import android.media.session.MediaSession;
14+
import android.media.session.MediaSessionManager;
15+
import android.media.session.MediaSessionToken;
16+
import android.os.Bundle;
17+
import android.util.Log;
18+
import android.view.KeyEvent;
19+
import android.view.Menu;
20+
import android.view.MenuItem;
21+
22+
23+
public class MainActivity extends Activity {
24+
25+
@Override
26+
protected void onCreate(Bundle savedInstanceState) {
27+
super.onCreate(savedInstanceState);
28+
setContentView(R.layout.activity_main);
29+
30+
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
31+
intent.setAction( MediaPlayerService.ACTION_PLAY );
32+
startService( intent );
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
package com.ptrprograms.mediasessionwithmediastylenotification;
2+
3+
import android.app.Notification;
4+
import android.app.NotificationManager;
5+
import android.app.PendingIntent;
6+
import android.app.Service;
7+
import android.content.Context;
8+
import android.content.Intent;
9+
import android.graphics.BitmapFactory;
10+
import android.media.AudioTrack;
11+
import android.media.MediaMetadata;
12+
import android.media.MediaPlayer;
13+
import android.media.Rating;
14+
import android.media.session.MediaController;
15+
import android.media.session.MediaSession;
16+
import android.media.session.MediaSessionManager;
17+
import android.media.session.MediaSessionToken;
18+
import android.media.session.PlaybackState;
19+
import android.os.IBinder;
20+
import android.util.Log;
21+
import android.widget.ProgressBar;
22+
23+
/**
24+
* Created by paulruiz on 10/28/14.
25+
*/
26+
public class MediaPlayerService extends Service {
27+
28+
public static final String ACTION_PLAY = "action_play";
29+
public static final String ACTION_PAUSE = "action_pause";
30+
public static final String ACTION_REWIND = "action_rewind";
31+
public static final String ACTION_FAST_FORWARD = "action_fast_foward";
32+
public static final String ACTION_NEXT = "action_next";
33+
public static final String ACTION_PREVIOUS = "action_previous";
34+
public static final String ACTION_STOP = "action_stop";
35+
36+
private MediaPlayer mMediaPlayer;
37+
private MediaSessionManager mManager;
38+
private MediaSession mSession;
39+
private MediaController mController;
40+
41+
@Override
42+
public IBinder onBind(Intent intent) {
43+
return null;
44+
}
45+
46+
private void handleIntent( Intent intent ) {
47+
if( intent == null || intent.getAction() == null )
48+
return;
49+
50+
String action = intent.getAction();
51+
52+
if( action.equalsIgnoreCase( ACTION_PLAY ) ) {
53+
mController.getTransportControls().play();
54+
} else if( action.equalsIgnoreCase( ACTION_PAUSE ) ) {
55+
mController.getTransportControls().pause();
56+
} else if( action.equalsIgnoreCase( ACTION_FAST_FORWARD ) ) {
57+
mController.getTransportControls().fastForward();
58+
} else if( action.equalsIgnoreCase( ACTION_REWIND ) ) {
59+
mController.getTransportControls().rewind();
60+
} else if( action.equalsIgnoreCase( ACTION_PREVIOUS ) ) {
61+
mController.getTransportControls().skipToPrevious();
62+
} else if( action.equalsIgnoreCase( ACTION_NEXT ) ) {
63+
mController.getTransportControls().skipToNext();
64+
} else if( action.equalsIgnoreCase( ACTION_STOP ) ) {
65+
mController.getTransportControls().stop();
66+
}
67+
}
68+
69+
private Notification.Action generateAction( int icon, String title, String intentAction ) {
70+
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
71+
intent.setAction( intentAction );
72+
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
73+
return new Notification.Action.Builder( icon, title, pendingIntent ).build();
74+
75+
}
76+
77+
private void buildNotification( Notification.Action action ) {
78+
Notification.MediaStyle style = new Notification.MediaStyle();
79+
style.setMediaSession( mSession.getSessionToken() );
80+
81+
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
82+
intent.setAction( ACTION_STOP );
83+
PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 1, intent, 0);
84+
Notification.Builder builder = new Notification.Builder( this )
85+
.setSmallIcon(R.drawable.ic_launcher)
86+
.setContentTitle( "Media Title" )
87+
.setContentText( "Media Artist" )
88+
.setDeleteIntent( pendingIntent )
89+
.setStyle(style);
90+
91+
builder.addAction( generateAction( android.R.drawable.ic_media_previous, "Previous", ACTION_PREVIOUS ) );
92+
builder.addAction( generateAction( android.R.drawable.ic_media_rew, "Rewind", ACTION_REWIND ) );
93+
builder.addAction( action );
94+
builder.addAction( generateAction( android.R.drawable.ic_media_ff, "Fast Foward", ACTION_FAST_FORWARD ) );
95+
builder.addAction( generateAction( android.R.drawable.ic_media_next, "Next", ACTION_NEXT ) );
96+
97+
NotificationManager notificationManager = (NotificationManager) getSystemService( Context.NOTIFICATION_SERVICE );
98+
notificationManager.notify( 1, builder.build() );
99+
}
100+
101+
@Override
102+
public int onStartCommand(Intent intent, int flags, int startId) {
103+
if( mManager == null ) {
104+
initMediaSessions();
105+
}
106+
107+
handleIntent( intent );
108+
return super.onStartCommand(intent, flags, startId);
109+
}
110+
111+
private void initMediaSessions() {
112+
mMediaPlayer = new MediaPlayer();
113+
mManager = (MediaSessionManager) getSystemService(Context.MEDIA_SESSION_SERVICE);
114+
mSession = mManager.createSession("sample session");
115+
mController = MediaController.fromToken( mSession.getSessionToken() );
116+
117+
mSession.addTransportControlsCallback( new MediaSession.TransportControlsCallback() {
118+
@Override
119+
public void onPlay() {
120+
super.onPlay();
121+
Log.e( "MediaPlayerService", "onPlay");
122+
buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
123+
}
124+
125+
@Override
126+
public void onPause() {
127+
super.onPause();
128+
Log.e( "MediaPlayerService", "onPause");
129+
buildNotification(generateAction(android.R.drawable.ic_media_play, "Play", ACTION_PLAY));
130+
}
131+
132+
@Override
133+
public void onSkipToNext() {
134+
super.onSkipToNext();
135+
Log.e( "MediaPlayerService", "onSkipToNext");
136+
//Change media here
137+
buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
138+
}
139+
140+
@Override
141+
public void onSkipToPrevious() {
142+
super.onSkipToPrevious();
143+
Log.e( "MediaPlayerService", "onSkipToPrevious");
144+
//Change media here
145+
buildNotification( generateAction( android.R.drawable.ic_media_pause, "Pause", ACTION_PAUSE ) );
146+
}
147+
148+
@Override
149+
public void onFastForward() {
150+
super.onFastForward();
151+
Log.e( "MediaPlayerService", "onFastForward");
152+
//Manipulate current media here
153+
}
154+
155+
@Override
156+
public void onRewind() {
157+
super.onRewind();
158+
Log.e( "MediaPlayerService", "onRewind");
159+
//Manipulate current media here
160+
}
161+
162+
@Override
163+
public void onStop() {
164+
super.onStop();
165+
Log.e( "MediaPlayerService", "onStop");
166+
//Stop media player here
167+
NotificationManager notificationManager = (NotificationManager) getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE);
168+
notificationManager.cancel( 1 );
169+
Intent intent = new Intent( getApplicationContext(), MediaPlayerService.class );
170+
stopService( intent );
171+
}
172+
173+
@Override
174+
public void onSeekTo(long pos) {
175+
super.onSeekTo(pos);
176+
}
177+
178+
@Override
179+
public void onSetRating(Rating rating) {
180+
super.onSetRating(rating);
181+
}
182+
});
183+
}
184+
185+
@Override
186+
public boolean onUnbind(Intent intent) {
187+
mSession.release();
188+
return super.onUnbind(intent);
189+
}
190+
}
Loading
Loading
Loading
Loading
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:paddingLeft="@dimen/activity_horizontal_margin"
6+
android:paddingRight="@dimen/activity_horizontal_margin"
7+
android:paddingTop="@dimen/activity_vertical_margin"
8+
android:paddingBottom="@dimen/activity_vertical_margin"
9+
tools:context=".MainActivity">
10+
11+
<TextView
12+
android:text="@string/hello_world"
13+
android:layout_width="wrap_content"
14+
android:layout_height="wrap_content" />
15+
16+
</RelativeLayout>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<menu xmlns:android="http://schemas.android.com/apk/res/android"
2+
xmlns:tools="http://schemas.android.com/tools"
3+
tools:context=".MainActivity" >
4+
<item android:id="@+id/action_settings"
5+
android:title="@string/action_settings"
6+
android:orderInCategory="100"
7+
android:showAsAction="never" />
8+
</menu>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
<style name="AppTheme" parent="android:Theme.Material.Light">
4+
</style>
5+
</resources>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<resources>
2+
<!-- Example customization of dimensions originally defined in res/values/dimens.xml
3+
(such as screen margins) for screens with more than 820dp of available width. This
4+
would include 7" and 10" devices in landscape (~960dp and ~1280dp respectively). -->
5+
<dimen name="activity_horizontal_margin">64dp</dimen>
6+
</resources>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<resources>
2+
<!-- Default screen margins, per the Android Design guidelines. -->
3+
<dimen name="activity_horizontal_margin">16dp</dimen>
4+
<dimen name="activity_vertical_margin">16dp</dimen>
5+
</resources>

0 commit comments

Comments
 (0)