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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
|
package org.fox.ttrss;
import android.content.Intent;
import android.content.res.Configuration;
import android.graphics.Rect;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.MediaController;
import android.widget.PopupMenu;
import java.io.IOException;
public class VideoPlayerActivity extends CommonActivity {
private final String TAG = this.getClass().getSimpleName();
private String m_streamUri;
private MediaPlayer mediaPlayer;
private SurfaceView surfaceView;
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(R.style.DarkTheme);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_video_player);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().hide();
surfaceView = (SurfaceView) findViewById(R.id.video_player);
registerForContextMenu(surfaceView);
setTitle(getIntent().getStringExtra("title"));
if (savedInstanceState == null) {
m_streamUri = getIntent().getStringExtra("streamUri");
} else {
m_streamUri = savedInstanceState.getString("streamUri");
}
findViewById(R.id.video_player_overflow).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(VideoPlayerActivity.this, v);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.activity_video_player, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
return onVideoMenuItemSelected(item);
}
});
popup.show();
}
});
final MediaController mediaController = new MediaController(this);
surfaceView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!mediaController.isShowing())
mediaController.show(5000);
else
mediaController.hide();
}
});
mediaPlayer = new MediaPlayer();
mediaController.setMediaPlayer(new MediaController.MediaPlayerControl() {
@Override
public void start() {
mediaPlayer.start();
}
@Override
public void pause() {
mediaPlayer.pause();
}
@Override
public int getDuration() {
return mediaPlayer.getDuration();
}
@Override
public int getCurrentPosition() {
return mediaPlayer.getCurrentPosition();
}
@Override
public void seekTo(int pos) {
mediaPlayer.seekTo(pos);
}
@Override
public boolean isPlaying() {
return mediaPlayer.isPlaying();
}
@Override
public int getBufferPercentage() {
return 0;
}
@Override
public boolean canPause() {
return true;
}
@Override
public boolean canSeekBackward() {
return true;
}
@Override
public boolean canSeekForward() {
return true;
}
@Override
public int getAudioSessionId() {
return 0;
}
});
SurfaceHolder sh = surfaceView.getHolder();
try {
mediaPlayer.setDataSource(this, Uri.parse(m_streamUri));
} catch (IOException e) {
e.printStackTrace();
}
Log.d(TAG, surfaceView.getWidth() + " " + surfaceView.getHeight());
final FrameLayout.LayoutParams svLayoutParams = new FrameLayout.LayoutParams(surfaceView.getWidth(), surfaceView.getHeight());
sh.addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder holder) {
mediaPlayer.setDisplay(holder);
try {
mediaPlayer.prepareAsync();
} catch (IllegalStateException e) {
e.printStackTrace();
}
mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
View loadingBar = findViewById(R.id.video_loading);
if (loadingBar != null) loadingBar.setVisibility(View.GONE);
resizeSurface();
mp.setLooping(true);
mp.start();
}
}
);
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
//
}
});
mediaController.setAnchorView(surfaceView);
}
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
resizeSurface();
}
@Override
public void onSaveInstanceState(Bundle out) {
super.onSaveInstanceState(out);
out.putString("streamUri", m_streamUri);
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_video_player, menu);
return true;
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
getMenuInflater().inflate(R.menu.activity_video_player, menu);
super.onCreateContextMenu(menu, v, menuInfo);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return onContextItemSelected(item); // this is really bad :()
} */
public boolean onVideoMenuItemSelected(android.view.MenuItem item) {
switch (item.getItemId()) {
case R.id.article_vid_open:
if (m_streamUri != null) {
try {
openUri(Uri.parse(m_streamUri));
} catch (Exception e) {
e.printStackTrace();
toast(R.string.error_other_error);
}
}
return true;
case R.id.article_vid_copy:
if (m_streamUri != null) {
copyToClipboard(m_streamUri);
}
return true;
case R.id.article_vid_share:
if (m_streamUri != null) {
shareText(m_streamUri);
}
return true;
default:
Log.d(TAG, "onVideoMenuItemSelected, unhandled id=" + item.getItemId());
return false;
}
}
protected void resizeSurface() {
// get the dimensions of the video (only valid when surfaceView is set)
float videoWidth = mediaPlayer.getVideoWidth();
float videoHeight = mediaPlayer.getVideoHeight();
Rect rectangle = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(rectangle);
int actionBarHeight = isPortrait() ? getSupportActionBar().getHeight() : 0;
Display display = getWindowManager().getDefaultDisplay();
float containerWidth = display.getWidth();
float containerHeight = display.getHeight() - rectangle.top - actionBarHeight;
// set dimensions to surfaceView's layout params (maintaining aspect ratio)
android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();
lp.width = (int) containerWidth;
lp.height = (int) ((videoHeight / videoWidth) * containerWidth);
if(lp.height > containerHeight) {
lp.width = (int) ((videoWidth / videoHeight) * containerHeight);
lp.height = (int) containerHeight;
}
surfaceView.setLayoutParams(lp);
}
@Override
public void onPause() {
super.onPause();
if (isFinishing()) {
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
}
}
|