summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/accessibility/VideoPlayer.java
blob: 8f94b768f67f2e7093d2db8ed103dc184c85dd67 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.settings.accessibility;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.media.MediaPlayer;
import android.view.Surface;
import android.view.TextureView;
import android.view.TextureView.SurfaceTextureListener;

import androidx.annotation.GuardedBy;
import androidx.annotation.RawRes;

/**
 * Plays the video by {@link MediaPlayer} on {@link TextureView}, calls {@link #create(Context, int,
 * TextureView)} to setup the listener for TextureView and start to play the video. Once this player
 * is no longer used, call {@link #release()} so that MediaPlayer object can be released.
 */
public class VideoPlayer implements SurfaceTextureListener {
    private final Context context;
    private final Object mediaPlayerLock = new Object();
    // Media player object can't be used after it has been released, so it will be set to null. But
    // VideoPlayer is asynchronized, media player object might be paused or resumed again before
    // released media player is set to null. Therefore, lock mediaPlayer and mediaPlayerState by
    // mediaPlayerLock keep their states consistent.
    @GuardedBy("mediaPlayerLock")
    private MediaPlayer mediaPlayer;
    @GuardedBy("mediaPlayerLock")
    private State mediaPlayerState = State.NONE;
    @RawRes
    private final int videoRes;
    private Surface animationSurface;


    /**
     * Creates a {@link MediaPlayer} for a given resource id and starts playback when the surface
     * for
     * a given {@link TextureView} is ready.
     */
    public static VideoPlayer create(Context context, @RawRes int videoRes,
            TextureView textureView) {
        return new VideoPlayer(context, videoRes, textureView);
    }

    private VideoPlayer(Context context, @RawRes int videoRes, TextureView textureView) {
        this.context = context;
        this.videoRes = videoRes;
        textureView.setSurfaceTextureListener(this);
    }

    public void pause() {
        synchronized (mediaPlayerLock) {
            if (mediaPlayerState == State.STARTED) {
                mediaPlayerState = State.PAUSED;
                mediaPlayer.pause();
            }
        }
    }

    public void resume() {
        synchronized (mediaPlayerLock) {
            if (mediaPlayerState == State.PAUSED) {
                mediaPlayer.start();
                mediaPlayerState = State.STARTED;
            }
        }
    }

    /** Release media player when it's no longer needed. */
    public void release() {
        synchronized (mediaPlayerLock) {
            if (mediaPlayerState != State.NONE && mediaPlayerState != State.END) {
                mediaPlayerState = State.END;
                mediaPlayer.release();
                mediaPlayer = null;
            }
        }
        if (animationSurface != null) {
            animationSurface.release();
            animationSurface = null;
        }
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        animationSurface = new Surface(surface);
        synchronized (mediaPlayerLock) {
            mediaPlayer = MediaPlayer.create(context, videoRes);
            mediaPlayerState = State.PREPARED;
            mediaPlayer.setSurface(animationSurface);
            mediaPlayer.setLooping(true);
            mediaPlayer.start();
            mediaPlayerState = State.STARTED;
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        release();
        return false;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
    }

    /**
     * The state of MediaPlayer object. Refer to
     * https://developer.android.com/reference/android/media/MediaPlayer#StateDiagram.
     */
    public enum State {
        /** MediaPlayer objects has not be created. */
        NONE,
        /** MediaPlayer objects is created by create() method. */
        PREPARED,
        /** MediaPlayer is started. It can be paused by pause() method. */
        STARTED,
        /** MediaPlayer object is paused. Calling start() to resume it. */
        PAUSED,
        /**
         * MediaPlayer object is stopped and cannot be started until calling prepare() or
         * prepareAsync()
         * methods.
         */
        STOPPED,
        /** MediaPlayer object is released. It cannot be used again. */
        END
    }
}