Search in sources :

Example 1 with SubtitleController

use of android.media.SubtitleController in project android_frameworks_base by ResurrectionRemix.

the class VideoView method openVideo.

private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    try {
        mMediaPlayer = new MediaPlayer();
        // TODO: create SubtitleController in MediaPlayer, but we need
        // a context for the subtitle renderers
        final Context context = getContext();
        final SubtitleController controller = new SubtitleController(context, mMediaPlayer.getMediaTimeProvider(), mMediaPlayer);
        controller.registerRenderer(new WebVttRenderer(context));
        controller.registerRenderer(new TtmlRenderer(context));
        controller.registerRenderer(new Cea708CaptionRenderer(context));
        controller.registerRenderer(new ClosedCaptionRenderer(context));
        mMediaPlayer.setSubtitleAnchor(controller, this);
        if (mAudioSession != 0) {
            mMediaPlayer.setAudioSessionId(mAudioSession);
        } else {
            mAudioSession = mMediaPlayer.getAudioSessionId();
        }
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        for (Pair<InputStream, MediaFormat> pending : mPendingSubtitleTracks) {
            try {
                mMediaPlayer.addSubtitleSource(pending.first, pending.second);
            } catch (IllegalStateException e) {
                mInfoListener.onInfo(mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0);
            }
        }
        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } finally {
        mPendingSubtitleTracks.clear();
    }
}
Also used : Context(android.content.Context) MediaFormat(android.media.MediaFormat) InputStream(java.io.InputStream) TtmlRenderer(android.media.TtmlRenderer) IOException(java.io.IOException) WebVttRenderer(android.media.WebVttRenderer) AudioManager(android.media.AudioManager) SubtitleController(android.media.SubtitleController) Cea708CaptionRenderer(android.media.Cea708CaptionRenderer) ClosedCaptionRenderer(android.media.ClosedCaptionRenderer) MediaPlayer(android.media.MediaPlayer)

Example 2 with SubtitleController

use of android.media.SubtitleController in project android_frameworks_base by ResurrectionRemix.

the class MediaPlayer method setSubtitleAnchor.

/**
     * The private version of setSubtitleAnchor is used internally to set mSubtitleController if
     * necessary when clients don't provide their own SubtitleControllers using the public version
     * {@link #setSubtitleAnchor(SubtitleController, Anchor)} (e.g. {@link VideoView} provides one).
     */
private synchronized void setSubtitleAnchor() {
    if (mSubtitleController == null) {
        final HandlerThread thread = new HandlerThread("SetSubtitleAnchorThread");
        thread.start();
        Handler handler = new Handler(thread.getLooper());
        handler.post(new Runnable() {

            @Override
            public void run() {
                Context context = ActivityThread.currentApplication();
                mSubtitleController = new SubtitleController(context, mTimeProvider, MediaPlayer.this);
                mSubtitleController.setAnchor(new Anchor() {

                    @Override
                    public void setSubtitleWidget(RenderingWidget subtitleWidget) {
                    }

                    @Override
                    public Looper getSubtitleLooper() {
                        return Looper.getMainLooper();
                    }
                });
                thread.getLooper().quitSafely();
            }
        });
        try {
            thread.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            Log.w(TAG, "failed to join SetSubtitleAnchorThread");
        }
    }
}
Also used : Context(android.content.Context) SubtitleController(android.media.SubtitleController) Anchor(android.media.SubtitleController.Anchor) HandlerThread(android.os.HandlerThread) Runnable(java.lang.Runnable) Handler(android.os.Handler) RenderingWidget(android.media.SubtitleTrack.RenderingWidget)

Example 3 with SubtitleController

use of android.media.SubtitleController in project android_frameworks_base by DirtyUnicorns.

the class MediaPlayer method setSubtitleAnchor.

/**
     * The private version of setSubtitleAnchor is used internally to set mSubtitleController if
     * necessary when clients don't provide their own SubtitleControllers using the public version
     * {@link #setSubtitleAnchor(SubtitleController, Anchor)} (e.g. {@link VideoView} provides one).
     */
private synchronized void setSubtitleAnchor() {
    if (mSubtitleController == null) {
        final HandlerThread thread = new HandlerThread("SetSubtitleAnchorThread");
        thread.start();
        Handler handler = new Handler(thread.getLooper());
        handler.post(new Runnable() {

            @Override
            public void run() {
                Context context = ActivityThread.currentApplication();
                mSubtitleController = new SubtitleController(context, mTimeProvider, MediaPlayer.this);
                mSubtitleController.setAnchor(new Anchor() {

                    @Override
                    public void setSubtitleWidget(RenderingWidget subtitleWidget) {
                    }

                    @Override
                    public Looper getSubtitleLooper() {
                        return Looper.getMainLooper();
                    }
                });
                thread.getLooper().quitSafely();
            }
        });
        try {
            thread.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            Log.w(TAG, "failed to join SetSubtitleAnchorThread");
        }
    }
}
Also used : Context(android.content.Context) SubtitleController(android.media.SubtitleController) Anchor(android.media.SubtitleController.Anchor) HandlerThread(android.os.HandlerThread) Runnable(java.lang.Runnable) Handler(android.os.Handler) RenderingWidget(android.media.SubtitleTrack.RenderingWidget)

Example 4 with SubtitleController

use of android.media.SubtitleController in project android_frameworks_base by crdroidandroid.

the class MediaPlayer method setSubtitleAnchor.

/**
     * The private version of setSubtitleAnchor is used internally to set mSubtitleController if
     * necessary when clients don't provide their own SubtitleControllers using the public version
     * {@link #setSubtitleAnchor(SubtitleController, Anchor)} (e.g. {@link VideoView} provides one).
     */
