green-code-initiative / ecoCode-challenge

Emboard in the hackhatons serie for improving ecoCode
3 stars 4 forks source link

[iOS] [Android] Videos and media content should not be preloaded #8

Open PierreEc opened 1 year ago

PierreEc commented 1 year ago

Lorsque l'on utilise le player native, et qu'on lance la méthode play dans une action comme onAppear, onDispose ou autre

https://developer.apple.com/documentation/avfoundation/avplayer/1386726-play https://developer.android.com/reference/android/media/MediaPlayer#start()

PierreEc commented 1 year ago

CR Kot001 CR Swift001

Rule name: Videos and media content should not be preloaded

Tickets existant dans le kanban

Type of consumption: Network requests, Battery drain, CPU/GPU

Sometimes useless or interrompt user Easy implementable

Example:

struct VideoView: View {
 @State var player = AVPlayer(url: ***) 
   var body: some View {
           VideoPlayer(player: player)
               .frame(width: 400, height: 300, alignment: .center)
               .onAppear {
                   player.play()
               }
   }
}
PierreEc commented 1 year ago

Videos and media content should not be preloaded

Platform

OS OS version Langage.
iOS/iPadOS/WatchOS/tvOS - Android - Swift / Kotlin

Main caracteristics

ID Title Category Sub-category
- Environment Optimized API

Severity / Remediation Cost

Severity Remediation Cost
Medium Minor

Rule short description

When you use native player, you can play the player on avent like onAppear/onLoad on apple platform or onDispose onandroid platform

Rule complete description

Text

When you use native player, you can play the player on avent like onAppear/onLoad on apple platform or onDispose on android platform

HTML

<p>When you use native player, you can play the player on avent like onAppear/onLoad on apple platform or onDispose on android platform</p>
<p>
<pre>
struct VideoView: View {
 @State var player = AVPlayer(url: ***) 
   var body: some View {
           VideoPlayer(player: player)
               .frame(width: 400, height: 300, alignment: .center)
               .onAppear {
                   player.play()
               }
   }
}
</pre>

Implementation principle

olegoaer commented 5 months ago

Clever idea. I've been wondering for a while how to detect video autoplay. This bad practice has a big impact.