Skip to content

Commit 2242d5f

Browse files
committed
add video player
1 parent 8a0b7e7 commit 2242d5f

File tree

4 files changed

+94
-1
lines changed

4 files changed

+94
-1
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
android:label="@string/app_name"
1313
android:supportsRtl="true"
1414
android:theme="@style/AppTheme">
15-
<activity android:name=".MD360DemoActivity">
15+
<activity android:name=".MediaPlayerActivity">
1616
<intent-filter>
1717
<action android:name="android.intent.action.MAIN" />
1818

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package com.asha.md360player4android;
2+
3+
import android.content.res.AssetFileDescriptor;
4+
import android.media.MediaPlayer;
5+
import android.os.Bundle;
6+
import android.support.annotation.Nullable;
7+
import android.support.v7.app.AppCompatActivity;
8+
import android.view.SurfaceHolder;
9+
import android.view.SurfaceView;
10+
import android.view.View;
11+
12+
import java.io.IOException;
13+
14+
15+
/**
16+
* Created by hzqiujiadi on 16/1/24.
17+
* hzqiujiadi ashqalcn@gmail.com
18+
*/
19+
public class MediaPlayerActivity extends AppCompatActivity implements SurfaceHolder.Callback, MediaPlayer.OnPreparedListener {
20+
private MediaPlayer mPlayer;
21+
@Override
22+
protected void onCreate(@Nullable Bundle savedInstanceState) {
23+
super.onCreate(savedInstanceState);
24+
setContentView(R.layout.activity_mediaplay);
25+
mPlayer = new MediaPlayer();
26+
mPlayer.setOnPreparedListener(this);
27+
28+
SurfaceView surface = (SurfaceView) findViewById(R.id.surface);
29+
SurfaceHolder surfaceHolder = surface.getHolder();
30+
surfaceHolder.addCallback(this);
31+
}
32+
33+
public void onPlayButtonClicked(View view) {
34+
try {
35+
AssetFileDescriptor afd = getResources().openRawResourceFd(R.raw.demo);
36+
if ( afd == null ) return;
37+
mPlayer.setDataSource(afd.getFileDescriptor(),afd.getStartOffset(), afd.getLength());
38+
afd.close();
39+
mPlayer.prepareAsync();
40+
} catch (IOException e) {
41+
e.printStackTrace();
42+
}
43+
}
44+
45+
@Override
46+
public void surfaceCreated(SurfaceHolder holder) {
47+
mPlayer.setDisplay(holder);
48+
}
49+
50+
@Override
51+
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
52+
53+
}
54+
55+
@Override
56+
public void surfaceDestroyed(SurfaceHolder holder) {
57+
if (mPlayer != null) mPlayer.setDisplay(null);
58+
}
59+
60+
@Override
61+
protected void onDestroy() {
62+
super.onDestroy();
63+
if ( mPlayer != null ){
64+
if (mPlayer.isPlaying()) mPlayer.stop();
65+
mPlayer.release();
66+
mPlayer = null;
67+
}
68+
}
69+
70+
@Override
71+
public void onPrepared(MediaPlayer mp) {
72+
mp.start();
73+
}
74+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"
3+
xmlns:android="http://schemas.android.com/apk/res/android">
4+
<SurfaceView
5+
android:id="@+id/surface"
6+
android:layout_width="match_parent"
7+
android:layout_height="match_parent"
8+
android:layout_gravity="center">
9+
</SurfaceView>
10+
<LinearLayout
11+
android:orientation="vertical" android:layout_width="match_parent"
12+
android:layout_height="match_parent">
13+
<Button
14+
android:onClick="onPlayButtonClicked"
15+
android:text="Play"
16+
android:layout_width="match_parent"
17+
android:layout_height="wrap_content" />
18+
</LinearLayout>
19+
</FrameLayout>

app/src/main/res/raw/demo.mp4

18.7 MB
Binary file not shown.

0 commit comments

Comments
 (0)