iis服务器助手广告广告
返回顶部
首页 > 资讯 > 移动开发 >Android实现视频的画中画功能
  • 269
分享到

Android实现视频的画中画功能

2024-04-02 19:04:59 269人浏览 独家记忆
摘要

简介: Android 8.0(api 级别 26)允许以画中画 (PIP) 模式启动 Activity。画中画是一种特殊类型的多窗口模式,最常用于视频播放。使用该模式,用户可以通过

简介: Android 8.0(api 级别 26)允许以画中画 (PIP) 模式启动 Activity。画中画是一种特殊类型的多窗口模式,最常用于视频播放。使用该模式,用户可以通过固定到屏幕一角的小窗口观看视频,同时在应用之间进行导航或浏览主屏幕上的内容。

画中画窗口会显示在屏幕的最上层,位于系统选择的一角。您可以将画中画窗口拖动到其他位置(会自动贴边)。当您点按该窗口时,会看到两个特殊的控件:全屏切换开关(位于窗口的中心)和关闭按钮(右上角的“X”)。

效果图:

1、声明对画中画的支持:

默认情况下,系统不会自动为应用提供画中画支持。如果您想在应用中支持画中画,可以通过将 android:supportsPictureInPicture 设置为 true,在清单中注册视频 Activity。此外,指定您的 Activity 处理布局配置更改,这样一来,在画中画模式转换期间发生布局更改时,您的 Activity 就不会重新启动。


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="Http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.csu.pictureinpicture">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

        <activity
            android:name=".VideoPipActivity"
            android:resizeableActivity="true"
            android:confiGChanges="screenSize|smallestScreenSize|screenLayout|orientation"
            android:supportsPictureInPicture="true" />
        <activity android:name=".MediaSessionPlaybackActivity"
            android:resizeableActivity="true"
            android:configChanges="screenSize|smallestScreenSize|screenLayout|orientation"
            android:supportsPictureInPicture="true">

   <intent-filter>
                <action android:name="android.intent.action.MaiN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            
  </activity>

    </application>

</manifest>

2、将 Activity 切换到画中画模式:

如要进入画中画模式,Activity 必须调用enterPictureInPictureMode() 。



void minimize() {
    if (null == mMovieView) {
        return;
    }

    // Hide the controls in Picture-in-Picture mode.
    mMovieView.hideControls();
    // Calculate the aspect ratio of the PiP screen.
    Rational aspectRatio = new Rational(mMovieView.getWidth(), mMovieView.getHeight());
    mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
    enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
}

3、处理画中画模式下的界面元素

当 Activity 进入或退出画中画模式时,系统会调用 Activity.onPictureInPictureModeChanged() Fragment.onPictureInPictureModeChanged() 。在画中画模式下,Activity 会在一个小窗口中显示。在画中画模式下,用户无法与界面元素互动,并且可能很难看清小界面元素的详细信息。在 Activity 进入画中画模式之前移除其他界面元素,并在 Activity 再次变为全屏时恢复这些元素:


@Override
public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
    super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
    if (isInPictureInPictureMode) {
        // Starts receiving events from action items in PiP mode.
        mReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                if (null == intent || !ACTION_MEDIA_CONTROL.equals(intent.getAction())) {
                    return;
                }

                // This is where we are called back from Picture-in-Picture action items.
                final int controlType = intent.getIntExtra(EXTRA_CONTROL_TYPE, 0);
                switch (controlType) {
                    case CONTROL_TYPE_PLAY:
                        mMovieView.play();
                        break;
                    case CONTROL_TYPE_PAUSE:
                        mMovieView.pause();
                        break;
                    default:
                        break;
                }
            }
        };
        reGISterReceiver(mReceiver, new IntentFilter(ACTION_MEDIA_CONTROL));
    } else {
        // We are out of PiP mode. We can stop receiving events from it.
        unregisterReceiver(mReceiver);
        mReceiver = null;
        // Show the video controls if the video is not playing
        if (null != mMovieView && !mMovieView.isPlaying()) {
            mMovieView.showControls();
        }
    }
}

完整代码:

