質問編集履歴

4

誤字

2017/08/04 06:50

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -1,7 +1,3 @@
1
- 何か対策はありませんでしょうか?
2
-
3
-
4
-
5
1
  Activityでは、動画を流す予定なので、ボタンの設置はできません。
6
2
 
7
3
  そのため、値を取得した後に、start()を呼ぼうと思います。

3

無駄な説明を無くした、

2017/08/04 06:50

投稿

giant
giant

スコア132

test CHANGED
@@ -1 +1 @@
1
- textureView.setSurfaceTextureListener(listener);エラーが出
1
+ Activityで、onClickListener()で、start処理をしていました。ServiceのonCreate()に入れたら、nullとなりした
test CHANGED
@@ -1,98 +1,62 @@
1
- コードを変更して、listenerを引数にして、
2
-
3
- //このメソッドをAutoFitTextureViewクラスのコードに追加したのですが、
4
-
5
- public void setSurfaceTextureListener(AutoFitTextureView listener) {
6
-
7
- }
8
-
9
- ところ、「仮想メソッドです」となります。
10
-
11
- どうすればいいのでしょうか
1
+ 何か対策はありませんでしょうか?
12
2
 
13
3
 
14
4
 
15
- ##//AutoFitTextureViewクラス
5
+ Activityでは、動画を流す予定なので、ボタンの設置はできません。
6
+
7
+ そのため、値を取得した後に、start()を呼ぼうと思います。
16
8
 
