├── sample
├── src
│ └── main
│ │ ├── res
│ │ ├── raw
│ │ │ └── video.mp4
│ │ ├── values
│ │ │ └── strings.xml
│ │ ├── drawable-hdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-ldpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-mdpi
│ │ │ └── ic_launcher.png
│ │ ├── drawable-xhdpi
│ │ │ └── ic_launcher.png
│ │ └── layout
│ │ │ └── activity_main.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ └── com
│ │ └── sprylab
│ │ └── android
│ │ └── texturevideoview
│ │ └── sample
│ │ └── MainActivity.java
└── pom.xml
├── library
├── pom.xml
└── src
│ └── main
│ └── java
│ └── com
│ └── sprylab
│ └── android
│ └── widget
│ └── TextureVideoView.java
├── .gitignore
├── CHANGELOG.md
├── README.md
├── pom.xml
└── LICENSE.txt
/sample/src/main/res/raw/video.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprylab/texturevideoview/HEAD/sample/src/main/res/raw/video.mp4
--------------------------------------------------------------------------------
/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | TextureVideoView Sample
3 |
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprylab/texturevideoview/HEAD/sample/src/main/res/drawable-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-ldpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprylab/texturevideoview/HEAD/sample/src/main/res/drawable-ldpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprylab/texturevideoview/HEAD/sample/src/main/res/drawable-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/drawable-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/sprylab/texturevideoview/HEAD/sample/src/main/res/drawable-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 |
14 |
15 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/library/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | com.sprylab.android.texturevideoview
7 | parent
8 | 1.2.2-SNAPSHOT
9 |
10 |
11 | texturevideoview
12 |
13 | TextureVideoView for Android - Library
14 |
15 |
16 |
17 | com.google.android
18 | android
19 | provided
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
6 |
7 |
10 |
11 |
12 |
13 |
18 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/sample/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | com.sprylab.android.texturevideoview
7 | parent
8 | 1.2.2-SNAPSHOT
9 |
10 |
11 | sample
12 | apk
13 |
14 | TextureVideoView for Android - Sample
15 |
16 |
17 |
18 | com.google.android
19 | android
20 | provided
21 |
22 |
23 |
24 | com.sprylab.android.texturevideoview
25 | texturevideoview
26 | ${project.version}
27 |
28 |
29 |
30 |
31 |
32 |
33 | com.simpligility.maven.plugins
34 | android-maven-plugin
35 |
36 |
37 |
38 |
39 |
40 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | ## Android
2 |
3 | # built application files
4 | *.apk
5 | *.ap_
6 |
7 | # files for the dex VM
8 | *.dex
9 |
10 | # Java class files
11 | *.class
12 |
13 | # generated files
14 | bin/
15 | gen/
16 | gen-external-apklibs/
17 |
18 | # Ignore gradle files
19 | .gradle/
20 | build/
21 |
22 | # Local configuration file (sdk path, etc)
23 | local.properties
24 |
25 | # Proguard folder generated by Eclipse
26 | proguard/
27 |
28 | ## Maven
29 |
30 | target/
31 | *.releaseBackup
32 |
33 | ## Eclipse
34 |
35 | *.pydevproject
36 | .metadata
37 | .gradle
38 | bin/**
39 | tmp/**
40 | tmp/**/*
41 | *.tmp
42 | *.bak
43 | *.swp
44 | *~.nib
45 | local.properties
46 | .settings/
47 | .loadpath
48 |
49 | # External tool builders
50 | .externalToolBuilders/
51 |
52 | # Locally stored "Eclipse launch configurations"
53 | *.launch
54 |
55 | # CDT-specific
56 | .cproject
57 |
58 | # PDT-specific
59 | .buildpath
60 |
61 | # TeXlipse plugin
62 | .texlipse
63 |
64 | ## JetBrains
65 |
66 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode
67 |
68 | ## Directory-based project format
69 | .idea/
70 | # if you remove the above rule, at least ignore user-specific stuff:
71 | # .idea/workspace.xml
72 | # .idea/tasks.xml
73 | # and these sensitive or high-churn files:
74 | # .idea/dataSources.ids
75 | # .idea/dataSources.xml
76 | # .idea/sqlDataSources.xml
77 | # .idea/dynamic.xml
78 |
79 | ## File-based project format
80 | *.ipr
81 | *.iws
82 | *.iml
83 |
84 | ## Additional for IntelliJ
85 | out/
86 |
87 | # generated by mpeltonen/sbt-idea plugin
88 | .idea_modules/
89 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | Change Log
2 | ==========
3 |
4 | Version 1.2.1
5 | -------------
6 |
7 | * clear the underlying `Surface` on API level 16+ when calling `stopPlayback()` (see #15, thanks to @hithere1985)
8 |
9 | Version 1.2.0
10 | -------------
11 |
12 | * synced TextureVideoView with latest sources from Android 7.1.1_r13
13 | (constructor improvements, code formatting)
14 | * API addition: Call `setShouldRequestAudioFocus(true)` to programmatically disable audio focus request before opening a video file -
15 | default behaviour is unchanged and complies with the current `VideoView` implementation
16 | (see #14, thanks to @ezaquarii)
17 |
18 | Version 1.1.1
19 | -------------
20 |
21 | * fixed potential activity leak caused by AudioMananger on Android pre-6.0 devices (thanks to @hzsweers)
22 |
23 | Version 1.1.0
24 | -------------
25 |
26 | * synced TextureVideoView with latest sources from Android 6.0.1_r10
27 | (un-hides [setVideoURI(Uri uri, Map headers)][1],
28 | uses proper audio focus, source code indention)
29 | * reverted to Android framework behaviour: OnCompletionListener may be called more than once again
30 | (see #6, thanks to @MrNovado)
31 |
32 | **This may break your code!** The OnCompletionListener itself is now responsible for guarding
33 | any unwanted invocation.
34 |
35 | [1]: http://developer.android.com/reference/android/widget/VideoView.html#setVideoURI%28android.net.Uri,%20java.util.Map%3Cjava.lang.String,%20java.lang.String%3E%29
36 |
37 | Version 1.0.2
38 | -------------
39 |
40 | * fixed a sizing bug when auto-starting the video with view size != video size (thanks to @lhunath)
41 | * fixed potential activity leak when streaming videos over HTTP(S) (thanks to @koral)
42 |
43 | Version 1.0.1
44 | -------------
45 |
46 | * fixed potential leak by releasing surface when destroyed
47 |
48 | Version 1.0.0
49 | -------------
50 |
51 | * initial public release on github
52 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | TextureVideoView for Android
2 | ============================
3 |
4 | A `VideoView` based on the official Android 7.1.1_r13 sources using a `TextureView` instead of a `SurfaceView` by [sprylab technologies GmbH][1]. It will work on Android 4.0+ (API level 15 and above).
5 |
6 | The main advantages of this drop-in replacement compared to the original `VideoView` are the following:
7 |
8 | * extends `android.view.TextureView` instead of a `android.view.SurfaceView` thus allowing proper view animations as described in the [Property Animation API guide][2]
9 | * API addition: Call `setShouldRequestAudioFocus(true)` to programmatically disable audio focus request before opening a video file -
10 | default behaviour is unchanged and complies with the current `VideoView` implementation (thanks to @ezaquarii)
11 | * clears the underlying `Surface` on API level 16+ when calling `stopPlayback()` (thanks to @hithere1985)
12 | * fixes a sizing bug when auto-starting the video with view size != video size (thanks to @lhunath)
13 | * fixes various memory leaks
14 | * potential leak by releasing surface when destroyed
15 | * potential activity leak when streaming videos over HTTP(S) (thanks to @koral)
16 | * potential activity leak caused by AudioManager on Android pre-6.0 devices (thanks to @hzsweers)
17 |
18 | Unfortunately, code that uses hidden APIs and thus is not publicly available had to be removed (e.g. subtitle support).
19 |
20 | Download
21 | --------
22 |
23 | Download [the latest JAR][3] or grab via Maven:
24 | ```xml
25 |
26 | com.sprylab.android.texturevideoview
27 | texturevideoview
28 | 1.2.1
29 |
30 | ```
31 | or Gradle:
32 | ```groovy
33 | compile 'com.sprylab.android.texturevideoview:texturevideoview:1.2.1'
34 | ```
35 |
36 | License
37 | =======
38 |
39 | Copyright 2014-2016 sprylab technologies GmbH
40 |
41 | Licensed under the Apache License, Version 2.0 (the "License");
42 | you may not use this file except in compliance with the License.
43 | You may obtain a copy of the License at
44 |
45 | http://www.apache.org/licenses/LICENSE-2.0
46 |
47 | Unless required by applicable law or agreed to in writing, software
48 | distributed under the License is distributed on an "AS IS" BASIS,
49 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
50 | See the License for the specific language governing permissions and
51 | limitations under the License.
52 |
53 | Note: The sample application includes a trailer for [Big Buck Bunny][4], which is released under the [Creative Commons Attribution 3.0][5] license.
54 |
55 | [1]: https://sprylab.com
56 | [2]: https://developer.android.com/guide/topics/graphics/prop-animation.html
57 | [3]: http://repository.sonatype.org/service/local/artifact/maven/redirect?r=central-proxy&g=com.sprylab.android.texturevideoview&a=texturevideoview&v=LATEST
58 | [4]: http://www.bigbuckbunny.org
59 | [5]: http://creativecommons.org/licenses/by/3.0/
60 |
--------------------------------------------------------------------------------
/sample/src/main/java/com/sprylab/android/texturevideoview/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2014 sprylab technologies GmbH
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.sprylab.android.texturevideoview.sample;
18 |
19 | import java.io.File;
20 | import java.io.FileOutputStream;
21 | import java.io.IOException;
22 |
23 | import com.sprylab.android.sample.texturevideoview.R;
24 | import com.sprylab.android.widget.TextureVideoView;
25 |
26 | import android.app.Activity;
27 | import android.graphics.Bitmap;
28 | import android.media.MediaPlayer;
29 | import android.os.Bundle;
30 | import android.util.Log;
31 | import android.view.View;
32 | import android.widget.Button;
33 | import android.widget.MediaController;
34 | import android.widget.Toast;
35 |
36 | public class MainActivity extends Activity {
37 |
38 | private static final String TAG = MainActivity.class.getName();
39 |
40 | private TextureVideoView mVideoView;
41 |
42 | private Button mCaptureFrameButton;
43 |
44 | @Override
45 | public void onCreate(Bundle savedInstanceState) {
46 | super.onCreate(savedInstanceState);
47 |
48 | setContentView(R.layout.activity_main);
49 |
50 | mVideoView = (TextureVideoView) findViewById(R.id.video_view);
51 | mCaptureFrameButton = (Button) findViewById(R.id.btn_capture_frame);
52 | mCaptureFrameButton.setOnClickListener(new View.OnClickListener() {
53 | @Override
54 | public void onClick(final View v) {
55 | saveCurrentFrame();
56 | }
57 | }
58 | );
59 |
60 | initVideoView();
61 | }
62 |
63 | @Override
64 | protected void onDestroy() {
65 | super.onDestroy();
66 |
67 | if (mVideoView != null) {
68 | mVideoView.stopPlayback();
69 | mVideoView = null;
70 | }
71 | }
72 |
73 | private void initVideoView() {
74 | mVideoView.setVideoPath(getVideoPath());
75 | mVideoView.setMediaController(new MediaController(this));
76 | mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
77 | @Override
78 | public void onPrepared(final MediaPlayer mp) {
79 | startVideoPlayback();
80 | startVideoAnimation();
81 | }
82 | });
83 | }
84 |
85 | private void startVideoPlayback() {
86 | // "forces" anti-aliasing - but increases time for taking frames - so keep it disabled
87 | // mVideoView.setScaleX(1.00001f);
88 | mVideoView.start();
89 | }
90 |
91 | private void startVideoAnimation() {
92 | mVideoView.animate().rotationBy(360.0f).setDuration(mVideoView.getDuration()).start();
93 | }
94 |
95 | private String getVideoPath() {
96 | return "android.resource://" + getPackageName() + "/" + R.raw.video;
97 | }
98 |
99 | private void saveCurrentFrame() {
100 | final Bitmap currentFrameBitmap = mVideoView.getBitmap();
101 |
102 | final File currentFrameFile = new File(getExternalFilesDir("frames"), "frame" + System.currentTimeMillis() + ".jpg");
103 | writeBitmapToFile(currentFrameBitmap, currentFrameFile);
104 |
105 | currentFrameBitmap.recycle();
106 |
107 | Toast.makeText(this, "Frame saved as " + currentFrameFile.getAbsolutePath() + ".", Toast.LENGTH_SHORT).show();
108 | }
109 |
110 | private void writeBitmapToFile(final Bitmap bitmap, final File file) {
111 | try {
112 | FileOutputStream out = new FileOutputStream(file);
113 | bitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
114 | out.close();
115 | } catch (final IOException e) {
116 | Log.e(TAG, "Error writing bitmap to file.", e);
117 | }
118 | }
119 |
120 | }
121 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | 4.0.0
4 |
5 |
6 | org.sonatype.oss
7 | oss-parent
8 | 7
9 |
10 |
11 | com.sprylab.android.texturevideoview
12 | parent
13 | 1.2.2-SNAPSHOT
14 | pom
15 |
16 | TextureVideoView for Android
17 | A VideoView based on the official Android 6.0.1_r10 sources using a TextureView instead of a
18 | SurfaceView.
19 |
20 | https://github.com/sprylab/texturevideoview
21 | 2014
22 |
23 |
24 |
25 | The Apache Software License, Version 2.0
26 | http://www.apache.org/licenses/LICENSE-2.0.txt
27 |
28 |
29 |
30 |
31 |
32 | Roman Zimmer
33 | roman.zimmer@sprylab.com
34 |
35 | Project-Lead
36 | Developer
37 |
38 |
39 |
40 |
41 |
42 | sprylab technologies GmbH
43 | http://sprylab.com
44 |
45 |
46 |
47 | https://github.com/sprylab/texturevideoview
48 | scm:git:git@github.com/sprylab/texturevideoview.git
49 | scm:git:git@github.com:sprylab/texturevideoview.git
50 | HEAD
51 |
52 |
53 |
54 | GitHub Issues
55 | https://github.com/sprylab/texturevideoview/issues
56 |
57 |
58 |
59 | library
60 | sample
61 |
62 |
63 |
64 |
65 | UTF-8
66 | UTF-8
67 | 1.6
68 |
69 |
70 | 4.1.1.4
71 | 16
72 |
73 |
74 | 1.4.1
75 | 3.1
76 | 4.3.0
77 | 2.2.1
78 | 2.9.1
79 | 2.5
80 | 1.5
81 |
82 |
83 |
84 |
85 |
86 | com.google.android
87 | android
88 | ${android.version}
89 | provided
90 |
91 |
92 |
93 | com.sprylab.android
94 | texturevideoview
95 | ${project.version}
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
104 | org.apache.maven.plugins
105 | maven-enforcer-plugin
106 | ${maven-enforcer-plugin.version}
107 |
108 |
109 | enforce-maven
110 |
111 | enforce
112 |
113 |
114 |
115 |
116 | 3.0.5
117 |
118 |
119 |
120 |
121 |
122 |
123 |
124 | org.apache.maven.plugins
125 | maven-compiler-plugin
126 | ${maven-compiler-plugin.version}
127 |
128 | ${java.version}
129 | ${java.version}
130 |
131 |
132 |
133 | com.simpligility.maven.plugins
134 | android-maven-plugin
135 | ${android-maven-plugin.version}
136 | true
137 |
138 |
139 | ${env.ANDROID_HOME}
140 | ${android.sdk.platform}
141 |
142 | true
143 | true
144 |
145 | true
146 |
147 |
148 |
149 |
150 | org.apache.maven.plugins
151 | maven-source-plugin
152 | ${maven-source-plugin.version}
153 |
154 |
155 | attach-sources
156 |
157 | jar
158 |
159 |
160 |
161 |
162 |
163 | org.apache.maven.plugins
164 | maven-javadoc-plugin
165 | ${maven-javadoc-plugin.version}
166 |
167 |
168 | attach-javadocs
169 |
170 | jar
171 |
172 |
173 |
174 |
175 |
176 | org.apache.maven.plugins
177 | maven-release-plugin
178 | ${maven-release-plugin.version}
179 |
180 | @{project.version}
181 | true
182 |
183 |
184 |
185 |
186 |
187 |
188 |
189 | org.apache.maven.plugins
190 | maven-source-plugin
191 |
192 |
193 | org.apache.maven.plugins
194 | maven-javadoc-plugin
195 |
196 |
197 | org.apache.maven.plugins
198 | maven-enforcer-plugin
199 |
200 |
201 |
202 |
203 |
204 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/library/src/main/java/com/sprylab/android/widget/TextureVideoView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2006 The Android Open Source Project
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.sprylab.android.widget;
18 |
19 | import android.app.AlertDialog;
20 | import android.content.Context;
21 | import android.content.DialogInterface;
22 | import android.content.res.Resources;
23 | import android.graphics.SurfaceTexture;
24 | import android.media.AudioManager;
25 | import android.media.MediaPlayer;
26 | import android.media.MediaPlayer.OnCompletionListener;
27 | import android.media.MediaPlayer.OnErrorListener;
28 | import android.media.MediaPlayer.OnInfoListener;
29 | import android.net.Uri;
30 | import android.opengl.GLES20;
31 | import android.os.Build;
32 | import android.util.AttributeSet;
33 | import android.util.Log;
34 | import android.view.KeyEvent;
35 | import android.view.MotionEvent;
36 | import android.view.Surface;
37 | import android.view.TextureView;
38 | import android.view.View;
39 | import android.view.accessibility.AccessibilityEvent;
40 | import android.view.accessibility.AccessibilityNodeInfo;
41 | import android.widget.MediaController;
42 | import android.widget.MediaController.MediaPlayerControl;
43 |
44 | import java.io.IOException;
45 | import java.util.Map;
46 |
47 | import javax.microedition.khronos.egl.EGL10;
48 | import javax.microedition.khronos.egl.EGLConfig;
49 | import javax.microedition.khronos.egl.EGLContext;
50 | import javax.microedition.khronos.egl.EGLDisplay;
51 | import javax.microedition.khronos.egl.EGLSurface;
52 |
53 | /**
54 | * Displays a video file. The TextureVideoView class
55 | * can load images from various sources (such as resources or content
56 | * providers), takes care of computing its measurement from the video so that
57 | * it can be used in any layout manager, and provides various display options
58 | * such as scaling and tinting.
59 | *
60 | * Note: VideoView does not retain its full state when going into the
61 | * background. In particular, it does not restore the current play state,
62 | * play position or selected tracks. Applications should
63 | * save and restore these on their own in
64 | * {@link android.app.Activity#onSaveInstanceState} and
65 | * {@link android.app.Activity#onRestoreInstanceState}.
66 | * Also note that the audio session id (from {@link #getAudioSessionId}) may
67 | * change from its previously returned value when the VideoView is restored.
68 | *
69 | * This code is based on the official Android sources for 7.1.1_r13 with the following differences:
70 | *
71 | *
extends {@link android.view.TextureView} instead of a {@link android.view.SurfaceView}
72 | * allowing proper view animations
73 | *
removes code that uses hidden APIs and thus is not available (e.g. subtitle support)
74 | *
various small fixes and improvements
75 | *
76 | */
77 | public class TextureVideoView extends TextureView
78 | implements MediaPlayerControl {
79 | private static final String TAG = "TextureVideoView";
80 |
81 | // all possible internal states
82 | private static final int STATE_ERROR = -1;
83 | private static final int STATE_IDLE = 0;
84 | private static final int STATE_PREPARING = 1;
85 | private static final int STATE_PREPARED = 2;
86 | private static final int STATE_PLAYING = 3;
87 | private static final int STATE_PAUSED = 4;
88 | private static final int STATE_PLAYBACK_COMPLETED = 5;
89 |
90 | // settable by the client
91 | private Uri mUri;
92 | private Map mHeaders;
93 |
94 | // mCurrentState is a TextureVideoView object's current state.
95 | // mTargetState is the state that a method caller intends to reach.
96 | // For instance, regardless the TextureVideoView object's current state,
97 | // calling pause() intends to bring the object to a target state
98 | // of STATE_PAUSED.
99 | private int mCurrentState = STATE_IDLE;
100 | private int mTargetState = STATE_IDLE;
101 |
102 | // All the stuff we need for playing and showing a video
103 | private Surface mSurface = null;
104 | private MediaPlayer mMediaPlayer = null;
105 | private int mAudioSession;
106 | private int mVideoWidth;
107 | private int mVideoHeight;
108 | private MediaController mMediaController;
109 | private OnCompletionListener mOnCompletionListener;
110 | private MediaPlayer.OnPreparedListener mOnPreparedListener;
111 | private int mCurrentBufferPercentage;
112 | private OnErrorListener mOnErrorListener;
113 | private OnInfoListener mOnInfoListener;
114 | private int mSeekWhenPrepared; // recording the seek position while preparing
115 | private boolean mCanPause;
116 | private boolean mCanSeekBack;
117 | private boolean mCanSeekForward;
118 | private boolean mShouldRequestAudioFocus = true;
119 |
120 | public TextureVideoView(Context context) {
121 | this(context, null);
122 | }
123 |
124 | public TextureVideoView(Context context, AttributeSet attrs) {
125 | this(context, attrs, 0);
126 | }
127 |
128 | public TextureVideoView(Context context, AttributeSet attrs, int defStyle) {
129 | super(context, attrs, defStyle);
130 |
131 | mVideoWidth = 0;
132 | mVideoHeight = 0;
133 |
134 | setSurfaceTextureListener(mSurfaceTextureListener);
135 |
136 | setFocusable(true);
137 | setFocusableInTouchMode(true);
138 | requestFocus();
139 |
140 | mCurrentState = STATE_IDLE;
141 | mTargetState = STATE_IDLE;
142 | }
143 |
144 | @Override
145 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
146 | //Log.i("@@@@", "onMeasure(" + MeasureSpec.toString(widthMeasureSpec) + ", "
147 | // + MeasureSpec.toString(heightMeasureSpec) + ")");
148 |
149 | int width = getDefaultSize(mVideoWidth, widthMeasureSpec);
150 | int height = getDefaultSize(mVideoHeight, heightMeasureSpec);
151 | if (mVideoWidth > 0 && mVideoHeight > 0) {
152 |
153 | int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
154 | int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
155 | int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
156 | int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec);
157 |
158 | if (widthSpecMode == MeasureSpec.EXACTLY && heightSpecMode == MeasureSpec.EXACTLY) {
159 | // the size is fixed
160 | width = widthSpecSize;
161 | height = heightSpecSize;
162 |
163 | // for compatibility, we adjust size based on aspect ratio
164 | if ( mVideoWidth * height < width * mVideoHeight ) {
165 | //Log.i("@@@", "image too wide, correcting");
166 | width = height * mVideoWidth / mVideoHeight;
167 | } else if ( mVideoWidth * height > width * mVideoHeight ) {
168 | //Log.i("@@@", "image too tall, correcting");
169 | height = width * mVideoHeight / mVideoWidth;
170 | }
171 | } else if (widthSpecMode == MeasureSpec.EXACTLY) {
172 | // only the width is fixed, adjust the height to match aspect ratio if possible
173 | width = widthSpecSize;
174 | height = width * mVideoHeight / mVideoWidth;
175 | if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
176 | // couldn't match aspect ratio within the constraints
177 | height = heightSpecSize;
178 | }
179 | } else if (heightSpecMode == MeasureSpec.EXACTLY) {
180 | // only the height is fixed, adjust the width to match aspect ratio if possible
181 | height = heightSpecSize;
182 | width = height * mVideoWidth / mVideoHeight;
183 | if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
184 | // couldn't match aspect ratio within the constraints
185 | width = widthSpecSize;
186 | }
187 | } else {
188 | // neither the width nor the height are fixed, try to use actual video size
189 | width = mVideoWidth;
190 | height = mVideoHeight;
191 | if (heightSpecMode == MeasureSpec.AT_MOST && height > heightSpecSize) {
192 | // too tall, decrease both width and height
193 | height = heightSpecSize;
194 | width = height * mVideoWidth / mVideoHeight;
195 | }
196 | if (widthSpecMode == MeasureSpec.AT_MOST && width > widthSpecSize) {
197 | // too wide, decrease both width and height
198 | width = widthSpecSize;
199 | height = width * mVideoHeight / mVideoWidth;
200 | }
201 | }
202 | } else {
203 | // no size yet, just adopt the given spec sizes
204 | }
205 | setMeasuredDimension(width, height);
206 | }
207 |
208 | @Override
209 | public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
210 | super.onInitializeAccessibilityEvent(event);
211 | event.setClassName(TextureVideoView.class.getName());
212 | }
213 |
214 | @Override
215 | public void onInitializeAccessibilityNodeInfo(AccessibilityNodeInfo info) {
216 | super.onInitializeAccessibilityNodeInfo(info);
217 | info.setClassName(TextureVideoView.class.getName());
218 | }
219 |
220 | public int resolveAdjustedSize(int desiredSize, int measureSpec) {
221 | return getDefaultSize(desiredSize, measureSpec);
222 | }
223 |
224 | /**
225 | * Sets video path.
226 | *
227 | * @param path the path of the video.
228 | */
229 | public void setVideoPath(String path) {
230 | setVideoURI(Uri.parse(path));
231 | }
232 |
233 | /**
234 | * Sets video URI.
235 | *
236 | * @param uri the URI of the video.
237 | */
238 | public void setVideoURI(Uri uri) {
239 | setVideoURI(uri, null);
240 | }
241 |
242 | /**
243 | * Sets video URI using specific headers.
244 | *
245 | * @param uri the URI of the video.
246 | * @param headers the headers for the URI request.
247 | * Note that the cross domain redirection is allowed by default, but that can be
248 | * changed with key/value pairs through the headers parameter with
249 | * "android-allow-cross-domain-redirect" as the key and "0" or "1" as the value
250 | * to disallow or allow cross domain redirection.
251 | */
252 | public void setVideoURI(Uri uri, Map headers) {
253 | mUri = uri;
254 | mHeaders = headers;
255 | mSeekWhenPrepared = 0;
256 | openVideo();
257 | requestLayout();
258 | invalidate();
259 | }
260 |
261 | public void stopPlayback() {
262 | if (mMediaPlayer != null) {
263 | mMediaPlayer.stop();
264 | mMediaPlayer.release();
265 | mMediaPlayer = null;
266 | mCurrentState = STATE_IDLE;
267 | mTargetState = STATE_IDLE;
268 | if (mShouldRequestAudioFocus) {
269 | AudioManager am = (AudioManager) getContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
270 | am.abandonAudioFocus(null);
271 | }
272 | }
273 | clearSurface();
274 | }
275 |
276 | /**
277 | * Clears the surface texture by attaching a GL context and clearing it.
278 | * Code taken from Hugo Gresse's answer on stackoverflow.com.
279 | */
280 | private void clearSurface() {
281 | if (mSurface == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
282 | return;
283 | }
284 |
285 | EGL10 egl = (EGL10) EGLContext.getEGL();
286 | EGLDisplay display = egl.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
287 | egl.eglInitialize(display, null);
288 |
289 | int[] attribList = {
290 | EGL10.EGL_RED_SIZE, 8,
291 | EGL10.EGL_GREEN_SIZE, 8,
292 | EGL10.EGL_BLUE_SIZE, 8,
293 | EGL10.EGL_ALPHA_SIZE, 8,
294 | EGL10.EGL_RENDERABLE_TYPE, EGL10.EGL_WINDOW_BIT,
295 | EGL10.EGL_NONE, 0, // placeholder for recordable [@-3]
296 | EGL10.EGL_NONE
297 | };
298 | EGLConfig[] configs = new EGLConfig[1];
299 | int[] numConfigs = new int[1];
300 | egl.eglChooseConfig(display, attribList, configs, configs.length, numConfigs);
301 | EGLConfig config = configs[0];
302 | EGLContext context = egl.eglCreateContext(display, config, EGL10.EGL_NO_CONTEXT, new int[]{
303 | 12440, 2, EGL10.EGL_NONE
304 | });
305 | EGLSurface eglSurface = egl.eglCreateWindowSurface(display, config, mSurface, new int[]{
306 | EGL10.EGL_NONE
307 | });
308 |
309 | egl.eglMakeCurrent(display, eglSurface, eglSurface, context);
310 | GLES20.glClearColor(0, 0, 0, 1);
311 | GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);
312 | egl.eglSwapBuffers(display, eglSurface);
313 | egl.eglDestroySurface(display, eglSurface);
314 | egl.eglMakeCurrent(display, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_SURFACE, EGL10.EGL_NO_CONTEXT);
315 | egl.eglDestroyContext(display, context);
316 | egl.eglTerminate(display);
317 | }
318 |
319 | private void openVideo() {
320 | if (mUri == null || mSurface == null) {
321 | // not ready for playback just yet, will try again later
322 | return;
323 | }
324 | // we shouldn't clear the target state, because somebody might have
325 | // called start() previously
326 | release(false);
327 |
328 | if (mShouldRequestAudioFocus) {
329 | AudioManager am = (AudioManager) getContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
330 | am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
331 | }
332 |
333 | try {
334 | mMediaPlayer = new MediaPlayer();
335 |
336 | if (mAudioSession != 0) {
337 | mMediaPlayer.setAudioSessionId(mAudioSession);
338 | } else {
339 | mAudioSession = mMediaPlayer.getAudioSessionId();
340 | }
341 | mMediaPlayer.setOnPreparedListener(mPreparedListener);
342 | mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
343 | mMediaPlayer.setOnCompletionListener(mCompletionListener);
344 | mMediaPlayer.setOnErrorListener(mErrorListener);
345 | mMediaPlayer.setOnInfoListener(mInfoListener);
346 | mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
347 | mCurrentBufferPercentage = 0;
348 | mMediaPlayer.setDataSource(getContext().getApplicationContext(), mUri, mHeaders);
349 | mMediaPlayer.setSurface(mSurface);
350 | mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
351 | mMediaPlayer.setScreenOnWhilePlaying(true);
352 | mMediaPlayer.prepareAsync();
353 |
354 | // we don't set the target state here either, but preserve the
355 | // target state that was there before.
356 | mCurrentState = STATE_PREPARING;
357 | attachMediaController();
358 | } catch (IOException ex) {
359 | Log.w(TAG, "Unable to open content: " + mUri, ex);
360 | mCurrentState = STATE_ERROR;
361 | mTargetState = STATE_ERROR;
362 | mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
363 | return;
364 | } catch (IllegalArgumentException ex) {
365 | Log.w(TAG, "Unable to open content: " + mUri, ex);
366 | mCurrentState = STATE_ERROR;
367 | mTargetState = STATE_ERROR;
368 | mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
369 | return;
370 | }
371 | }
372 |
373 | public void setMediaController(MediaController controller) {
374 | if (mMediaController != null) {
375 | mMediaController.hide();
376 | }
377 | mMediaController = controller;
378 | attachMediaController();
379 | }
380 |
381 | private void attachMediaController() {
382 | if (mMediaPlayer != null && mMediaController != null) {
383 | mMediaController.setMediaPlayer(this);
384 | View anchorView = this.getParent() instanceof View ?
385 | (View)this.getParent() : this;
386 | mMediaController.setAnchorView(anchorView);
387 | mMediaController.setEnabled(isInPlaybackState());
388 | }
389 | }
390 |
391 | MediaPlayer.OnVideoSizeChangedListener mSizeChangedListener =
392 | new MediaPlayer.OnVideoSizeChangedListener() {
393 | public void onVideoSizeChanged(MediaPlayer mp, int width, int height) {
394 | mVideoWidth = mp.getVideoWidth();
395 | mVideoHeight = mp.getVideoHeight();
396 | if (mVideoWidth != 0 && mVideoHeight != 0) {
397 | getSurfaceTexture().setDefaultBufferSize(mVideoWidth, mVideoHeight);
398 | requestLayout();
399 | }
400 | }
401 | };
402 |
403 | MediaPlayer.OnPreparedListener mPreparedListener = new MediaPlayer.OnPreparedListener() {
404 | public void onPrepared(MediaPlayer mp) {
405 | mCurrentState = STATE_PREPARED;
406 |
407 | mCanPause = mCanSeekBack = mCanSeekForward = true;
408 |
409 | if (mOnPreparedListener != null) {
410 | mOnPreparedListener.onPrepared(mMediaPlayer);
411 | }
412 | if (mMediaController != null) {
413 | mMediaController.setEnabled(true);
414 | }
415 | mVideoWidth = mp.getVideoWidth();
416 | mVideoHeight = mp.getVideoHeight();
417 |
418 | int seekToPosition = mSeekWhenPrepared; // mSeekWhenPrepared may be changed after seekTo() call
419 | if (seekToPosition != 0) {
420 | seekTo(seekToPosition);
421 | }
422 | if (mVideoWidth != 0 && mVideoHeight != 0) {
423 | //Log.i("@@@@", "video size: " + mVideoWidth +"/"+ mVideoHeight);
424 | getSurfaceTexture().setDefaultBufferSize(mVideoWidth, mVideoHeight);
425 | // We won't get a "surface changed" callback if the surface is already the right size, so
426 | // start the video here instead of in the callback.
427 | if (mTargetState == STATE_PLAYING) {
428 | start();
429 | if (mMediaController != null) {
430 | mMediaController.show();
431 | }
432 | } else if (!isPlaying() &&
433 | (seekToPosition != 0 || getCurrentPosition() > 0)) {
434 | if (mMediaController != null) {
435 | // Show the media controls when we're paused into a video and make 'em stick.
436 | mMediaController.show(0);
437 | }
438 | }
439 | } else {
440 | // We don't know the video size yet, but should start anyway.
441 | // The video size might be reported to us later.
442 | if (mTargetState == STATE_PLAYING) {
443 | start();
444 | }
445 | }
446 | }
447 | };
448 |
449 | private MediaPlayer.OnCompletionListener mCompletionListener =
450 | new MediaPlayer.OnCompletionListener() {
451 | public void onCompletion(MediaPlayer mp) {
452 | mCurrentState = STATE_PLAYBACK_COMPLETED;
453 | mTargetState = STATE_PLAYBACK_COMPLETED;
454 | if (mMediaController != null) {
455 | mMediaController.hide();
456 | }
457 | if (mOnCompletionListener != null) {
458 | mOnCompletionListener.onCompletion(mMediaPlayer);
459 | }
460 | }
461 | };
462 |
463 | private MediaPlayer.OnInfoListener mInfoListener =
464 | new MediaPlayer.OnInfoListener() {
465 | public boolean onInfo(MediaPlayer mp, int arg1, int arg2) {
466 | if (mOnInfoListener != null) {
467 | mOnInfoListener.onInfo(mp, arg1, arg2);
468 | }
469 | return true;
470 | }
471 | };
472 |
473 | private MediaPlayer.OnErrorListener mErrorListener =
474 | new MediaPlayer.OnErrorListener() {
475 | public boolean onError(MediaPlayer mp, int framework_err, int impl_err) {
476 | Log.d(TAG, "Error: " + framework_err + "," + impl_err);
477 | mCurrentState = STATE_ERROR;
478 | mTargetState = STATE_ERROR;
479 | if (mMediaController != null) {
480 | mMediaController.hide();
481 | }
482 |
483 | /* If an error handler has been supplied, use it and finish. */
484 | if (mOnErrorListener != null) {
485 | if (mOnErrorListener.onError(mMediaPlayer, framework_err, impl_err)) {
486 | return true;
487 | }
488 | }
489 |
490 | /* Otherwise, pop up an error dialog so the user knows that
491 | * something bad has happened. Only try and pop up the dialog
492 | * if we're attached to a window. When we're going away and no
493 | * longer have a window, don't bother showing the user an error.
494 | */
495 | if (getWindowToken() != null) {
496 | Resources r = getContext().getResources();
497 | int messageId;
498 |
499 | if (framework_err == MediaPlayer.MEDIA_ERROR_NOT_VALID_FOR_PROGRESSIVE_PLAYBACK) {
500 | messageId = android.R.string.VideoView_error_text_invalid_progressive_playback;
501 | } else {
502 | messageId = android.R.string.VideoView_error_text_unknown;
503 | }
504 |
505 | new AlertDialog.Builder(getContext())
506 | .setMessage(messageId)
507 | .setPositiveButton(android.R.string.VideoView_error_button,
508 | new DialogInterface.OnClickListener() {
509 | public void onClick(DialogInterface dialog, int whichButton) {
510 | /* If we get here, there is no onError listener, so
511 | * at least inform them that the video is over.
512 | */
513 | if (mOnCompletionListener != null) {
514 | mOnCompletionListener.onCompletion(mMediaPlayer);
515 | }
516 | }
517 | })
518 | .setCancelable(false)
519 | .show();
520 | }
521 | return true;
522 | }
523 | };
524 |
525 | private MediaPlayer.OnBufferingUpdateListener mBufferingUpdateListener =
526 | new MediaPlayer.OnBufferingUpdateListener() {
527 | public void onBufferingUpdate(MediaPlayer mp, int percent) {
528 | mCurrentBufferPercentage = percent;
529 | }
530 | };
531 |
532 | /**
533 | * Register a callback to be invoked when the media file
534 | * is loaded and ready to go.
535 | *
536 | * @param l The callback that will be run
537 | */
538 | public void setOnPreparedListener(MediaPlayer.OnPreparedListener l)
539 | {
540 | mOnPreparedListener = l;
541 | }
542 |
543 | /**
544 | * Register a callback to be invoked when the end of a media file
545 | * has been reached during playback.
546 | *
547 | * @param l The callback that will be run
548 | */
549 | public void setOnCompletionListener(OnCompletionListener l)
550 | {
551 | mOnCompletionListener = l;
552 | }
553 |
554 | /**
555 | * Register a callback to be invoked when an error occurs
556 | * during playback or setup. If no listener is specified,
557 | * or if the listener returned false, TextureVideoView will inform
558 | * the user of any errors.
559 | *
560 | * @param l The callback that will be run
561 | */
562 | public void setOnErrorListener(OnErrorListener l)
563 | {
564 | mOnErrorListener = l;
565 | }
566 |
567 | /**
568 | * Register a callback to be invoked when an informational event
569 | * occurs during playback or setup.
570 | *
571 | * @param l The callback that will be run
572 | */
573 | public void setOnInfoListener(OnInfoListener l) {
574 | mOnInfoListener = l;
575 | }
576 |
577 | TextureView.SurfaceTextureListener mSurfaceTextureListener = new SurfaceTextureListener()
578 | {
579 | @Override
580 | public void onSurfaceTextureSizeChanged(final SurfaceTexture surface, final int width, final int height) {
581 | boolean isValidState = (mTargetState == STATE_PLAYING);
582 | boolean hasValidSize = (width > 0 && height > 0);
583 | if (mMediaPlayer != null && isValidState && hasValidSize) {
584 | if (mSeekWhenPrepared != 0) {
585 | seekTo(mSeekWhenPrepared);
586 | }
587 | start();
588 | }
589 | }
590 |
591 | @Override
592 | public void onSurfaceTextureAvailable(final SurfaceTexture surface, final int width, final int height) {
593 | mSurface = new Surface(surface);
594 | openVideo();
595 | }
596 |
597 | @Override
598 | public boolean onSurfaceTextureDestroyed(final SurfaceTexture surface) {
599 | // after we return from this we can't use the surface any more
600 | if (mSurface != null) {
601 | mSurface.release();
602 | mSurface = null;
603 | }
604 | if (mMediaController != null) mMediaController.hide();
605 | release(true);
606 | return true;
607 | }
608 | @Override
609 | public void onSurfaceTextureUpdated(final SurfaceTexture surface) {
610 | // do nothing
611 | }
612 | };
613 |
614 | /*
615 | * release the media player in any state
616 | */
617 | private void release(boolean cleartargetstate) {
618 | if (mMediaPlayer != null) {
619 | mMediaPlayer.reset();
620 | mMediaPlayer.release();
621 | mMediaPlayer = null;
622 | mCurrentState = STATE_IDLE;
623 | if (cleartargetstate) {
624 | mTargetState = STATE_IDLE;
625 | }
626 | if (mShouldRequestAudioFocus) {
627 | AudioManager am = (AudioManager) getContext().getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
628 | am.abandonAudioFocus(null);
629 | }
630 | }
631 | }
632 |
633 | @Override
634 | public boolean onTouchEvent(MotionEvent ev) {
635 | if (isInPlaybackState() && mMediaController != null) {
636 | toggleMediaControlsVisiblity();
637 | }
638 | return false;
639 | }
640 |
641 | @Override
642 | public boolean onTrackballEvent(MotionEvent ev) {
643 | if (isInPlaybackState() && mMediaController != null) {
644 | toggleMediaControlsVisiblity();
645 | }
646 | return false;
647 | }
648 |
649 | @Override
650 | public boolean onKeyDown(int keyCode, KeyEvent event)
651 | {
652 | boolean isKeyCodeSupported = keyCode != KeyEvent.KEYCODE_BACK &&
653 | keyCode != KeyEvent.KEYCODE_VOLUME_UP &&
654 | keyCode != KeyEvent.KEYCODE_VOLUME_DOWN &&
655 | keyCode != KeyEvent.KEYCODE_VOLUME_MUTE &&
656 | keyCode != KeyEvent.KEYCODE_MENU &&
657 | keyCode != KeyEvent.KEYCODE_CALL &&
658 | keyCode != KeyEvent.KEYCODE_ENDCALL;
659 | if (isInPlaybackState() && isKeyCodeSupported && mMediaController != null) {
660 | if (keyCode == KeyEvent.KEYCODE_HEADSETHOOK ||
661 | keyCode == KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
662 | if (mMediaPlayer.isPlaying()) {
663 | pause();
664 | mMediaController.show();
665 | } else {
666 | start();
667 | mMediaController.hide();
668 | }
669 | return true;
670 | } else if (keyCode == KeyEvent.KEYCODE_MEDIA_PLAY) {
671 | if (!mMediaPlayer.isPlaying()) {
672 | start();
673 | mMediaController.hide();
674 | }
675 | return true;
676 | } else if (keyCode == KeyEvent.KEYCODE_MEDIA_STOP
677 | || keyCode == KeyEvent.KEYCODE_MEDIA_PAUSE) {
678 | if (mMediaPlayer.isPlaying()) {
679 | pause();
680 | mMediaController.show();
681 | }
682 | return true;
683 | } else {
684 | toggleMediaControlsVisiblity();
685 | }
686 | }
687 |
688 | return super.onKeyDown(keyCode, event);
689 | }
690 |
691 | private void toggleMediaControlsVisiblity() {
692 | if (mMediaController.isShowing()) {
693 | mMediaController.hide();
694 | } else {
695 | mMediaController.show();
696 | }
697 | }
698 |
699 | @Override
700 | public void start() {
701 | if (isInPlaybackState()) {
702 | mMediaPlayer.start();
703 | mCurrentState = STATE_PLAYING;
704 | }
705 | mTargetState = STATE_PLAYING;
706 | }
707 |
708 | @Override
709 | public void pause() {
710 | if (isInPlaybackState()) {
711 | if (mMediaPlayer.isPlaying()) {
712 | mMediaPlayer.pause();
713 | mCurrentState = STATE_PAUSED;
714 | }
715 | }
716 | mTargetState = STATE_PAUSED;
717 | }
718 |
719 | public void suspend() {
720 | release(false);
721 | }
722 |
723 | public void resume() {
724 | openVideo();
725 | }
726 |
727 | @Override
728 | public int getDuration() {
729 | if (isInPlaybackState()) {
730 | return mMediaPlayer.getDuration();
731 | }
732 |
733 | return -1;
734 | }
735 |
736 | @Override
737 | public int getCurrentPosition() {
738 | if (isInPlaybackState()) {
739 | return mMediaPlayer.getCurrentPosition();
740 | }
741 | return 0;
742 | }
743 |
744 | @Override
745 | public void seekTo(int msec) {
746 | if (isInPlaybackState()) {
747 | mMediaPlayer.seekTo(msec);
748 | mSeekWhenPrepared = 0;
749 | } else {
750 | mSeekWhenPrepared = msec;
751 | }
752 | }
753 |
754 | @Override
755 | public boolean isPlaying() {
756 | return isInPlaybackState() && mMediaPlayer.isPlaying();
757 | }
758 |
759 | @Override
760 | public int getBufferPercentage() {
761 | if (mMediaPlayer != null) {
762 | return mCurrentBufferPercentage;
763 | }
764 | return 0;
765 | }
766 |
767 | private boolean isInPlaybackState() {
768 | return (mMediaPlayer != null &&
769 | mCurrentState != STATE_ERROR &&
770 | mCurrentState != STATE_IDLE &&
771 | mCurrentState != STATE_PREPARING);
772 | }
773 |
774 | @Override
775 | public boolean canPause() {
776 | return mCanPause;
777 | }
778 |
779 | @Override
780 | public boolean canSeekBackward() {
781 | return mCanSeekBack;
782 | }
783 |
784 | @Override
785 | public boolean canSeekForward() {
786 | return mCanSeekForward;
787 | }
788 |
789 | public int getAudioSessionId() {
790 | if (mAudioSession == 0) {
791 | MediaPlayer foo = new MediaPlayer();
792 | mAudioSession = foo.getAudioSessionId();
793 | foo.release();
794 | }
795 | return mAudioSession;
796 | }
797 |
798 | /**
799 | * Sets the request audio focus flag. If enabled, {@link TextureVideoView} will request
800 | * audio focus when opening a video by calling {@link AudioManager}. This flag
801 | * should be set before calling {@link TextureVideoView#setVideoPath(String)} or
802 | * {@link TextureVideoView#setVideoURI(Uri)}. By default, {@link TextureVideoView} will
803 | * request audio focus.
804 | *
805 | * @param shouldRequestAudioFocus If {@code true}, {@link TextureVideoView} will request
806 | * audio focus before opening a video, else audio focus is not requested
807 | */
808 | public void setShouldRequestAudioFocus(boolean shouldRequestAudioFocus) {
809 | mShouldRequestAudioFocus = shouldRequestAudioFocus;
810 | }
811 |
812 | /**
813 | * Returns the current state of the audio focus request flag.
814 | *
815 | * @return {@code true}, if {@link TextureVideoView} will request
816 | * audio focus before opening a video, else {@code false}
817 | */
818 | public boolean shouldRequestAudioFocus() {
819 | return mShouldRequestAudioFocus;
820 | }
821 |
822 | }
823 |
--------------------------------------------------------------------------------