1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
|
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.onemedia;
import android.content.Context;
import android.content.Intent;
import android.media.session.MediaSession;
import android.media.session.MediaSessionManager;
import android.media.session.MediaSessionToken;
import android.media.session.PlaybackState;
import android.media.session.TransportPerformer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import com.android.onemedia.playback.LocalRenderer;
import com.android.onemedia.playback.Renderer;
import com.android.onemedia.playback.RendererFactory;
public class PlayerSession {
private static final String TAG = "PlayerSession";
protected MediaSession mSession;
protected Context mContext;
protected RendererFactory mRendererFactory;
protected LocalRenderer mRenderer;
protected MediaSession.Callback mCallback;
protected Renderer.Listener mRenderListener;
protected TransportPerformer mPerformer;
protected PlaybackState mPlaybackState;
protected Listener mListener;
public PlayerSession(Context context) {
mContext = context;
mRendererFactory = new RendererFactory();
mRenderer = new LocalRenderer(context, null);
mCallback = new ControllerCb();
mRenderListener = new RenderListener();
mPlaybackState = new PlaybackState();
mPlaybackState.setActions(PlaybackState.ACTION_PAUSE
| PlaybackState.ACTION_PLAY);
mRenderer.registerListener(mRenderListener);
}
public void createSession() {
if (mSession != null) {
mSession.release();
}
MediaSessionManager man = (MediaSessionManager) mContext
.getSystemService(Context.MEDIA_SESSION_SERVICE);
Log.d(TAG, "Creating session for package " + mContext.getBasePackageName());
mSession = man.createSession("OneMedia");
mSession.addCallback(mCallback);
mPerformer = mSession.setTransportPerformerEnabled();
mPerformer.addListener(new TransportListener());
mPerformer.setPlaybackState(mPlaybackState);
mSession.publish();
}
public void onDestroy() {
if (mSession != null) {
mSession.release();
}
if (mRenderer != null) {
mRenderer.unregisterListener(mRenderListener);
mRenderer.onDestroy();
}
}
public void setListener(Listener listener) {
mListener = listener;
}
public MediaSessionToken getSessionToken() {
return mSession.getSessionToken();
}
public void setContent(Bundle request) {
mRenderer.setContent(request);
}
public void setNextContent(Bundle request) {
mRenderer.setNextContent(request);
}
public interface Listener {
public void onPlayStateChanged(PlaybackState state);
}
private class RenderListener implements Renderer.Listener {
@Override
public void onError(int type, int extra, Bundle extras, Throwable error) {
Log.d(TAG, "Sending onError with type " + type + " and extra " + extra);
mPlaybackState.setState(PlaybackState.PLAYSTATE_ERROR);
if (error != null) {
mPlaybackState.setErrorMessage(error.getLocalizedMessage());
}
mPerformer.setPlaybackState(mPlaybackState);
if (mListener != null) {
mListener.onPlayStateChanged(mPlaybackState);
}
}
@Override
public void onStateChanged(int newState) {
if (newState != Renderer.STATE_ERROR) {
mPlaybackState.setErrorMessage(null);
}
switch (newState) {
case Renderer.STATE_ENDED:
case Renderer.STATE_STOPPED:
mPlaybackState.setState(PlaybackState.PLAYSTATE_STOPPED);
break;
case Renderer.STATE_INIT:
case Renderer.STATE_PREPARING:
mPlaybackState.setState(PlaybackState.PLAYSTATE_BUFFERING);
break;
case Renderer.STATE_ERROR:
mPlaybackState.setState(PlaybackState.PLAYSTATE_ERROR);
break;
case Renderer.STATE_PAUSED:
mPlaybackState.setState(PlaybackState.PLAYSTATE_PAUSED);
break;
case Renderer.STATE_PLAYING:
mPlaybackState.setState(PlaybackState.PLAYSTATE_PLAYING);
break;
default:
mPlaybackState.setState(PlaybackState.PLAYSTATE_ERROR);
mPlaybackState.setErrorMessage("unkown state");
break;
}
mPlaybackState.setPosition(mRenderer.getSeekPosition());
mPerformer.setPlaybackState(mPlaybackState);
if (mListener != null) {
mListener.onPlayStateChanged(mPlaybackState);
}
}
@Override
public void onBufferingUpdate(int percent) {
}
@Override
public void onFocusLost() {
Log.d(TAG, "Focus lost, changing state to " + Renderer.STATE_PAUSED);
mPlaybackState.setState(PlaybackState.PLAYSTATE_PAUSED);
mPlaybackState.setPosition(mRenderer.getSeekPosition());
mPerformer.setPlaybackState(mPlaybackState);
if (mListener != null) {
mListener.onPlayStateChanged(mPlaybackState);
}
}
@Override
public void onNextStarted() {
}
}
private class ControllerCb extends MediaSession.Callback {
@Override
public void onMediaButton(Intent mediaRequestIntent) {
if (Intent.ACTION_MEDIA_BUTTON.equals(mediaRequestIntent.getAction())) {
KeyEvent event = (KeyEvent) mediaRequestIntent
.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
switch (event.getKeyCode()) {
case KeyEvent.KEYCODE_MEDIA_PLAY:
Log.d(TAG, "play button received");
mRenderer.onPlay();
break;
case KeyEvent.KEYCODE_MEDIA_PAUSE:
Log.d(TAG, "pause button received");
mRenderer.onPause();
break;
}
}
}
}
private class TransportListener extends TransportPerformer.Listener {
@Override
public void onPlay() {
mRenderer.onPlay();
}
@Override
public void onPause() {
mRenderer.onPause();
}
}
}
|