17
9
  ```java
18
10
 
19
- import android.content.Context;
20
11
 
21
- import android.util.AttributeSet;
22
12
 
13
+ @Override
14
+
23
- import android.view.TextureView;
15
+ public void onCreate() {
16
+
17
+ Log.e(TAG,"1");
24
18
 
25
19
 
26
20
 
27
- /**
21
+ startBackgroundThread();
28
22
 
29
- * A {@link TextureView} that can be adjusted to a specified aspect ratio.
23
+ if (mTextureView.isAvailable()) {
30
24
 
31
- */
25
+ openCamera(mTextureView.getWidth(), mTextureView.getHeight());
32
26
 
27
+ } else {
28
+
33
- public class AutoFitTextureView extends TextureView {
29
+ mTextureView.setSurfaceTextureListener(mSurfaceTextureListener);
30
+
31
+ }
32
+
33
+ }
34
34
 
35
35
 
36
36
 
37
- private int mRatioWidth = 0;
37
+ @Override
38
38
 
39
+ public void onDestroy() {
40
+
41
+ Log.e(TAG,"2");
42
+
43
+ // windowからViewを排除する
44
+
39
- private int mRatioHeight = 0;
45
+ windowManager.removeView(views);
40
46
 
41
47
 
42
48
 
43
- public AutoFitTextureView(TextureView textureView, Context context) {
49
+ //onPause()のやつ2つ
44
50
 
45
- this(context, null);
51
+ closeCamera();
46
52
 
47
- }
53
+ stopBackgroundThread();
48
54
 
49
55
 
50
56
 
51
- public AutoFitTextureView(Context context, AttributeSet attrs) {
57
+ // 削除作業を継続させる
52
58
 
53
- this(context, attrs, 0);
54
-
55
- }
56
-
57
-
58
-
59
- public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
60
-
61
- super(context, attrs, defStyle);
59
+ super.onDestroy();
62
-
63
- }
64
-
65
-
66
-
67
- /**
68
-
69
- * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
70
-
71
- * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
72
-
73
- * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
74
-
75
- *
76
-
77
- * @param width Relative horizontal size
78
-
79
- * @param height Relative vertical size
80
-
81
- */
82
-
83
- public void setAspectRatio(int width, int height) {
84
-
85
- if (width < 0 || height < 0) {
86
-
87
- throw new IllegalArgumentException("Size cannot be negative.");
88
-
89
- }
90
-
91
- mRatioWidth = width;
92
-
93
- mRatioHeight = height;
94
-
95
- requestLayout();
96
60
 
97
61
  }
98
62
 
@@ -100,27 +64,53 @@
100
64
 
101
65
  @Override
102
66
 
103
- protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
67
+ public IBinder onBind(Intent intent) {
104
68
 
105
- super.onMeasure(widthMeasureSpec, heightMeasureSpec);
69
+ Log.e(TAG,"3");
106
70
 
107
- int width = MeasureSpec.getSize(widthMeasureSpec);
108
71
 
109
- int height = MeasureSpec.getSize(heightMeasureSpec);
110
72
 
111
- if (0 == mRatioWidth || 0 == mRatioHeight) {
73
+ //この下は本当はonCLickのコード
112
74
 
75
+ if (mIsRecordingVideo) {
76
+
113
- setMeasuredDimension(width, height);
77
+ stopRecordingVideo();
114
78
 
115
79
  } else {
116
80
 
117
- if (width < height * mRatioWidth / mRatioHeight) {
81
+ startRecordingVideo();
118
82
 
119
- setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
83
+ }
120
84
 
121
- } else {
122
85
 
86
+
87
+ return null;
88
+
89
+ }
90
+
91
+
92
+
93
+
94
+
95
+ private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
96
+
97
+
98
+
99
+ @Override
100
+
101
+ public void onOpened(@NonNull CameraDevice cameraDevice) {
102
+
103
+ Log.e(TAG,"11");
104
+
105
+ mCameraDevice = cameraDevice;
106
+
107
+ startPreview();
108
+
109
+ mCameraOpenCloseLock.release();
110
+
111
+ if (null != mTextureView) {
112
+
123
- setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
113
+ configureTransform(mTextureView.getWidth(), mTextureView.getHeight());
124
114
 
125
115
  }
126
116
 
@@ -130,164 +120,60 @@
130
120
 
131
121
 
132
122
 
133
- //このメソッドを追加したのですが、
123
+ private void configureTransform(int viewWidth, int viewHeight) {
134
124
 
135
- public void setSurfaceTextureListener(AutoFitTextureView listener) {
125
+ if (null == mTextureView || null == mPreviewSize ) {
126
+
127
+ return;
128
+
129
+ }
130
+
131
+ WindowManager windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
132
+
133
+ int rotation = windowManager.getDefaultDisplay().getRotation();
134
+
135
+ Matrix matrix = new Matrix();
136
+
137
+ RectF viewRect = new RectF(0, 0, viewWidth, viewHeight);
138
+
139
+ RectF bufferRect = new RectF(0, 0, mPreviewSize.getHeight(), mPreviewSize.getWidth());
140
+
141
+ float centerX = viewRect.centerX();
142
+
143
+ float centerY = viewRect.centerY();
144
+
145
+ if (Surface.ROTATION_90 == rotation || Surface.ROTATION_270 == rotation) {
146
+
147
+ bufferRect.offset(centerX - bufferRect.centerX(), centerY - bufferRect.centerY());
148
+
149
+ matrix.setRectToRect(viewRect, bufferRect, Matrix.ScaleToFit.FILL);
150
+
151
+ float scale = Math.max(
152
+
153
+ (float) viewHeight / mPreviewSize.getHeight(),
154
+
155
+ (float) viewWidth / mPreviewSize.getWidth());
156
+
157
+ matrix.postScale(scale, scale, centerX, centerY);
158
+
159
+ matrix.postRotate(90 * (rotation - 2), centerX, centerY);
160
+
161
+ }
162
+
163
+ mTextureView.setTransform(matrix);
164
+
165
+
166
+
167
+ //ここで、mTextureViewが、nullじゃなければ、startするようにしようと思います。
136
168
 
137
169
  }
138
170
 
139
- }
171
+
172
+
173
+
174
+
175
+
140
176
 
141
177
 
142
178
 
143
179
  ```
