Android Advertising Video Playback & caching scheme

Video-based advertising on Android-powered device, such as: subway advertising screen, self-service advertising on the machine, take a number machine advertising.

The last two days made the system a machine called the number the end, an internal processing loop play video ads, and thus have Cipian Summary

The first to understand some of the characteristics of a video ad

First, the network video caching

Second, the repeat function

Third, dynamically updated video ad

Fourth, timeliness guarantee

First, the network video caching

Every player obviously not have to load the network, consuming too much traffic, even if it is under the WIFI case, also needs to be cached.

Here you can use third-party frameworks through AndroidVideoCache

1, adding a dependency

implementation 'com.danikula:videocache:2.7.0

2, initialize the proxy configuration

public static HttpProxyCacheServer proxy; //視頻緩存代理

//創建视频播放緩存代理

proxy = new HttpProxyCacheServer.Builder(this)

        .maxCacheSize(1024 * 1024 * 1024) //1Gb 緩存

        .maxCacheFilesCount(5)//最大緩存5個視頻

        .build();

Second, the loop processing according to the number of recursive calls of a plurality of video, using modulo manner

Core code

   public void playVideoOne() {

        int urlSize = urlList.size();

        index = index % urlSize;

        MediaController mediaController = new MediaController(getThis());

        //缓存起来这个 url 视频 提供缓存文件的名称默认情况下,AndroidVideoCache使用视频网址的MD5作为文件名

//        String proxyUrl = App.proxy.getProxyUrl("[http://jzvd.nathen.cn/63f3f73712544394be981d9e4f56b612/69c5767bb9e54156b5b60a1b6edeb3b5-5287d2089db37e62345123a1be272f8b.mp4](http://jzvd.nathen.cn/63f3f73712544394be981d9e4f56b612/69c5767bb9e54156b5b60a1b6edeb3b5-5287d2089db37e62345123a1be272f8b.mp4)");

        String proxyUrl = App.proxy.getProxyUrl(urlList.get(index));

        videoView.setMediaController(mediaController);//如果设置Controller 点击时会有可交互模式,暂停快进或快退;不设置的话更适用于广告,不可交互,只进行展示

        videoView.setVideoPath(proxyUrl); //為videoview設置播放路徑

        videoView.start();

        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {

            @Override

            public void onCompletion(MediaPlayer mPlayer) {

//                mPlayer.start();

//                //循环播放

//                mPlayer.setLooping(true);

                index++;

                playVideoOne();// 監聽視頻一的播放完成事件,播放完畢就播放視頻二 递归

            }
            //在播放错误的监听回调中也最好做上面监听内的 操作,并且还要注意 在 onPause() 和 onResume() 回调中处理视频的停止和播放!

        });

    }

TIps:

1, if a single video loop can be
  videoView.setOnPreparedListener();

  videoView.setOnCompletionListener();

//这两个方法回调中调用 ,均可达到循环播放单一视频的效果,建议写在 onCompletion 之后

mPlayer.start();
//循环播放
mPlayer.setLooping(true);
2, broadcast network address Api
videoView.setVideoURI(Uri.parse("xxx"));//也可以达到播放网络视频的效果,

因为videoView.setVideoPath 内部调用的就是 videoView.setVideoURI
3, .xml use VideoView show
<VideoView

    android:id="@+id/videoView"

    android:layout_width="match_parent"

    android:layout_height="match_parent" />
4, abnormal playback time of treatment VideoView
videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        videoView.stopPlayback(); //播放異常,則停止播放,防止彈窗使界面阻塞
        return true;
    }
});
5, on ViewView pause and resume from the last position player
在OnPause你可以得到当前的位置。

stopLength=mVideoView.getCurrentPosition();

mVideoView.pause() 

在OnResume()中使用。

mVideoView.seekTo(stopLength);

mVideoView.start()

If set to ViedeView setVisibility GONE hidden, use the above manner will resume play from the beginning!

Scenes:

According to the notification a remote server, when the video is being played on the ad unit switches to show a notification page cover playing video area.

solution:

You can use overlapping layout "FrameLayout" approach, ViedoView on the bottom layer, has been playing. Dynamic control VideoView above notice page hide show. VieoView do not need to be directly pause and resume the operation. Of course, you can also pause and then resume playback processing. I remember not allowed to set ViedeView hide!

6, calls the method jumps are not allowed to issue seekTo

Then execute VideoView.start listener methods in the finished seekTo ();

// 设置 VideoView 的 OnPrepared 监听,拿到 MediaPlayer 对象。
videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
            //设置 MediaPlayer 的 OnSeekComplete 监听
                mp.setOnSeekCompleteListener(new MediaPlayer.OnSeekCompleteListener() {
                    @Override
                    public void onSeekComplete(MediaPlayer mp) {
                    // seekTo 方法完成时的回调
                        if(isPause){
                            videoView.start();
                            isPause = false;
                        }
                    }
                });
            }
        });

Refer https://www.jianshu.com/p/f51b2febcfd2

7, video switching back into the page after the replay process again
  /**
     * 在onResume()方法控制视频继续播放
     */
    @Override
    protected void onResume() {
        super.onResume();

        if (videoView != null) {
            videoView.seekTo(videoCurrentPosition);
            videoView.start();
        }
    }

    /**
     * onPause() 控制视频暂停播放
     */
    @Override
    protected void onPause() {
        super.onPause();

        if (videoView != null) {
            videoCurrentPosition = videoView.getCurrentPosition();
            videoView.pause();
        }
    }

Third, dynamically updated ad

There are many ways such as:

1, receive push requests time to re-do

2, the timing of the polling request or a specified time-date, in order to ensure the timeliness of the link address may be determined later date extraction process

Fourth, timeliness guarantee

Ads need to regularly update, we can achieve real-time updates through the dynamic cache content url, as for the frequency of updates, to see how the stitching url.

By day updates:

If it is to update the ads by date, you can be in the picture, behind the video url add "date", so, ensures that the url changing daily, and caching framework will pull in the day is first loaded data back to load data directly from the local cache. And before the cached content will be automatically cleaned up.

By period update:

If it is according to different times of the day to replace the ad plays, you should start with the server pulls what period of time, and then in the period between the current time, the beginning of the period after the url spliced ​​or end time can be.

By date range update:

If the date is updated in accordance with the span, such as 2017/01/01 ~ 2017/02/03 play a few video. In fact, this is only time to play big concept only, the same token, we start with the server queries the current date is between the periods which video player, and then splice or termination date to start after the url.

By date + time update:

Comprehensive above date range, time of day interval to play different ads: splicing to terminate the termination date + time period.

Live Update:
If you want to ensure that each player is new and can be spliced random number.

Timeliness guarantee reference http://www.cnblogs.com/ygj0930/p/7742996.html

Published 26 original articles · won praise 19 · views 40000 +

Guess you like

Origin blog.csdn.net/a23006239/article/details/80693949
Recommended