页面布局文件:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/activity_video"
    android:orientation="vertical"
    tools:context=".VideoPipActivity">

    <com.csu.pictureinpicture.widget.MovieView
        android:id="@+id/movie"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:adjustViewBounds="true"
        android:src="@raw/vid_bigbuckbunny"
        android:title="@string/title_bigbuckbunny"/>

    <ScrollView
        android:id="@+id/scroll"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <LinearLayout
            android:id="@+id/vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/activity_vertical_margin"
            android:paddingEnd="@dimen/activity_horizontal_margin"
            android:paddingStart="@dimen/activity_horizontal_margin"
            android:paddingTop="@dimen/activity_vertical_margin">

            <Button
                android:id="@+id/pip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/enter_picture_in_picture"/>

        </LinearLayout>

    </ScrollView>

</LinearLayout>

Activity文件:


public class VideoPipActivity extends AppCompatActivity {

    
    private static final String ACTION_MEDIA_CONTROL = "media_control";

    
    private static final String EXTRA_CONTROL_TYPE = "control_type";

    
    private static final int REQUEST_PLAY = 1;

    
    private static final int REQUEST_PAUSE = 2;

    
    private static final int REQUEST_INFO = 3;

    
    private static final int CONTROL_TYPE_PLAY = 1;

    
    private static final int CONTROL_TYPE_PAUSE = 2;

    
    private final PictureInPictureParams.Builder mPictureInPictureParamsBuilder =
            new PictureInPictureParams.Builder();

    
    private MovieView mMovieView;

    
    private ScrollView mScrollView;

    
    private BroadcastReceiver mReceiver;

    private String mPlay;
    private String mPause;