144
-
145
- ##Service
146
-
147
- ```java
148
-
149
- public class Camera3Service extends Service {
150
-
151
-
152
-
153
- private TextureView.SurfaceTextureListener mSurfaceTextureListener
154
-
155
- = new TextureView.SurfaceTextureListener() {
156
-
157
- }
158
-
159
-
160
-
161
- @Override
162
-
163
- public void onCreate() {
164
-
165
- super.onCreate();
166
-
167
- // WindowManagerを取得する
168
-
169
- windowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
170
-
171
-
172
-
173
- LayoutInflater inflater = LayoutInflater.from(this);
174
-
175
- views = inflater.inflate(R.layout.service_camera3, null);
176
-
177
- {
178
-
179
-
180
-
181
- WindowManager.LayoutParams params = new WindowManager.LayoutParams(
182
-
183
- // レイアウトの幅 / 高さ設定
184
-
185
- WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
186
-
187
- // レイアウトの挿入位置設定
188
-
189
- // TYPE_SYSTEM_OVERLAYはほぼ最上位に位置して、ロック画面よりも上に表示される。
190
-
191
- // ただし、タッチを拾うことはできない。
192
-
193
- WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
194
-
195
- // ウィンドウ属性
196
-
197
- // TextureViewを利用するには、FLAG_HARDWARE_ACCELERATED が必至となる。
198
-
199
- WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
200
-
201
- // 透過属性を持たなければならないため、TRANSLUCENTを利用する
202
-
203
- PixelFormat.TRANSLUCENT);
204
-
205
-
206
-
207
- // Viewを画面上に重ね合わせする
208
-
209
- windowManager.addView(views, params);
210
-
211
- }
212
-
213
-
214
-
215
- // TextureViewの取得
216
-
217
- textureView = (TextureView) views.findViewById(R.id.texture_view);
218
-
219
-
220
-
221
- // TextureViewの操作用リスナとUIを登録
222
-
223
- {
224
-
225
- // カメラ自体の処理はリスナ側に記述している
226
-
227
- AutoFitTextureView listener = new AutoFitTextureView(textureView, this);
228
-
229
-
230
-
231
- //この下の行にエラーが出ます。コード下の行からその下の行へ変更しました
232
-
233
- // textureView.setSurfaceTextureListener((TextureView.SurfaceTextureListener) listener);
234
-
235
- mTextureView.setSurfaceTextureListener(listener);
236
-
237
-
238
-
239
- }
240
-
241
- }
242
-
243
- ```
244
-
245
- ##//エラーコード
246
-
247
- ```java
248
-
249
- 08-04 12:41:22.589 29149-29149/com.google.android.exoplayer2.demo E/UncaughtException: java.lang.RuntimeException: Unable to create service com.google.android.exoplayer2.demo.Camera3Service: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.demo.AutoFitTextureView.setSurfaceTextureListener(com.google.android.exoplayer2.demo.AutoFitTextureView)' on a null object reference
250
-
251
- at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201)
252
-
253
- at android.app.ActivityThread.-wrap5(ActivityThread.java)
254
-
255
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
256
-
257
- at android.os.Handler.dispatchMessage(Handler.java:102)
258
-
259
- at android.os.Looper.loop(Looper.java:154)
260
-
261
- at android.app.ActivityThread.main(ActivityThread.java:6119)
262
-
263
- at java.lang.reflect.Method.invoke(Native Method)
264
-
265
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
266
-
267
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
268
-
269
- Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.demo.AutoFitTextureView.setSurfaceTextureListener(com.google.android.exoplayer2.demo.AutoFitTextureView)' on a null object reference
270
-
271
- at com.google.android.exoplayer2.demo.Camera3Service.onCreate(Camera3Service.java:115)
272
-
273
- at android.app.ActivityThread.handleCreateService(ActivityThread.java:3191)
274
-
275
- at android.app.ActivityThread.-wrap5(ActivityThread.java) 
276
-
277
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
278
-
279
- at android.os.Handler.dispatchMessage(Handler.java:102) 
280
-
281
- at android.os.Looper.loop(Looper.java:154) 
282
-
283
- at android.app.ActivityThread.main(ActivityThread.java:6119) 
284
-
285
- at java.lang.reflect.Method.invoke(Native Method) 
286
-
287
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
288
-
289
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
290
-
291
-
292
-
293
- ```

2

コードの変更

2017/08/04 06:50

投稿

giant
giant

スコア132

test CHANGED
File without changes
test CHANGED
@@ -146,6 +146,18 @@
146
146
 
