Media Controller in fragments

public class Tab1Helloworld extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.tab1helloworld, container, false); return rootView;

    VideoView video = (VideoView)rootView.findViewById(R.id.videoView);

    video.requestFocus();

    String videopath = "android.resource://com.r.teamkape.kiddiecodiel/" + R.raw.helloworldvid;

    video.setVideoURI(Uri.parse(videopath));

    video.setMediaController(new MediaController(this)); //error in (this)[enter image description here][1]

    video.requestFocus();
    video.start();
}

}


@xhen, Use below code this works perfect,

in xml layout add this

<sg.xyz.test.util.CustomVideoView
    android:id="@+id/videoview"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentBottom="true"
    android:layout_alignParentTop="true"
     />

in your Fragment class call these medthods

 private MediaController mediaController;
 private Uri uri;

 public void setupVideo(){
 if (mediaController == null) {
                mediaController = new
                        MediaController(mContext);

                mediaController.setAnchorView(mBinding.videoview);
                mBinding.videoview.setMediaController(mediaController);
            }
            try {
                uri = Util.buildURiPath(resourceID);
                mBinding.videoview.setVideoURI(uri);

            } catch (Exception e) {
                Logger.debug("Error: " + e.getMessage());
                e.printStackTrace();
            }
            mBinding.videoview.requestFocus();
            mBinding.imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                    mBinding.videoview.start();

                    mBinding.imageView.setVisibility(View.INVISIBLE);

                }
            });
            mBinding.videoview.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {

                    mp.setOnVideoSizeChangedListener(new MediaPlayer.OnVideoSizeChangedListener() {
                        @Override
                        public void onVideoSizeChanged(MediaPlayer mediaPlayer, int width, int height) {
                            mediaController.setAnchorView(mBinding.videoview);
                        }
                    });
                    Logger.debug("Duration = " +
                            mBinding.videoview.getDuration());

                }
            });
            mBinding.videoview.setOnErrorListener(new MediaPlayer.OnErrorListener() {
                @Override
                public boolean onError(MediaPlayer mp, int what, int extra) {
                    AlertDialogFactory.buildAlertDialog(mContext, 0, R.string.video_playing_error).show();
                    return false;
                }
            });
 }


public static Uri buildURiPath(int resourceID)
{
    String uriPath = String.valueOf(Uri.parse("android.resource://" + LoginActivity.PACKAGE_NAME + "/"
            +resourceID));
    Uri uri = Uri.parse(uriPath);
    return uri;
}

Replace this mBinding.videoview with your video object that's it. Now, Create CustomVideoView class

public class CustomVideoView extends VideoView {

private int width;
private int height;
private Context context;
private VideoSizeChangeListener listener;
private boolean isFullscreen;

public CustomVideoView(Context context) {
    super(context);
    init(context);
}

public CustomVideoView(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context);
}

/**
 * get video screen width and height for calculate size
 *
 * @param context Context
 */
private void init(Context context) {
    this.context = context;
    setScreenSize();
}

/**
 * calculate real screen size
 */
private void setScreenSize() {
    Display display = ((Activity) context).getWindowManager().getDefaultDisplay();

    if (Build.VERSION.SDK_INT >= 17) {
        //new pleasant way to get real metrics
        DisplayMetrics realMetrics = new DisplayMetrics();
        display.getRealMetrics(realMetrics);
        width = realMetrics.widthPixels;
        height = realMetrics.heightPixels;

    } else if (Build.VERSION.SDK_INT >= 14) {
        //reflection for this weird in-between time
        try {
            Method mGetRawH = Display.class.getMethod("getRawHeight");
            Method mGetRawW = Display.class.getMethod("getRawWidth");
            width = (Integer) mGetRawW.invoke(display);
            height = (Integer) mGetRawH.invoke(display);
        } catch (Exception e) {
            //this may not be 100% accurate, but it's all we've got
            width = display.getWidth();
            height = display.getHeight();
        }

    } else {
        //This should be close, as lower API devices should not have window navigation bars
        width = display.getWidth();
        height = display.getHeight();
    }

    // when landscape w > h, swap it
    if (width > height) {
        int temp = width;
        width = height;
        height = temp;
    }
}

/**
 * set video size change listener
 *
 */
public void setVideoSizeChangeListener(VideoSizeChangeListener listener) {
    this.listener = listener;
}

public interface VideoSizeChangeListener {
    /**
     * when landscape
     */
    void onFullScreen();

    /**
     * when portrait
     */
    void onNormalSize();
}

@Override
public void setVideoURI(Uri uri) {
    MediaMetadataRetriever retriever = new MediaMetadataRetriever();
    retriever.setDataSource(this.getContext(), uri);
    width = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_WIDTH));
    height = Integer.parseInt(retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_VIDEO_HEIGHT));
    super.setVideoURI(uri);
    super.setVideoURI(uri);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    if (context.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // full screen when landscape
        setSize(height, width);
        if (listener != null) listener.onFullScreen();
        isFullscreen = true;
    } else {
        // height = width * 9/16
        setSize(width, width * 9 / 16);
        if (listener != null) listener.onNormalSize();
        isFullscreen = false;
    }
}

/**
 * @return true: fullscreen
 */
public boolean isFullscreen() {
    return isFullscreen;
}

/**
 * set video sie
 *
 * @param w Width
 * @param h Height
 */
private void setSize(int w, int h) {
    setMeasuredDimension(w, h);
    getHolder().setFixedSize(w, h);
}
}

public class Tab1Helloworld extends Fragment 
{ 
  @Override 
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
     { View rootView = inflater.inflate(R.layout.tab1helloworld,container, false); 

     VideoView video = (VideoView)rootView.findViewById(R.id.videoView);

       video.requestFocus();

       String videopath = "android.resource://com.r.teamkape.kiddiecodiel/" + R.raw.helloworldvid;

       video.setVideoURI(Uri.parse(videopath));

       video.setMediaController(new MediaController(getActivity())); //error in (this)[enter image description here][1]

       video.requestFocus();
       video.start();
       return rootView;
          }
   }

the issue from the error you added

flag    Error:(33, 54) error: incompatible types: Tab1Helloworld cannot be converted to Context

is in this line

replace

video.setMediaController(new MediaController(this)); 

with

 video.setMediaController(new MediaController(getActivity())); 

also move

return rootView; 

to the end of the method after starting the video

链接地址: http://www.djcxy.com/p/81420.html

上一篇: 如何保存CHOICE MODE MULTIPLE

下一篇: 媒体控制器分段