    private final View.OnClickListener mOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v.getId() == R.id.pip) {
                minimize();
            }
        }
    };

    
    private MovieView.MovieListener mMovieListener = new MovieView.MovieListener() {
        @Override
        public void onMovieStarted() {
            // We are playing the video now. In PiP mode, we want to show an action item to
            // pause
            // the video.
            updatePictureInPictuReactions(R.drawable.ic_pause_24dp, mPause, CONTROL_TYPE_PAUSE, REQUEST_PAUSE);
        }

        @Override
        public void onMovieStopped() {
            // The video stopped or reached its end. In PiP mode, we want to show an action
            // item to play the video.
            updatePictureInPictureActions(R.drawable.ic_play_arrow_24dp, mPlay, CONTROL_TYPE_PLAY, REQUEST_PLAY);
        }

        @Override
        public void onMovieMinimized() {
            // The MovieView wants us to minimize it. We enter Picture-in-Picture mode now.
            minimize();
        }
    };

    
    void updatePictureInPictureActions(@DrawableRes int iconId, String title, int controlType, int requestCode) {
        final ArrayList<RemoteAction> actions = new ArrayList<>();

        // This is the PendingIntent that is invoked when a user clicks on the item.
        // You need to use distinct request codes for play and pause, or the PendingIntent wont't
        // be properly updated.
        final PendingIntent intent = PendingIntent.getBroadcast(
                VideoPipActivity.this,
                requestCode,
                new Intent(ACTION_MEDIA_CONTROL).putExtra(EXTRA_CONTROL_TYPE, controlType),
                0);
        final Icon icon = Icon.createWithResource(VideoPipActivity.this, iconId);
        actions.add(new RemoteAction(icon, title, title, intent));

        // Another action item. This is a fixed action.
        actions.add(new RemoteAction(
                Icon.createWithResource(VideoPipActivity.this, R.drawable.ic_info_24dp),
                getString(R.string.info),
                getString(R.string.info_description),
                PendingIntent.getActivity(
                        VideoPipActivity.this,
                        REQUEST_INFO,
                        new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.info_uri))), 0)
        ));

        mPictureInPictureParamsBuilder.setActions(actions);

        // This is how you can update action items (or aspect ratio) for Picture-in-Picture mode.
        // Note this call can happen even when the app is not in PiP mode. In that case, the
        // arguments will be used for at the next call of #enterPictureInPictureMode.
        setPictureInPictureParams(mPictureInPictureParamsBuilder.build());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_pip);

        // Prepare string resource for Picture-in-Picture actions.
        mPlay = getString(R.string.play);
        mPause = getString(R.string.pause);

        // View references
        mMovieView = findViewById(R.id.movie);
        mScrollView = findViewById(R.id.scroll);

        // Set up the video; it automatically starts.
        mMovieView.setMovieListener(mMovieListener);
        findViewById(R.id.pip).setOnClickListener(mOnClickListener);
    }

    @Override
    protected void onStop() {
        // On entering Picture-in-Picture mode, onPause is called, but not onStop.
        // For this reason, this is the place where we should pause the video playback.
        mMovieView.pause();
        super.onStop();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (!isInPictureInPictureMode()) {
            // Show the video controls so the video can be easily resumed.
            mMovieView.showControls();
        }
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        adjustFullScreen(newConfig);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            adjustFullScreen(getResources().getConfiguration());
        }
    }

    @Override
    public void onPictureInPictureModeChanged(boolean isInPictureInPictureMode, Configuration newConfig) {
        super.onPictureInPictureModeChanged(isInPictureInPictureMode, newConfig);
        if (isInPictureInPictureMode) {
            // Starts receiving events from action items in PiP mode.
            mReceiver = new BroadcastReceiver() {
                @Override
                public void onReceive(Context context, Intent intent) {
                    if (null == intent || !ACTION_MEDIA_CONTROL.equals(intent.getAction())) {
                        return;
                    }

                    // This is where we are called back from Picture-in-Picture action items.
                    final int controlType = intent.getIntExtra(EXTRA_CONTROL_TYPE, 0);
                    switch (controlType) {
                        case CONTROL_TYPE_PLAY:
                            mMovieView.play();
                            break;
                        case CONTROL_TYPE_PAUSE:
                            mMovieView.pause();
                            break;
                        default:
                            break;
                    }
                }
            };
            registerReceiver(mReceiver, new IntentFilter(ACTION_MEDIA_CONTROL));
        } else {
            // We are out of PiP mode. We can stop receiving events from it.
            unregisterReceiver(mReceiver);
            mReceiver = null;
            // Show the video controls if the video is not playing
            if (null != mMovieView && !mMovieView.isPlaying()) {
                mMovieView.showControls();
            }
        }
    }

    
    void minimize() {
        if (null == mMovieView) {
            return;
        }

        // Hide the controls in Picture-in-Picture mode.
        mMovieView.hideControls();
        // Calculate the aspect ratio of the PiP screen.
        Rational aspectRatio = new Rational(mMovieView.getWidth(), mMovieView.getHeight());
        mPictureInPictureParamsBuilder.setAspectRatio(aspectRatio).build();
        enterPictureInPictureMode(mPictureInPictureParamsBuilder.build());
    }

    
    private void adjustFullScreen(Configuration config) {
        final View decorView = getWindow().getDecorView();
        if (config.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            decorView.setSystemUiVisibility(
                    View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                            | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                            | View.SYSTEM_UI_FLAG_FULLSCREEN
                            | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
            mScrollView.setVisibility(View.GONE);
            mMovieView.setAdjustViewBounds(false);
        } else {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            mScrollView.setVisibility(View.VISIBLE);
            mMovieView.setAdjustViewBounds(true);
        }
    }

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: Android实现视频的画中画功能

本文链接: https://www.lsjlt.com/news/133703.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Android实现视频的画中画功能
    简介: Android 8.0(API 级别 26)允许以画中画 (PIP) 模式启动 Activity。画中画是一种特殊类型的多窗口模式,最常用于视频播放。使用该模式,用户可以通过...
    99+
    2024-04-02
  • Android实现画中画功能(图片)
    简介: Android 8.0(API 级别 26)允许以画中画 (PIP) 模式启动 Activity。画中画是一种特殊类型的多窗口模式,最常用于视频播放。使用该模式,用户可以通过...
    99+
    2024-04-02
  • Android实现简单画中画功能
    Android 8.0推出了PictureInPicture(画中画功能),目前只有在8.0以上的系统上支持。对比IOS,IOS的Picture in Picture 模式是苹果公司...
    99+
    2024-04-02
  • Android实现短视频画心效果
    本文实例为大家分享了Android实现短视频画心效果的具体代码,供大家参考,具体内容如下 布局 主布局 <?xml version="1.0" encoding="...
    99+
    2024-04-02
  • Android 8.0 中如何实现视频通话的画中画模式的示例
    Android 8.0 当中允许 Activiy 以画中画模式展现。这是一种多窗口模式的改进加强,在视频类应用中用处非常大,有了这种模式,就可以在视频通话或者观看直播的过程当中打开另外的应用而不用退出当前视频。更详细的就不再累述了,大家去阅...
    99+
    2023-05-30
    android 画中画 roi
  • Android实现画板功能(一)
    本文实例为大家分享了Android实现画板功能的具体代码,供大家参考,具体内容如下 前言 最近看到了一些Android手写相关的功能,比如说: 钉钉手写签名功能,输入法手写功能,笔记...
    99+
    2024-04-02
  • Android实现画板功能(二)
    本文实例为大家分享了Android实现画板功能的具体代码,讲解使用imageView,bitmap的方式实现画板功能,供大家参考,具体内容如下 前言 在上一篇Android实现画板功...
    99+
    2024-04-02
  • Android实现绘画板功能
    目录实现流程:实现步骤:一、预期效果二、设置横竖屏切换三、确定布局四、自定义滑动条五、绘画区域六、MainActivity实现流程:     ...
    99+
    2024-04-02
  • 如何在Android中实现短视频画心效果
    如何在Android中实现短视频画心效果?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。主布局<xml version="1.0"&nbs...
    99+
    2023-06-15
  • python实现bilibili动画下载视频批量改名功能
    bilib应用 在微软商店中下载哔哩哔哩动画,虽然软件UI古老,但是贵在稳定和支持下载 安装以后搜索自己想要的视频,然后缓存下载 下载后进入下载的路径 视频文件重命名 打开自动...
    99+
    2024-04-02
  • python实现多个视频文件合成画中画效果
    本文实例为大家分享了多个视频文件合成画中画效果的python代码,供大家参考,具体内容如下 Step 1 从视频中分离出音频(MP4->mp3) def separateM...
    99+
    2024-04-02
  • Android如何实现绘画板功能
    这篇文章主要介绍了Android如何实现绘画板功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。实现流程:     &nbs...
    99+
    2023-06-15
  • python如何实现bilibili动画下载视频批量改名功能
    这篇文章主要讲解了“python如何实现bilibili动画下载视频批量改名功能”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“python如何实现bilibili动画下载视频批量改名功能”吧...
    99+
    2023-06-21
  • Android视图动画怎么实现
    本文小编为大家详细介绍“Android视图动画怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“Android视图动画怎么实现”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。介绍视图动画主要有两种:一、Twe...
    99+
    2023-06-04
  • Android GSYVideoPlayer视频播放器功能的实现
    GSYVideoPlayer GSYVideoPlayer官方地址 GSYVideoPlayer 一个基于IJkPlayer的播放器 支持调节声音亮度 边播边缓存 使用了Andro...
    99+
    2024-04-02
  • Android视频录制功能的实现步骤
    官方使用指南请查看Google音频和视频指南 视频录制基本步骤 1.申明权限 <uses-permission android:name="android.permiss...
    99+
    2024-04-02
  • html5如何实现画板画笔功能
    这篇文章主要介绍了html5如何实现画板画笔功能,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。   实现画板画笔功能,效果如下:   1...
    99+
    2024-04-02
  • Android实现绘画板功能的示例分析
    这篇文章主要介绍Android实现绘画板功能的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!实现流程:        一、预期效果&nbs...
    99+
    2023-06-15
  • android怎么实现视频播放功能
    Android可以使用MediaPlayer或ExoPlayer等库来实现视频播放功能。以下是一种常见的实现方法:1. 添加权限和依...
    99+
    2023-08-23
    android
  • Android开发中项目实现一个画板功能
    这期内容当中小编将会给大家带来有关Android开发中项目实现一个画板功能,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。效果图如下XML布局代码<RelativeLayout xmlns:andro...
    99+
    2023-05-31
    android roi
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作