147
147
  ```java
148
148
 
149
+ public class Camera3Service extends Service {
150
+
151
+
152
+
153
+ private TextureView.SurfaceTextureListener mSurfaceTextureListener
154
+
155
+ = new TextureView.SurfaceTextureListener() {
156
+
157
+ }
158
+
159
+
160
+
149
161
  @Override
150
162
 
151
163
  public void onCreate() {

1

質問の修正

2017/08/04 04:12

投稿

giant
giant

スコア132

test CHANGED
@@ -1 +1 @@
1
- textureView.setSurfaceTextureListener((TextureView.SurfaceTextureListener) listener);にエラーが出ます。
1
+ textureView.setSurfaceTextureListener(listener);にエラーが出ます。
test CHANGED
@@ -1,12 +1,152 @@
1
+ コードを変更して、listenerを引数にして、
2
+
3
+ //このメソッドをAutoFitTextureViewクラスのコードに追加したのですが、
4
+
1
- このしたのコードに、SurfaceTextureListenerにキャストすることはできません
5
+ public void setSurfaceTextureListener(AutoFitTextureView listener) {
6
+
2
-
7
+ }
8
+
9
+ ところ、「仮想メソッドです」となります。
10
+
3
- と出ます。どうすればいいのでしょうか?
11
+ どうすればいいのでしょうか?
12
+
13
+
14
+
4
-
15
+ ##//AutoFitTextureViewクラス
5
-
6
16
 
7
17
  ```java
8
18
 
19
+ import android.content.Context;
20
+
21
+ import android.util.AttributeSet;
22
+
23
+ import android.view.TextureView;
24
+
25
+
26
+
27
+ /**
28
+
29
+ * A {@link TextureView} that can be adjusted to a specified aspect ratio.
30
+
31
+ */
32
+
33
+ public class AutoFitTextureView extends TextureView {
34
+
35
+
36
+
37
+ private int mRatioWidth = 0;
38
+
39
+ private int mRatioHeight = 0;
40
+
41
+
42
+
43
+ public AutoFitTextureView(TextureView textureView, Context context) {
44
+
45
+ this(context, null);
46
+
47
+ }
48
+
49
+
50
+
51
+ public AutoFitTextureView(Context context, AttributeSet attrs) {
52
+
53
+ this(context, attrs, 0);
54
+
55
+ }
56
+
57
+
58
+
59
+ public AutoFitTextureView(Context context, AttributeSet attrs, int defStyle) {
60
+
61
+ super(context, attrs, defStyle);
62
+
63
+ }
64
+
65
+
66
+
67
+ /**
68
+
69
+ * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
70
+
71
+ * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
72
+
73
+ * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
74
+
75
+ *
76
+
77
+ * @param width Relative horizontal size
78
+
79
+ * @param height Relative vertical size
80
+
81
+ */
82
+
83
+ public void setAspectRatio(int width, int height) {
84
+
85
+ if (width < 0 || height < 0) {
86
+
87
+ throw new IllegalArgumentException("Size cannot be negative.");
88
+
89
+ }
90
+
91
+ mRatioWidth = width;
92
+
93
+ mRatioHeight = height;
94
+
95
+ requestLayout();
96
+
97
+ }
98
+
99
+
100
+
9
- @Override
101
+ @Override
102
+
103
+ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
104
+
105
+ super.onMeasure(widthMeasureSpec, heightMeasureSpec);
106
+
107
+ int width = MeasureSpec.getSize(widthMeasureSpec);
108
+
109
+ int height = MeasureSpec.getSize(heightMeasureSpec);
110
+
111
+ if (0 == mRatioWidth || 0 == mRatioHeight) {
112
+
113
+ setMeasuredDimension(width, height);
114
+
115
+ } else {
116
+
117
+ if (width < height * mRatioWidth / mRatioHeight) {
118
+
119
+ setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
120
+
121
+ } else {
122
+
123
+ setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
124
+
125
+ }
126
+
127
+ }
128
+
129
+ }
130
+
131
+
132
+
133
+ //このメソッドを追加したのですが、
134
+
135
+ public void setSurfaceTextureListener(AutoFitTextureView listener) {
136
+
137
+ }
138
+
139
+ }
140
+
141
+
142
+
143
+ ```
144
+
145
+ ##Service
146
+
147
+ ```java
148
+
149
+ @Override
10
150
 
