跟踪用户观看视频Android Exoplayer的时间

时间:2019-01-26 02:39:24

标签: android

我想在我的应用程序中添加此功能,用户在Exo Player中观看视频多长时间了?谁能告诉我这个代码是否可行。请解决这个问题。

3 个答案:

答案 0 :(得分:1)

在ExoPlayer 2.11中,您可以使用PlaybackStatsListener

要使用PlaybackStatsListener,请通过调用player.addAnalyticsListener(playbackStatsListener)向播放器注册一个实例。

可以使用playbackStatsListener.playbackStats.totalPlayTimeMs来获取总播放时间。

答案 1 :(得分:0)

少量笔记

是的,可以轻松地跟踪在ExoPlayer中播放的视频的运行时间。

您可以做什么

在此示例中,我使用了kotlin和 ExoPlayer v2.9.3

mPlayer.prepare()

// For example you attempted to get the duration immediately
mPlayer.getDuration()

最好的持续时间是监听ExoPlayer的状态变化,尤其是当它准备就绪时,否则,

UNKNOWN_TIME

您将看到错误为.row{ width: 100%; margin-left: auto; margin-right: auto; margin-top: 0; margin-bottom: 0; max-width: 75em; } .coluna{ position: relative; padding-left: 15px; padding-right: 15px; float: left; } .row:before { content: " "; display: table; } .row:after { clear: both; } #conteudo > div > div { padding-top: 35px; padding-bottom: 30px; } .colunas{ position: relative; padding-left: 15px; padding-right: 15px; float: left; width: 860px; } .menu-conteudo { margin-top: -30px; margin-right: -15px; background: #33bd6c; display: block; clear: both; overflow: hidden; margin-bottom: 20px; border-bottom: 1px solid #136e39; } article div.entrada-conteudo{ min-height: 165px; } div .entrada-conteudo p{ color: #595959; font-size: 13px; line-height: 20px; text-align: justify; } .row .row { width: auto; margin-left: -15px; margin-right: -15px; margin-top: 0; margin-bottom: 0; max-width: none; } .images img{ width: 270px; height: 200px; } .coluna article{ border-bottom: 1px solid #33bd6c; margin-bottom: 40px; width: 265px; } #conteudo .data { color: #595959; display: inline-block; font-size: 12px; margin-top: 20px; font-weight: normal; margin-bottom: 15px; } #conteudo h4.titulo { line-height: 20px; margin-top: 0; margin-bottom: 20px; color: #33bd6c; } #conteudo h4.titulo a { color: #33bd6c; font-family: "fs_hackney_webbold",sans-serif; font-size: 18px; } .entrada-conteudo p{ color: #595959; font-size: 13px; line-height: 20px; } .texto{ margin-top: 40px; padding-left: 22px; width: 180px; height: 100px; display: inline-block; position:relative; } .botao{ margin-top: 40px; width: 90px; height: 100px; display: inline-block; position:relative; } input[type="text"]{ background-color: #fff; border: 1px none #cccccc; box-shadow: inset 0 1px 2px rgba(0, 0, 0, 0.2); margin: 0 0 16px 0; padding: 8px; height: 37px; font-size: 14px; width: 100%; box-sizing: border-box; } input[type="submit"]{ text-align: center; border: none; z-index: 2; width: 100%; padding-top: 0; padding-bottom: 0; border-style: solid; border-width: 1px; overflow: hidden; font-size: 14px; height: 37px; line-height: 34px; background-color: #33bd6c; color: #fff; } input[type="submit"]:hover{ background-color: rgb(19,110,57); } .liga{ margin-top: -10px; padding: 10px; font-family: "fs_hackney_webbold",sans-serif; font-size: 14px; margin-left: 22px; display: inline-block; width: 255px; background: #33bd6c; color: #FFF!important; border-bottom: 3px #136e39 solid; margin-bottom: 0px!important; } #border-tabel{ border: 1px #cdcdcd solid; display: inline-block; margin-left: 22px; width: 273px; margin-bottom: 30px; } table#classificacao{ position: relative; width: 100%; } #classificacao{ font-family: "fs_hackney_webbold",sans-serif; font-size: 14px; color: #626262; } #classificacao th{ color: #9b9b9b; } #classificacao tr, #classificacao th{ padding: 10px; line-height: 14px; } #classificacao td{ position: static; white-space: normal; overflow: visible; padding: 10px 12px; border-top: 1px #dddddd solid; } #posicao{ color: #33bd6c; } .bordo-result{ border: 1px #cdcdcd solid; border-top: 0px; padding: 0 10px; background: #FFF; margin-left: 22px; width: 253px; display: inline-block; } .bordo-topo{ padding: 10px; border-top: 1px #dddddd solid; color: #626262; font-size: 14px; text-align: center; } .team{ color: #33bd6c; }的日志。

参考

答案 2 :(得分:0)

万一有人在使用Xamarin时看到此消息。

对于Xamarin.Android,由于我们无法“新建”接口,因此请创建一个新类并继承自IPlayerEventListener

public class CustomExoListener : Java.Lang.Object, IPlayerEventListener
    {
        PlayerView _player;
        public CustomExoListener(PlayerView player)
        {       
            _player = player;     
        }
        public void OnLoadingChanged(bool isLoading) { }
        public void OnPlayerError(ExoPlaybackException error) { }
        public void OnPlayerStateChanged(bool playWhenReady, int playbackState) 
        {
            if (playbackState != PlaybackState.PlaybackPositionUnknown)
            {
                var duration = _player.Player.Duration;
                var position = _player.Player.CurrentPosition;
                var percentageWatched = (100 * position / duration);
                System.Diagnostics.Debug.WriteLine($"Player State: {playbackState} duration: {duration} position: {position} Watched: {percentageWatched}%");
            }
        }
        public void OnPositionDiscontinuity(int reason) { }
        public void OnSeekProcessed() { }
        public void OnIsPlayingChanged(bool isPlaying) { }
        public void OnTimelineChanged(Timeline timeline, int reason) { }
        public void OnTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) { }
    }

然后分配给玩家

var exoPlayerView = new SimpleExoPlayer.Builder(ApplicationContext).Build();
var uri = Android.Net.Uri.Parse(mediaUrl);

IHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(Util.GetUserAgent(ApplicationContext, "ChangeToYourAppName"));
IMediaSource mediaSource = new HlsMediaSource.Factory(httpDataSourceFactory).CreateMediaSource(uri);

// Prepare the player with the source.
exoPlayerView.Prepare(mediaSource);
exoPlayerView.AddListener(new CustomExoListener());

并在继承自IPlayerEventListener的新类的事件中做您需要做的一切