Proyectos de Subversion Android Microlearning

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
package com.cesams.twogetskills.activity;
2
 
3
import androidx.appcompat.app.AppCompatActivity;
4
 
5
import android.content.Intent;
6
import android.net.Uri;
7
import android.os.Bundle;
8
import android.util.Log;
9
import android.view.View;
10
import android.widget.ProgressBar;
11
 
12
import com.cesams.twogetskills.Constants;
13
import com.cesams.twogetskills.R;
14
import com.cesams.twogetskills.library.MD5;
15
import com.google.android.exoplayer2.ExoPlayer;
16
import com.google.android.exoplayer2.MediaItem;
17
import com.google.android.exoplayer2.Player;
18
import com.google.android.exoplayer2.SimpleExoPlayer;
19
import com.google.android.exoplayer2.source.MediaSource;
20
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
21
import com.google.android.exoplayer2.ui.PlayerView;
22
import com.google.android.exoplayer2.upstream.DataSource;
23
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
24
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
25
import com.google.android.exoplayer2.util.Util;
26
 
27
import java.util.Calendar;
28
import java.util.Random;
29
import java.util.TimeZone;
30
 
31
public class VideoAudioActivity extends AppCompatActivity {
32
 
33
    private static final String TAG = "C2GS - VideoßActivity";
34
    private SimpleExoPlayer simpleExoPlayer;
35
    private PlayerView playerView;
36
    private ProgressBar progressBar;
37
    private long playbackPosition;
38
    private String videoAudioUrl;
39
    private String deviceId;
40
    private String password;
41
    private boolean durationSet = false;
42
    private long realDurationMillis = 0;
43
 
44
    @Override
45
    protected void onCreate(Bundle savedInstanceState) {
46
        super.onCreate(savedInstanceState);
47
        setContentView(R.layout.activity_video_audio);
48
 
49
        videoAudioUrl = getIntent().getStringExtra("videoAudioUrl");
50
        deviceId = getIntent().getStringExtra("deviceId");
51
        password = getIntent().getStringExtra("password");
52
 
53
        playerView = (PlayerView) findViewById(R.id.activity_video_audio_player);
54
        progressBar = (ProgressBar) findViewById(R.id.activity_video_audio_progress_bar);
55
 
56
        try {
57
 
58
        } catch (Exception e) {
59
            Log.e("VideoAudioActivity", " exoplayer error " + e.toString());
60
        }
61
 
62
 
63
    }
64
 
65
    public void onStart()
66
    {
67
        super.onStart();
68
        initializePlayer();
69
    }
70
 
71
    public void onStop()
72
    {
73
        super.onStop();
74
        releasePlayer();
75
    }
76
 
77
    @Override
78
    public void onBackPressed() {
79
 
80
        try {
81
 
82
            long currentPosition = simpleExoPlayer.getCurrentPosition();
83
            long duration = simpleExoPlayer.getDuration();
84
            long dif = duration - currentPosition;
85
            //10 seg x 1000 milliseconds
86
            if(dif <= 10000 ) {
87
                Intent intent = new Intent();
88
                intent.putExtra("completed", true);
89
                intent.putExtra("requestCode", Constants.REQUEST_CODE_AUDIO_VIDEO);
90
                setResult(RESULT_OK, intent);
91
                finish();
92
            }
93
        } catch(Exception e) {
94
 
95
        }
96
 
97
        setResult(RESULT_CANCELED);
98
        finish();
99
    }
100
 
101
    public void initializePlayer() {
102
        simpleExoPlayer = new SimpleExoPlayer.Builder(this).build();
103
        preparePlayer();
104
        playerView.setPlayer(simpleExoPlayer);
105
 
106
        simpleExoPlayer.setPlayWhenReady(true);
107
        simpleExoPlayer.seekTo(0, playbackPosition);
108
        simpleExoPlayer.addListener(new Player.EventListener() {
109
 
110
 
111
            @Override
112
            public void onPlaybackStateChanged(int state) {
113
                if (state == ExoPlayer.STATE_READY && !durationSet) {
114
                    realDurationMillis = simpleExoPlayer.getDuration();
115
                    durationSet = true;
116
                } else if(state == ExoPlayer.STATE_IDLE) {
117
                    playerView.setKeepScreenOn(false);
118
 
119
 
120
                } else  if(state == ExoPlayer.STATE_ENDED) {
121
                    playerView.setKeepScreenOn(false);
122
 
123
                    Intent intent = new Intent();
124
                    intent.putExtra("completed", true);
125
                    intent.putExtra("requestCode", Constants.REQUEST_CODE_AUDIO_VIDEO);
126
                    setResult(RESULT_OK, intent);
127
                    finish();
128
                } else {
129
                    playerView.setKeepScreenOn(true);
130
                }
131
 
132
            }
133
 
134
            @Override
135
            public void onIsLoadingChanged(boolean isLoading) {
136
                if (isLoading){
137
                    progressBar.setVisibility(View.VISIBLE);
138
                } else {
139
                    progressBar.setVisibility(View.INVISIBLE);
140
                }
141
            }
142
        });
143
    }
144
 
145
    public void preparePlayer()
146
    {
147
        TimeZone timeZone = TimeZone.getTimeZone("UTC");
148
        Calendar calendar = Calendar.getInstance(timeZone);
149
        TimeZone tz = calendar.getTimeZone();
150
        int created =  (int) (calendar.getTimeInMillis() / 1000);
151
 
152
        Random random = new Random(created);
153
        int rand = 1000 + random.nextInt(8999);
154
 
155
 
156
        Log.d(TAG, "token = " + deviceId);
157
        Log.d(TAG, "created = " + created);
158
        Log.d(TAG, "rand = " + rand);
159
        Log.d(TAG, "calc = " + password + ':' +  created + ':' + rand);
160
 
161
        String secret = MD5.generar(password + ':' +  created + ':' + rand);
162
 
163
 
164
        try {
165
            DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(Util.getUserAgent(this, this.getString(R.string.app_name)), null);
166
            httpDataSourceFactory.getDefaultRequestProperties().set(Constants.HTTP_HEADER_SECURITY_TOKEN, deviceId);
167
            httpDataSourceFactory.getDefaultRequestProperties().set(Constants.HTTP_HEADER_SECURITY_SECRET, secret);
168
            httpDataSourceFactory.getDefaultRequestProperties().set(Constants.HTTP_HEADER_SECURITY_CREATED, String.valueOf(created));
169
            httpDataSourceFactory.getDefaultRequestProperties().set(Constants.HTTP_HEADER_SECURITY_RAND, String.valueOf(rand));
170
 
171
            DataSource.Factory dataSourceFactory = new DefaultDataSourceFactory(this, null, httpDataSourceFactory);
172
 
173
 
174
            // Create a progressive media source pointing to a stream uri.
175
            Log.d(TAG, "video file = " + videoAudioUrl);
176
            MediaSource mediaSource = new ProgressiveMediaSource.Factory(dataSourceFactory).createMediaSource(MediaItem.fromUri(Uri.parse(videoAudioUrl)));
177
 
178
            simpleExoPlayer.setMediaSource(mediaSource);
179
 
180
 
181
        } catch (Exception e) {
182
            Log.e("MainAcvtivity", " exoplayer error " + e.toString());
183
        }
184
 
185
    }
186
 
187
    public void releasePlayer() {
188
        playbackPosition = simpleExoPlayer.getCurrentPosition();
189
        simpleExoPlayer.release();
190
    }
191
 
192
 
193
}