###前提・実現したいこと
Androidにてバックグラウンドでカメラを起動しつつ、前面で動画を再生するアプリケーションを作成しています。(実際にはViewPagerで隠しています)
また動画はViewPagerを使用してフリックなどで画面を切り替えられるようにしています。
カメラのプレビューを隠すため、ViewPagerのBackgroundを黒で塗りつぶしています。
しかし上手く動画が表示されません。
カメラの映像を出さず、動画を正しく再生する方法はありますか?
###発生している問題・エラーメッセージ
実行したとき、動画(VideoView)の部分に動画ではなくカメラの映像が表示されてしまいます。
動画の音声は再生されていました。
###該当のソースコード
java
1 2public class MainActivity extends AppCompatActivity { 3 4 private SurfaceView mySurfaceView; 5 private Camera myCamera; //hardware 6 7 @Override 8 protected void onCreate(Bundle savedInstanceState) { 9 super.onCreate(savedInstanceState); 10 setContentView(R.layout.activity_main); 11 12 mySurfaceView = (SurfaceView)findViewById(R.id.mySurfaceView); 13 //SurfaceHolder(SVの制御に使うInterface) 14 SurfaceHolder holder = mySurfaceView.getHolder(); 15 //コールバックを設定 16 holder.addCallback(callback); 17 } 18 19 //コールバック 20 private SurfaceHolder.Callback callback = new SurfaceHolder.Callback() { 21 @Override 22 public void surfaceCreated(SurfaceHolder surfaceHolder) { 23 //CameraOpen 24 myCamera = Camera.open(); 25 26 //出力をSurfaceViewに設定 27 try{ 28 myCamera.setPreviewDisplay(surfaceHolder); 29 }catch(Exception e){ 30 e.printStackTrace(); 31 } 32 } 33 34 @Override 35 public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i2, int i3) { 36 //プレビュースタート(Changedは最初にも1度は呼ばれる) 37 myCamera.startPreview(); 38 } 39 40 @Override 41 public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 42 //片付け 43 myCamera.release(); 44 myCamera = null; 45 } 46 }; 47 48 @Override 49 public View onCreateView(LayoutInflater inflater, ViewGroup container, 50 Bundle savedInstanceState) { 51 View rootView = inflater.inflate(R.layout.fragment_main, container, false); 52 TextView textView = (TextView) rootView.findViewById(R.id.section_label); 53 textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 54 textView.setTextColor(Color.RED); 55 56 String movePath = Environment.getExternalStorageDirectory().getPath() + "/test/test.mp4"; 57 58 LinearLayout layout = (LinearLayout)rootView.findViewById(R.id.innerLayout); 59 60 VideoView videoView = new VideoView(getContext()); 61 videoView.setVideoPath(movePath); 62 63 LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); 64 lp.gravity = Gravity.CENTER; 65 66 videoView.setLayoutParams(lp); 67 68 layout.addView(videoView); 69 70 videoView.start(); 71 72 return rootView; 73 } 74} 75
XML
1activity_main.xml 2<?xml version="1.0" encoding="utf-8"?> 3<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" 4 xmlns:tools="http://schemas.android.com/tools" 5 xmlns:app="http://schemas.android.com/apk/res-auto" 6 android:id="@+id/main_content" 7 android:layout_width="match_parent" 8 android:layout_height="match_parent" 9 android:fitsSystemWindows="true" 10 tools:context="com.example.nara.testproject.MainActivity"> 11 12 <android.support.design.widget.AppBarLayout 13 android:id="@+id/appbar" 14 android:layout_width="match_parent" 15 android:layout_height="wrap_content" 16 android:paddingTop="@dimen/appbar_padding_top" 17 android:theme="@style/AppTheme.AppBarOverlay"> 18 19 <android.support.v7.widget.Toolbar 20 android:id="@+id/toolbar" 21 android:layout_width="match_parent" 22 android:layout_height="?attr/actionBarSize" 23 android:background="?attr/colorPrimary" 24 app:popupTheme="@style/AppTheme.PopupOverlay" 25 app:layout_scrollFlags="scroll|enterAlways"> 26 27 </android.support.v7.widget.Toolbar> 28 29 </android.support.design.widget.AppBarLayout> 30 31 <SurfaceView 32 android:layout_width="match_parent" 33 android:layout_height="match_parent" 34 android:id="@+id/mySurfaceView" /> 35 36 <android.support.v4.view.ViewPager 37 android:id="@+id/container" 38 android:layout_width="match_parent" 39 android:layout_height="match_parent" 40 android:background="#000000" 41 app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 42 43 <android.support.design.widget.FloatingActionButton 44 android:id="@+id/fab" 45 android:layout_width="wrap_content" 46 android:layout_height="wrap_content" 47 android:layout_gravity="end|bottom" 48 android:layout_margin="@dimen/fab_margin" 49 app:srcCompat="@android:drawable/ic_dialog_email" /> 50 51</android.support.design.widget.CoordinatorLayout> 52 53 54flagment_main.xml 55<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 56 android:id="@+id/work_process_parent_layout" 57 android:layout_width="match_parent" 58 android:layout_height="match_parent"> 59 60 <LinearLayout 61 android:orientation="vertical" 62 android:layout_width="match_parent" 63 android:layout_height="match_parent" 64 android:id="@+id/innerLayout"></LinearLayout> 65 66 <TextView 67 android:id="@+id/section_label" 68 android:layout_width="wrap_content" 69 android:layout_height="wrap_content" /> 70 71</FrameLayout>
###補足情報(言語/FW/ツール等のバージョンなど)
AndroidStudio2.2
Android4.4
プロジェクトのベースはTabbed Activityです。
該当のソースコードには今回追加した部分のみ書いています。
権限にはファイルの読み書き、カメラを追加しています。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/09/26 06:48
2016/09/26 07:11
2016/09/26 07:15
2016/09/26 07:42
2016/09/26 08:45
2016/09/26 09:32
2016/09/26 10:50
2016/09/27 00:53
2016/09/27 01:13
2016/09/27 02:39
2016/09/27 04:35
2016/09/27 04:38
2016/09/27 08:33