我正在将Exoplayer与ViewPager2配合使用。
主片段。Java语言
public class HomeFragment extends Fragment {
View view;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view= inflater.inflate(R.layout.fragment_home, container, false);
ViewPager2 verticalViewPager = view.findViewById(R.id.vPager);
verticalViewPager.setOrientation(ViewPager2.ORIENTATION_VERTICAL);
ArrayList<VideoDataModel> dataset = new ArrayList<VideoDataModel>();
VideoDataModel videoData = new VideoDataModel();
videoData.setId("1");
videoData.setUrl("https://cdn.videvo.net/videvo_files/video/free/2019-02/small_watermarked/181004_10_LABORATORIES-SCIENCE_22_preview.webm");
videoData.setUserid("112800058400468785815");
videoData.setDescription("Video 1");
dataset.add(videoData);
VideoDataModel videoData1 = new VideoDataModel();
videoData1.setId("2");
videoData1.setUrl("https://cdn.videvo.net/videvo_files/video/premium/video0122/small_watermarked/100a%20Factory_preview.webm");
videoData1.setUserid("112800058400468785815");
videoData1.setDescription("Video 2");
dataset.add(videoData1);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(view.getContext(),dataset);
verticalViewPager.setAdapter(viewPagerAdapter);
return view;
}
@Override
public void onResume() {
super.onResume();
BottomNavigationView bottomNavigationView = ((MainActivity) getActivity()).findViewById(R.id.bottom_navigation);
bottomNavigationView.setBackground( new ColorDrawable(getResources().getColor(android.R.color.black)));
bottomNavigationView.setItemTextColor(ColorStateList.valueOf(Color.WHITE));
bottomNavigationView.setItemIconTintList(ColorStateList.valueOf(Color.WHITE));
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
tools:context=".Home.HomeFragment">
<androidx.viewpager2.widget.ViewPager2
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/vPager"/>
</FrameLayout>
ViewpagerAdapter。Java语言
public class ViewPagerAdapter extends RecyclerView.Adapter<ViewPagerAdapter.ViewHolder> {
Context mContext;
LinearLayout sourdImage_layout;
ImageView soundImage;
LinearLayout shared_layout;
LinearLayout comment_layout;
LinearLayout like_layout;
FrameLayout profile_lt;
LinearLayout gift_layout;
LinearLayout collab_layout;
Button check_bal_btn;
LinearLayout upload;
SharedPreferences pref;
LinearLayout shop;
ArrayList<VideoDataModel> videoDataset;
public ViewPagerAdapter(Context mContext, ArrayList<VideoDataModel> videoDataset) {
this.mContext = mContext;
this.videoDataset = videoDataset;
}
@NonNull
@Override
public ViewPagerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup container, int viewType) {
LayoutInflater mLayoutInflater = LayoutInflater.from(container.getContext());
View itemView = mLayoutInflater.inflate(R.layout.customviewpager, container, false);
ViewHolder viewHolder = new ViewHolder(itemView);
soundImage = itemView.findViewById(R.id.sound_image);
sourdImage_layout = itemView.findViewById(R.id.sound_image_layout);
shared_layout = itemView.findViewById(R.id.shared_layout);
comment_layout = itemView.findViewById(R.id.comment_layout);
like_layout = itemView.findViewById(R.id.like_layout);
profile_lt = itemView.findViewById(R.id.profile_lt);
gift_layout = itemView.findViewById(R.id.gift_layout);
collab_layout = itemView.findViewById(R.id.collab_layout);
upload = itemView.findViewById(R.id.upload);
shop = itemView.findViewById(R.id.shop);
like_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(),"Released",Toast.LENGTH_SHORT).show();
}
});
pref = mContext.getSharedPreferences(Variables.pref_name, 0); // 0 - for private mode
upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( pref.getBoolean(Variables.islogin,false)!=false)
{
Intent intent1 = new Intent(mContext, VideoRecorderActivity.class);
mContext.startActivity(intent1);
}
else
{
Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
}
}
});
shop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( pref.getBoolean(Variables.islogin,false)!=false)
{
Intent intent1 = new Intent(mContext, ShopActivity.class);
mContext.startActivity(intent1);
}
else
{
Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
}
}
});
final BottomSheetDialog CommentBottomSheetDialog = new BottomSheetDialog(mContext);
View sheetView = mLayoutInflater.inflate(R.layout.comment_bottom_sheet, null);
CommentBottomSheetDialog.setContentView(sheetView);
//
final BottomSheetDialog GiftBottomSheetDialog = new BottomSheetDialog(mContext);
View giftSheetView = mLayoutInflater.inflate(R.layout.gift_bottom_sheet, null);
check_bal_btn = giftSheetView.findViewById(R.id.check_bal_btn);
GiftBottomSheetDialog.setContentView(giftSheetView);
check_bal_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent1 = new Intent(mContext, WalletActivity.class);
mContext.startActivity(intent1);
}
});
final BottomSheetDialog collabBottomSheetDialog = new BottomSheetDialog(mContext);
View collabSheetView = mLayoutInflater.inflate(R.layout.collab_bottom_sheet, null);
collabBottomSheetDialog.setContentView(collabSheetView);
RotateAnimation rotate = new RotateAnimation(
0, 360,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f
);
rotate.setDuration(4800);
rotate.setRepeatCount(Animation.INFINITE);
sourdImage_layout.startAnimation(rotate);
//share button
shared_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT,"I suggest this app for you : https://play.google.com/store/apps/details?id=com.android.chrome");
intent.setType("text/plain");
mContext.startActivity(intent);
}
});
//Comments
comment_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( pref.getBoolean(Variables.islogin,false)!=false) {
CommentBottomSheetDialog.show();
}
else
{
Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
}
}
});
//Gift
gift_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( pref.getBoolean(Variables.islogin,false)!=false) {
GiftBottomSheetDialog.show();
}
else
{
Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
}
}
});
//Collab
collab_layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( pref.getBoolean(Variables.islogin,false)!=false) {
collabBottomSheetDialog.show();
}else
{
Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_SHORT).show();
}
}
});
//Open Others Profile
profile_lt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if( pref.getBoolean(Variables.islogin,false)!=false) {
Intent intent1 = new Intent(mContext, OthersProfileActivity.class);
intent1.putExtra("userID", "111805417824620727725");
mContext.startActivity(intent1);
}
else
{
Toast.makeText(mContext,"Pls Login to Continue...",Toast.LENGTH_LONG).show();
}
}
});
return viewHolder;
}
@Override
public void onBindViewHolder(@NonNull ViewPagerAdapter.ViewHolder holder, int position) {
MediaSource videoSource = new ExtractorMediaSource.Factory(holder.datasourceFactory
).createMediaSource(Uri.parse(videoDataset.get(position).getUrl()));
holder.player.prepare(videoSource);
holder.player.setPlayWhenReady(true);
holder.player.setRepeatMode(Player.REPEAT_MODE_ONE);
holder.playerView.setUseController(false);
holder.playerView.hideController();
holder.playerView.setShowBuffering(true);
holder.playerView.setResizeMode(AspectRatioFrameLayout.RESIZE_MODE_FILL);
holder.username.setText(videoDataset.get(position).getUserid());
holder.description.setText(videoDataset.get(position).getDescription());
}
@Override
public int getItemCount() {
return videoDataset.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
private PlayerView playerView;
private SimpleExoPlayer player;
DataSource.Factory datasourceFactory;
TextView username;
TextView description;
public ViewHolder(View itemView) {
super(itemView);
playerView = itemView.findViewById(R.id.playerview);
username = itemView.findViewById(R.id.username);
description = itemView.findViewById(R.id.desc_txt);
player = ExoPlayerFactory.newSimpleInstance(itemView.getContext(),new DefaultTrackSelector());
playerView.setPlayer(player);
datasourceFactory= new DefaultDataSourceFactory(itemView.getContext(),
Util.getUserAgent(itemView.getContext(), "videoApp"));
}
}
}
customviewPager。xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="@color/black"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/tools"
app:ignore="NamespaceTypo">
<androidx.cardview.widget.CardView
android:id="@+id/card_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
card_view:cardBackgroundColor="@color/transparent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mainlayout">
<com.google.android.exoplayer2.ui.PlayerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/playerview"
app:show_buffering="when_playing"
app:controller_layout_id="@layout/item_player_contoller"
android:layout_gravity="center"
app:surface_type="surface_view"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
我想在滑动页面时释放并停止播放器。只有当前正在查看的页面才应该运行exoplayer。运行所有播放器会导致性能问题。我尝试使用onViewDetachedFromWindow方法释放播放器,但如果我向后滑动,我的播放器将无法播放。。。
覆盖ViewpagerAdapter中的以下方法。java类
和类查看者函数,如下所示
@Override
public void onViewDetachedFromWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewDetachedFromWindow(holder);
holder.pausePlayer();
holder.releasePlayer();
}
@Override
public void onViewAttachedToWindow(@NonNull VideoPlayerViewHolder holder) {
super.onViewAttachedToWindow(holder);
holder.startPlayer();
}
ViewHolder函数
public void startPlayer() {
player.setPlayWhenReady(true);
player.getPlaybackState();
}
public void pausePlayer() {
player.setPlayWhenReady(false);
player.getPlaybackState();
}
public void releasePlayer() {
player.setPlayWhenReady(false);
player.release();
player.stop(true);
}
如果有任何错误,请告诉我。。?
ViewPager
具有回调函数,可能对您的情况有所帮助
onPageScrolled()
-当前页面发生滚动事件时触发您可以在选择新页面时停止旧的exoplayer并启动新页面
在恢复时再次初始化你的播放器,因为你已经释放了播放器的表面。
问题内容: 我创建了一个JFrame,它的中央有一个矩形,当我按某些键时,该矩形会移动。一切都很好,但当我松开按键时,矩形会继续前进。实际上,如果我多次按一个键,则矩形会加速。这可能是(肯定),因为在按下某个键时,我正在使用计时器来避免令人讨厌的0.5秒输入延迟。 我认为我必须在该方法中添加一些内容,但是我对放置在其中的内容一无所知。有小费吗?谢谢。 PS: 请 不要对我不使用按键绑定大吼大叫。我
在从to, Thymeleaf停止工作。 我已经按照Thymeleaf3迁移指南阅读了Spring Boot发行版,但没有成功。。。 路径比较: 谢谢你的帮助。谢谢 编辑: @标记,模型设置: 默认情况下为上下文路径()
我创建的Android应用程序包含2个按钮,点击每个按钮播放mp3文件。问题是,当我播放按钮1时,它播放声音1,当我单击按钮2时,它播放声音2。 我检查其他玩家的每个按钮是否正常工作,然后停止并播放单击的按钮 但是,如果我两次单击同一个按钮,则会将第一个音频保持在后台播放,然后再次播放另一个音频 我试图检查并停止它,但它不起作用! 我想,如果我点击按钮1,它将播放声音,如果再次点击它,它将停止播放
ap.stopBackgroundAudio(CALLBACK) 停止播放音乐。 代码示例 <script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script> <style>.output{ display:block; max-width: 100%;
我试图通过使用寻求酒吧听众播放一首歌。但是当我尝试拖动进度条时,它会从歌曲的开头播放。它需要从那个被拖动的位置演奏。 这是我的代码: 如何解决这个问题。提前谢谢。。
我的应用程序中有一项服务。它发送和接收webservice请求和响应。当我收到来自web服务的特定响应时,我需要停止播放音频/视频文件(媒体文件正在从另一个应用程序播放)。当我完成某个过程时,我需要再次恢复。 注: 在andoid设备中,如果任何2G呼叫停止播放媒体文件,则音乐播放器播放任何音乐,然后当2G呼叫断开时,停止播放媒体文件。我只需要这样做。