Closed mayursancheti closed 2 months ago
I'm afraid we're unable to review such an enormous amount of code for correctness - we simply don't have the resources.
Please make your question more focussed in order for us to help you.
ok i understand you are confusing code,
step 1) i have declare exoplayer in viewpager
simpleExoPlayer = new SimpleExoPlayer.Builder(context).build();
String userAgent = Util.getUserAgent(context, context.getString(R.string.app_name));
DefaultDataSourceFactory defdataSourceFactory = new DefaultDataSourceFactory(context,userAgent);
Uri uriOfContentUrl = Uri.parse(proxyUrl);
MediaSource mediaSource = new ProgressiveMediaSource.Factory(defdataSourceFactory).createMediaSource(uriOfContentUrl); // creating a media source
simpleExoPlayer.prepare(mediaSource);
//video_for_add_text.hideController();
//simpleExoPlayer.setPlayWhenReady(true); // start loading video and play it at the moment a
simpleExoPlayer.setVideoScalingMode(VIDEO_SCALING_MODE_SCALE_TO_FIT);
//simpleExoPlayer.play();
simpleExoPlayer.setRepeatMode(Player.REPEAT_MODE_ONE); //to loop in video
simpleExoPlayer.setVideoScalingMode(VIDEO_SCALING_MODE_SCALE_TO_FIT);
mVideoView.setPlayer(simpleExoPlayer);
my code is 1) mixing multiple videos sound is playing when i swipe viewpager or change activity and 2) exoplayer loading very slow for url based video .
@mayursancheti I have solved the issue earlier. When the view pager is swiped, it creates another view holder, and every view holder holds a unique playerView instance. So, to control the player, you need to store all holder objects in a hashmap. The hasmaps key will be the adapter
position, and the Value will be the viewHolder
object. Ex: var hashMap: HashMap<Int, ViewHolderClass> = HashMap()
In onBindViewHolder, // add holder with its index to map
if (hashMap.containsKey(position)){ hashMap.remove(position) } hashMap[position] = holder
Now, pause the previous player when playing new videos. Just get the playing position in the adapter and control (play, pause, or release) the player view of that viewholder. This will help you to understand the video playback inside the view pager.
In Addition, when the page is swiped to another, it calls the onViewDetachedFromWindow
function in the adapter
class, So overriding the function, Pause the current playing video. Also you can handle viewPagers OnPageChangeCallback()
, override onPageSelected(position: Int)
. Make a public function inside adapter to pause the current play video and play new video, then call the function from here You can find the solution in my demo project. It will fix the mixing video playback issue. Make sure to release player from onDetachedFromRecyclerView
and onViewRecycled
, to optimize the app.
`
override fun onViewDetachedFromWindow(holder: VideoViewHolder) {
super.onViewDetachedFromWindow(holder)
holder.binding.videoFrame.player?.pause()
simpleCache.release()
}
override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) {
super.onDetachedFromRecyclerView(recyclerView)
player?.release()
simpleCache.release()
}
override fun onViewRecycled(holder: VideoViewHolder) {
super.onViewRecycled(holder)
holder.binding.videoFrame.player?.release()
simpleCache.release()
}`
For your reference: https://github.com/sayedsyfuzzaman/Swipeable-Video-Player-Android
@mayursancheti I have solved the issue earlier. When the view pager is swiped, it creates another view holder, and every view holder holds a unique playerView instance. So, to control the player, you need to store all holder objects in a hashmap. The hasmaps key will be the
adapter
position, and the Value will be theviewHolder
object. Ex:var hashMap: HashMap<Int, ViewHolderClass> = HashMap()
In onBindViewHolder,
// add holder with its index to map
if (hashMap.containsKey(position)){ hashMap.remove(position) } hashMap[position] = holder
Now, pause the previous player when playing new videos. Just get the playing position in the adapter and control (play, pause, or release) the player view of that viewholder. This will help you to understand the video playback inside the view pager.
In Addition, when the page is swiped to another, it calls the
onViewDetachedFromWindow
function in theadapter
class, So overriding the function, Pause the current playing video. Also you can handle viewPagersOnPageChangeCallback()
, overrideonPageSelected(position: Int)
. Make a public function inside adapter to pause the current play video and play new video, then call the function from here You can find the solution in my demo project. It will fix the mixing video playback issue. Make sure to release player fromonDetachedFromRecyclerView
andonViewRecycled
, to optimize the app. `override fun onViewDetachedFromWindow(holder: VideoViewHolder) { super.onViewDetachedFromWindow(holder) holder.binding.videoFrame.player?.pause() simpleCache.release() } override fun onDetachedFromRecyclerView(recyclerView: RecyclerView) { super.onDetachedFromRecyclerView(recyclerView) player?.release() simpleCache.release() } override fun onViewRecycled(holder: VideoViewHolder) { super.onViewRecycled(holder) holder.binding.videoFrame.player?.release() simpleCache.release() }`
For your reference: https://github.com/sayedsyfuzzaman/Swipeable-Video-Player-Android
Issue solved . Thanks Brother
Closing because it seems the question was answered above.
ExoPlayer Version
2.15.1
Devices that reproduce the issue
All android devices from 8 onwards, i test in moto, nokia, narzo but still same issue
Devices that do not reproduce the issue
-
Reproducible in the demo app?
No
Reproduction steps
i tried below code `package com.example.splashscreen;
import static androidx.viewpager2.widget.ViewPager2.OFFSCREEN_PAGE_LIMIT_DEFAULT;
import androidx.annotation.NonNull; import androidx.viewpager2.widget.ViewPager2;
import android.app.NotificationChannel; import android.app.NotificationManager; import android.content.SharedPreferences; import android.os.Build; import android.os.Bundle; import android.util.Log; import android.view.MotionEvent; import android.view.ViewGroup; import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import com.google.firebase.database.ValueEventListener; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.storage.StorageReference;
import java.util.ArrayList; import java.util.List; import java.util.Objects;
//import static com.example.splashscreen.VideosAdapter.VideoViewHolder.nofication_imag;
public class MainActivity extends common_bottom_menu {
}`
and for adapter below code,
`package com.example.splashscreen;
import static android.content.Context.MODE_PRIVATE; import static android.media.MediaPlayer.VIDEO_SCALING_MODE_SCALE_TO_FIT;
import static com.example.splashscreen.App.getProxy; import static com.facebook.FacebookSdk.getApplicationContext;
import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; import android.net.Uri; import android.os.Bundle; import android.os.Looper; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.inputmethod.InputMethodManager; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.MediaController; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import android.widget.VideoView;
import androidx.annotation.NonNull; import androidx.appcompat.app.AlertDialog; import androidx.fragment.app.FragmentActivity; //import androidx.multidex.BuildConfig; import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide; import com.bumptech.glide.request.RequestOptions; import com.danikula.videocache.BuildConfig; import com.danikula.videocache.HttpProxyCacheServer; import com.google.android.exoplayer2.Player; import com.google.android.exoplayer2.SimpleExoPlayer; import com.google.android.exoplayer2.source.MediaSource; import com.google.android.exoplayer2.source.ProgressiveMediaSource; import com.google.android.exoplayer2.ui.PlayerView; import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory; import com.google.android.exoplayer2.upstream.cache.SimpleCache; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; import com.google.firebase.database.ChildEventListener; import com.google.firebase.database.DataSnapshot; import com.google.firebase.database.DatabaseError; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.ValueEventListener;
import java.util.HashMap; import java.util.List; import java.util.Objects;
public class MainActivity_VideosAdapter extends RecyclerView.Adapter {
private List mVideoItems;
DatabaseReference likedata;
boolean testclick = true;
String TAG = "100";
}
`
but this program just playing multiple videos sound on swipe , even if i changes activity still exoplayer sound playing in background.
Expected result
Actual result
-
Media
-
Bug Report
adb bugreport
to dev.exoplayer@gmail.com after filing this issue.