11
151
  public void onCreate() {
12
152
 
@@ -74,11 +214,13 @@
74
214
 
75
215
  AutoFitTextureView listener = new AutoFitTextureView(textureView, this);
76
216
 
77
-
78
-
217
+
218
+
79
- //この下の行にエラーが出ます。
219
+ //この下の行にエラーが出ます。コード下の行からその下の行へ変更しました
80
-
220
+
81
- textureView.setSurfaceTextureListener((TextureView.SurfaceTextureListener) listener);
221
+ // textureView.setSurfaceTextureListener((TextureView.SurfaceTextureListener) listener);
222
+
223
+ mTextureView.setSurfaceTextureListener(listener);
82
224
 
83
225
 
84
226
 
@@ -88,55 +230,51 @@
88
230
 
89
231
  ```
90
232
 
91
-
233
+ ##//エラーコード
92
234
 
93
235
  ```java
94
236
 
95
- E/AndroidRuntime: FATAL EXCEPTION: main
96
-
97
- Process: com.google.android.exoplayer2.demo, PID: 27533
98
-
99
- java.lang.RuntimeException: Unable to create service com.google.android.exoplayer2.demo.Camera3Service: java.lang.ClassCastException: com.google.android.exoplayer2.demo.AutoFitTextureView cannot be cast to android.view.TextureView$SurfaceTextureListener
100
-
101
- at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201)
102
-
103
- at android.app.ActivityThread.-wrap5(ActivityThread.java)
104
-
105
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
106
-
107
- at android.os.Handler.dispatchMessage(Handler.java:102)
108
-
109
- at android.os.Looper.loop(Looper.java:154)
110
-
111
- at android.app.ActivityThread.main(ActivityThread.java:6119)
112
-
113
- at java.lang.reflect.Method.invoke(Native Method)
114
-
115
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
116
-
117
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
118
-
119
- Caused by: java.lang.ClassCastException: com.google.android.exoplayer2.demo.AutoFitTextureView cannot be cast to android.view.TextureView$SurfaceTextureListener
120
-
121
- at com.google.android.exoplayer2.demo.Camera3Service.onCreate(Camera3Service.java:112)
122
-
123
- at android.app.ActivityThread.handleCreateService(ActivityThread.java:3191)
124
-
125
- at android.app.ActivityThread.-wrap5(ActivityThread.java) 
126
-
127
- at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567
128
-
129
- at android.os.Handler.dispatchMessage(Handler.java:102
130
-
131
- at android.os.Looper.loop(Looper.java:154
132
-
133
- at android.app.ActivityThread.main(ActivityThread.java:6119
134
-
135
- at java.lang.reflect.Method.invoke(Native Method
136
-
137
- at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
138
-
139
- at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
237
+ 08-04 12:41:22.589 29149-29149/com.google.android.exoplayer2.demo E/UncaughtException: java.lang.RuntimeException: Unable to create service com.google.android.exoplayer2.demo.Camera3Service: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.demo.AutoFitTextureView.setSurfaceTextureListener(com.google.android.exoplayer2.demo.AutoFitTextureView)' on a null object reference
238
+
239
+ at android.app.ActivityThread.handleCreateService(ActivityThread.java:3201)
240
+
241
+ at android.app.ActivityThread.-wrap5(ActivityThread.java)
242
+
243
+ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
244
+
245
+ at android.os.Handler.dispatchMessage(Handler.java:102)
246
+
247
+ at android.os.Looper.loop(Looper.java:154)
248
+
249
+ at android.app.ActivityThread.main(ActivityThread.java:6119)
250
+
251
+ at java.lang.reflect.Method.invoke(Native Method)
252
+
253
+ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
254
+
255
+ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
256
+
257
+ Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.exoplayer2.demo.AutoFitTextureView.setSurfaceTextureListener(com.google.android.exoplayer2.demo.AutoFitTextureView)' on a null object reference
258
+
259
+ at com.google.android.exoplayer2.demo.Camera3Service.onCreate(Camera3Service.java:115)
260
+
261
+ at android.app.ActivityThread.handleCreateService(ActivityThread.java:3191)
262
+
263
+ at android.app.ActivityThread.-wrap5(ActivityThread.java) 
264
+
265
+ at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
266
+
267
+ at android.os.Handler.dispatchMessage(Handler.java:102
268
+
269
+ at android.os.Looper.loop(Looper.java:154
270
+
271
+ at android.app.ActivityThread.main(ActivityThread.java:6119
272
+
273
+ at java.lang.reflect.Method.invoke(Native Method
274
+
275
+ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
276
+
277
+ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776
140
278
 
141
279
 
142
280