如何在Android中的片段中加载SurfaceView

时间:2016-10-06 08:22:25

标签: android android-mediaplayer surfaceview

我正在我的应用程序中的Activity类中播放视频,它的工作正常。我是这样做的

PreLoginActivity extends AppCompatActivity implements SurfaceHolder.Callback

编写用于播放视频的代码

public void playVideoStream(final SurfaceHolder holderRef)
{
    Log.v("act 1","log 1<======");
    try
    {
        //new Thread(new Runnable() {
            //public void run()
                // {
                Uri video = Uri.parse("android.resource://" + getActivity().getPackageName() + "/" + R.raw.xyz);
                Log.v("act 2","log 2<======");
                mediaPlayer = MediaPlayer.create(getActivity(),video,holderRef);
                Log.v("act 3","log 3<======");
                //mediaPlayer.setDisplay(holderRef);
                Log.v("act 4","log 4<======");
                //Get the dimensions of the video
                int videoWidth = mediaPlayer.getVideoWidth();
                int videoHeight = mediaPlayer.getVideoHeight();
                //Get the width of the screen
                int screenWidth = getActivity().getWindowManager().getDefaultDisplay().getWidth();
                //Get the SurfaceView layout parameters
                android.view.ViewGroup.LayoutParams lp = surfaceView.getLayoutParams();
                //Start video
                mediaPlayer.start();
                mediaPlayer.setLooping(true);
            //}
        //});
    }
    catch (Exception e)
    {
        Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_LONG).show();
    }
}

我最近为我的应用程序实现了BottomBar,并将用于播放视频的代码库从Activity转移到Fragment,这就是我的代码现在的样子

public class HomeTabFragment extends Fragment implements SurfaceHolder.Callback

并在按钮点击和surfaceCreated方法上调用相同的方法,没有任何运气

它没有显示视频,任何人都可以帮助我

0 个答案:

没有答案