@@ -109,7 +109,10 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
109109 height = actualHeight
110110 }
111111
112- val isVideo = model.uri?.lowercase()?.endsWith(" .mp4" ) == true || (model.uri?.startsWith(" content://" ) == true && view.context.contentResolver.getType(android.net.Uri .parse(model.uri))?.startsWith(" video/" ) == true )
112+ val isVideo = model.uri?.lowercase()
113+ ?.endsWith(" .mp4" ) == true || (model.uri?.startsWith(" content://" ) == true && view.context.contentResolver.getType(
114+ android.net.Uri .parse(model.uri)
115+ )?.startsWith(" video/" ) == true )
113116
114117 // set or clear the click listener, videos should not open the external viewer
115118 if (isVideo) {
@@ -128,12 +131,18 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
128131 val fd = cr.openFileDescriptor(uri, " r" )
129132 fd?.fileDescriptor?.let { retriever.setDataSource(it) }
130133 // get frame at 0ms or closest
131- thumbBmp = retriever.getFrameAtTime(0 , android.media.MediaMetadataRetriever .OPTION_CLOSEST )
134+ thumbBmp = retriever.getFrameAtTime(
135+ 0 ,
136+ android.media.MediaMetadataRetriever .OPTION_CLOSEST
137+ )
132138 } catch (_: Exception ) {
133139 // ignore and fallback
134140 thumbBmp = null
135141 } finally {
136- try { retriever.release() } catch (_: Exception ) {}
142+ try {
143+ retriever.release()
144+ } catch (_: Exception ) {
145+ }
137146 }
138147
139148 if (thumbBmp != null ) {
@@ -149,10 +158,22 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
149158
150159 // Always show thumbnail on bind and clear any previously attached media/listeners.
151160 // This prevents VideoView from auto-starting when the view is rebound.
152- try { videoView?.stopPlayback() } catch (_: Exception ) {}
153- try { videoView?.setOnPreparedListener(null ) } catch (_: Exception ) {}
154- try { videoView?.setOnCompletionListener(null ) } catch (_: Exception ) {}
155- try { videoView?.setOnErrorListener(null ) } catch (_: Exception ) {}
161+ try {
162+ videoView?.stopPlayback()
163+ } catch (_: Exception ) {
164+ }
165+ try {
166+ videoView?.setOnPreparedListener(null )
167+ } catch (_: Exception ) {
168+ }
169+ try {
170+ videoView?.setOnCompletionListener(null )
171+ } catch (_: Exception ) {
172+ }
173+ try {
174+ videoView?.setOnErrorListener(null )
175+ } catch (_: Exception ) {
176+ }
156177 // Reset UI to thumbnail state (do not auto-start playback on bind)
157178 videoView?.visibility = View .GONE
158179 imageView.visibility = View .VISIBLE
@@ -165,19 +186,34 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
165186 val resetToThumbnail: () -> Unit = {
166187 try {
167188 // clear model playback state
168- (itemView.tag as ? Model )?.let { it.isPlaying = false ; it.lastPlaybackPosition = 0 }
189+ (itemView.tag as ? Model )?.let {
190+ it.isPlaying = false ; it.lastPlaybackPosition = 0
191+ }
169192 currentlyPlayingVideo = null
170193 currentlyPlayingHolder = null
171194 // stop and clear listeners to fully reset
172- try { videoView?.stopPlayback() } catch (_: Exception ) {}
173- try { videoView?.setOnPreparedListener(null ) } catch (_: Exception ) {}
174- try { videoView?.setOnCompletionListener(null ) } catch (_: Exception ) {}
175- try { videoView?.setOnErrorListener(null ) } catch (_: Exception ) {}
195+ try {
196+ videoView?.stopPlayback()
197+ } catch (_: Exception ) {
198+ }
199+ try {
200+ videoView?.setOnPreparedListener(null )
201+ } catch (_: Exception ) {
202+ }
203+ try {
204+ videoView?.setOnCompletionListener(null )
205+ } catch (_: Exception ) {
206+ }
207+ try {
208+ videoView?.setOnErrorListener(null )
209+ } catch (_: Exception ) {
210+ }
176211 videoView?.visibility = View .GONE
177212 imageView.visibility = View .VISIBLE
178213 playButton?.visibility = View .VISIBLE
179214 pauseButton?.visibility = View .GONE
180- } catch (_: Exception ) {}
215+ } catch (_: Exception ) {
216+ }
181217 }
182218
183219 // play button starts inline playback using VideoView
@@ -186,39 +222,50 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
186222
187223 // if the same video is tracked as currently playing and it's only paused, resume
188224 if (currentlyPlayingVideo == videoView && videoView?.isPlaying == false ) {
189- // resume from current position (MediaPlayer preserves paused position)
190- videoView.start()
191- (itemView.tag as ? Model )?.let { it.isPlaying = true }
192- pauseButton?.visibility = View .VISIBLE
193- playButton.visibility = View .GONE
194- currentlyPlayingVideo = videoView
195- currentlyPlayingHolder = this
196- return @setOnClickListener
197- }
225+ // resume from current position (MediaPlayer preserves paused position)
226+ videoView.start()
227+ (itemView.tag as ? Model )?.let { it.isPlaying = true }
228+ pauseButton?.visibility = View .VISIBLE
229+ playButton.visibility = View .GONE
230+ currentlyPlayingVideo = videoView
231+ currentlyPlayingHolder = this
232+ return @setOnClickListener
233+ }
198234
199235 // stop any currently playing video first
200236 if (currentlyPlayingVideo != null ) {
201237 try {
202238 currentlyPlayingHolder?.let { holder ->
203239 // save current playback position for the other holder, and pause it
204240 (holder.itemView.tag as ? Model )?.let { m ->
205- try { m.lastPlaybackPosition = holder.videoView?.currentPosition ? : 0 } catch (_: Exception ) {}
241+ try {
242+ m.lastPlaybackPosition =
243+ holder.videoView?.currentPosition ? : 0
244+ } catch (_: Exception ) {
245+ }
206246 m.isPlaying = false
207247 }
208- try { holder.videoView?.pause() } catch (_: Exception ) {}
248+ try {
249+ holder.videoView?.pause()
250+ } catch (_: Exception ) {
251+ }
209252 holder.pauseButton?.visibility = View .GONE
210253 holder.playButton?.visibility = View .VISIBLE
211254 holder.videoView?.visibility = View .GONE
212255 holder.imageView.visibility = View .VISIBLE
213256 }
214- } catch (_: Exception ) {}
257+ } catch (_: Exception ) {
258+ }
215259 currentlyPlayingVideo = null
216260 currentlyPlayingHolder = null
217261 }
218262
219263 val uri = android.net.Uri .parse(model.uri)
220264 // prepare video; do not assume it's already prepared from previous binds
221- try { videoView?.stopPlayback() } catch (_: Exception ) {}
265+ try {
266+ videoView?.stopPlayback()
267+ } catch (_: Exception ) {
268+ }
222269 videoView?.setVideoURI(uri)
223270 // ensure UI updates for playback start
224271 imageView.visibility = View .GONE
@@ -262,7 +309,11 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
262309 try {
263310 if (videoView?.isPlaying == true ) {
264311 // persist current position and pause without fully stopping playback
265- try { (itemView.tag as ? Model )?.lastPlaybackPosition = videoView.currentPosition } catch (_: Exception ) {}
312+ try {
313+ (itemView.tag as ? Model )?.lastPlaybackPosition =
314+ videoView.currentPosition
315+ } catch (_: Exception ) {
316+ }
266317 (itemView.tag as ? Model )?.isPlaying = false
267318 videoView.pause()
268319 pauseButton.visibility = View .GONE
@@ -280,7 +331,8 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
280331
281332 val preview = if (model.uri == " NA" ) {
282333 // show NA placeholder only when value is literally "NA"
283- val data = context.resources.assets.open(" na_placeholder.jpg" ).readBytes()
334+ val data =
335+ context.resources.assets.open(" na_placeholder.jpg" ).readBytes()
284336 BitmapFactory .decodeByteArray(data, 0 , data.size)
285337 } else if (model.uri?.contains(" content://" ) != true ) {
286338 // external/no content URI: show nothing or placeholder label
@@ -289,12 +341,16 @@ class ImageAdapter(private val context: Context, private val listener: ImageItem
289341 BitmapLoader .getPreview(view.context, model.uri, model.orientation)
290342 }
291343
292- imageView.setImageBitmap(preview)
344+ imageView.setImageBitmap(preview)
293345
294- } catch (f: FileNotFoundException ) {
346+ } catch (f: FileNotFoundException ) {
295347
296- f.printStackTrace()
348+ f.printStackTrace()
297349
350+ }
351+ }
352+ } catch (e: Exception ) {
353+ e.printStackTrace()
298354 }
299355 }
300356 }
0 commit comments