private synchronized void setSubtitleAnchor() {
    if (mSubtitleController == null) {
        final HandlerThread thread = new HandlerThread("SetSubtitleAnchorThread");
        thread.start();
        Handler handler = new Handler(thread.getLooper());
        handler.post(new Runnable() {

            @Override
            public void run() {
                Context context = ActivityThread.currentApplication();
                mSubtitleController = new SubtitleController(context, mTimeProvider, MediaPlayer.this);
                mSubtitleController.setAnchor(new Anchor() {

                    @Override
                    public void setSubtitleWidget(RenderingWidget subtitleWidget) {
                    }

                    @Override
                    public Looper getSubtitleLooper() {
                        return Looper.getMainLooper();
                    }
                });
                thread.getLooper().quitSafely();
            }
        });
        try {
            thread.join();
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            Log.w(TAG, "failed to join SetSubtitleAnchorThread");
        }
    }
}
Also used : Context(android.content.Context) SubtitleController(android.media.SubtitleController) Anchor(android.media.SubtitleController.Anchor) HandlerThread(android.os.HandlerThread) Runnable(java.lang.Runnable) Handler(android.os.Handler) RenderingWidget(android.media.SubtitleTrack.RenderingWidget)

Example 5 with SubtitleController

use of android.media.SubtitleController in project android_frameworks_base by crdroidandroid.

the class VideoView method openVideo.

private void openVideo() {
    if (mUri == null || mSurfaceHolder == null) {
        // not ready for playback just yet, will try again later
        return;
    }
    // we shouldn't clear the target state, because somebody might have
    // called start() previously
    release(false);
    AudioManager am = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
    am.requestAudioFocus(null, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
    try {
        mMediaPlayer = new MediaPlayer();
        // TODO: create SubtitleController in MediaPlayer, but we need
        // a context for the subtitle renderers
        final Context context = getContext();
        final SubtitleController controller = new SubtitleController(context, mMediaPlayer.getMediaTimeProvider(), mMediaPlayer);
        controller.registerRenderer(new WebVttRenderer(context));
        controller.registerRenderer(new TtmlRenderer(context));
        controller.registerRenderer(new Cea708CaptionRenderer(context));
        controller.registerRenderer(new ClosedCaptionRenderer(context));
        mMediaPlayer.setSubtitleAnchor(controller, this);
        if (mAudioSession != 0) {
            mMediaPlayer.setAudioSessionId(mAudioSession);
        } else {
            mAudioSession = mMediaPlayer.getAudioSessionId();
        }
        mMediaPlayer.setOnPreparedListener(mPreparedListener);
        mMediaPlayer.setOnVideoSizeChangedListener(mSizeChangedListener);
        mMediaPlayer.setOnCompletionListener(mCompletionListener);
        mMediaPlayer.setOnErrorListener(mErrorListener);
        mMediaPlayer.setOnInfoListener(mInfoListener);
        mMediaPlayer.setOnBufferingUpdateListener(mBufferingUpdateListener);
        mCurrentBufferPercentage = 0;
        mMediaPlayer.setDataSource(mContext, mUri, mHeaders);
        mMediaPlayer.setDisplay(mSurfaceHolder);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setScreenOnWhilePlaying(true);
        mMediaPlayer.prepareAsync();
        for (Pair<InputStream, MediaFormat> pending : mPendingSubtitleTracks) {
            try {
                mMediaPlayer.addSubtitleSource(pending.first, pending.second);
            } catch (IllegalStateException e) {
                mInfoListener.onInfo(mMediaPlayer, MediaPlayer.MEDIA_INFO_UNSUPPORTED_SUBTITLE, 0);
            }
        }
        // we don't set the target state here either, but preserve the
        // target state that was there before.
        mCurrentState = STATE_PREPARING;
        attachMediaController();
    } catch (IOException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } catch (IllegalArgumentException ex) {
        Log.w(TAG, "Unable to open content: " + mUri, ex);
        mCurrentState = STATE_ERROR;
        mTargetState = STATE_ERROR;
        mErrorListener.onError(mMediaPlayer, MediaPlayer.MEDIA_ERROR_UNKNOWN, 0);
        return;
    } finally {
        mPendingSubtitleTracks.clear();
    }
}
Also used : Context(android.content.Context) MediaFormat(android.media.MediaFormat) InputStream(java.io.InputStream) TtmlRenderer(android.media.TtmlRenderer) IOException(java.io.IOException) WebVttRenderer(android.media.WebVttRenderer) AudioManager(android.media.AudioManager) SubtitleController(android.media.SubtitleController) Cea708CaptionRenderer(android.media.Cea708CaptionRenderer) ClosedCaptionRenderer(android.media.ClosedCaptionRenderer) MediaPlayer(android.media.MediaPlayer)

Aggregations

Context (android.content.Context)10 SubtitleController (android.media.SubtitleController)10 AudioManager (android.media.AudioManager)5 Cea708CaptionRenderer (android.media.Cea708CaptionRenderer)5 ClosedCaptionRenderer (android.media.ClosedCaptionRenderer)5 MediaFormat (android.media.MediaFormat)5 MediaPlayer (android.media.MediaPlayer)5 Anchor (android.media.SubtitleController.Anchor)5 RenderingWidget (android.media.SubtitleTrack.RenderingWidget)5 TtmlRenderer (android.media.TtmlRenderer)5 WebVttRenderer (android.media.WebVttRenderer)5 Handler (android.os.Handler)5 HandlerThread (android.os.HandlerThread)5 IOException (java.io.IOException)5 InputStream (java.io.InputStream)5 Runnable (java.lang.Runnable)5