片段中的媒体控制器
Posted
技术标签:
【中文标题】片段中的媒体控制器【英文标题】:Media Controller in fragments 【发布时间】:2017-04-08 17:00:03 【问题描述】:我正在尝试在 android Studio 中制作媒体控制器,但使用的是片段而不是活动,我正在使用下面的代码,但它给了我以下错误:
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();
【问题讨论】:
请添加日志以更好地帮助您,有什么问题? 错误:(33, 54) 错误:不兼容的类型:Tab1Helloworld 无法转换为上下文 使用 geActivity() 代替这个。 @fightingCoder Error:(25, 19) error: unreachable statement return 语句应该在整个代码之后。 【参考方案1】: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;
【讨论】:
【参考方案2】:@xhen,使用下面的代码,效果很好,
在 xml 布局中添加这个
<sg.xyz.test.util.CustomVideoView
android:id="@+id/videoview"
android:layout_
android:layout_
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
/>
在您的 Fragment 类中调用这些方法
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;
用你的video
对象替换这个mBinding.videoview
就可以了。
现在,创建CustomVideoView
类
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);
【讨论】:
【参考方案3】:你添加的错误的问题
flag Error:(33, 54) error: incompatible types: Tab1Helloworld cannot be converted to Context
在这一行
替换
video.setMediaController(new MediaController(this));
与
video.setMediaController(new MediaController(getActivity()));
也移动
return rootView;
视频开始后到方法结束
【讨论】:
我在这一行出现错误 VideoView video = (VideoView)rootView.findViewById(R.id.videoView); @fightingCoder Error:(26, 19) error: unreachable statement @Hala.M 当我移动返回 rootView 时;不幸的是已停止 请添加日志 在日志中不显示错误,但单击按钮时应用程序本身崩溃以上是关于片段中的媒体控制器的主要内容,如果未能解决你的问题,请参考以下文章
如果使用导航控制器,如何删除某些片段中的底部导航视图和工具栏?