vitamio直播流编程(供学习)

2023-10-30 01:20
文章标签 学习 编程 直播 vitamio

本文主要是介绍vitamio直播流编程(供学习),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

编写测试的RTMP直播流过程

1、在Android Studio中引用Vitamio库的步骤如下:

1. 下载Vitamio bundle第三方库, https://github.com/yixia/VitamioBundle,官方需要授权才可用其中功能。(https://www.vitamio.org 

2. 解压并且在Android Studio上File->Import Module

3. 指定到VitamioBundle路径,选择vitamio文件夹 点击完成

4. 在build.gradle(Module: app)依赖部分添加依赖项目(‘:vitamio’)

5. 打开build.gradle (Module: vitamio) - 改变最小sdk版本为7,我的修改为9,如不报错就行,报错系统会提示,修改相应版本即可。

不要忘记在manifest.xml中添加internet权限:<uses-permission  android:name="android.permission.INTERNET"/>加在<application>之外

6. 完成!

 

1、主Activity界面xml文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fac0ca"
    android:orientation="horizontal"
    android:gravity="center_horizontal"
    >
<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_marginTop="20dp"
    android:layout_marginRight="15dp"><Button
        android:id="@+id/cctv12"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:text="中央新闻台"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:onClick="onclick"/><Button
        android:id="@+id/cctv12g"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="中央新闻高清"
        android:onClick="onclick"/><Button
        android:id="@+id/cctv1"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="中央1台"
        android:onClick="onclick"/><Button
        android:id="@+id/cctv4"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="中央4台"
        android:onClick="onclick"/><Button
        android:id="@+id/cctv5"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="中央5台"
        android:onClick="onclick"/><Button
        android:id="@+id/cctv5g"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="中央5台高清"
        android:onClick="onclick"/><Button
        android:id="@+id/cctv6"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="中央6台"
        android:onClick="onclick"
        />
</LinearLayout><LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:layout_marginTop="20dp"
        android:layout_marginRight="15dp"><Button
        android:id="@+id/cctv6g"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="中央6台高清"
        android:onClick="onclick"/><Button
        android:id="@+id/hntv"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="湖南卫视"
        android:onClick="onclick"/><Button
        android:id="@+id/hntvg"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="湖南卫视高清"
        android:onClick="onclick"/><Button
        android:id="@+id/hktv"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="香港卫视"
        android:onClick="onclick"/><Button
        android:id="@+id/jstvg"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="江苏卫视高清"
        android:onClick="onclick"/><Button
        android:id="@+id/zjtvg"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="浙江卫视高清"
        android:onClick="onclick"/><Button
        android:id="@+id/bttvg"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/button1"
        android:textColor="#fff"
        android:text="兵团卫视"
        android:onClick="onclick"/></LinearLayout>
</LinearLayout>
效果图:(所需资源图自行设计)

2、启动Java代码(包名因环境不同而不同)

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import java.util.HashMap;
import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
import static io.vov.vitamio.MediaPlayer.VIDEOQUALITY_HIGH;

public class MainActivity extends AppCompatActivity {
    private static final String TAG "MainActivity";
    private String path;
    //private HashMap<String, String> options;
    private VideoView mVideoView;
    private Button button;

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

    public void onclick(View view){
        button=(Button) view;
        Intent intent = new Intent();
        intent.setClass(this, mainVidoPlayer.class);


        if(button.getText().toString().equals("中央新闻台"))
           {
            intent.putExtra("path""直播流地址XXXXXXX");//需要自行加入直播流地址,以下类似。
               //System.out.println("传前结果:"+button.getText());
            }
        else if (button.getText().toString().equals("中央新闻高清"))
        {
            intent.putExtra("path","直播流地址XXXXXXX");
        }
        else if (button.getText().toString().equals("中央1台"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("中央4台"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("中央5台"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("中央5台高清"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("中央6台"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("中央6台高清"))
        {
            intent.putExtra("path""直播流地址XXXXXXX");
        }
        else if (button.getText().toString().equals("湖南卫视"))
        {
            intent.putExtra("path""rtmp:直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("湖南卫视高清"))
        {
            intent.putExtra("path""h ");
        }
        else if (button.getText().toString().equals("香港卫视"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("江苏卫视高清"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("浙江卫视高清"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        else if (button.getText().toString().equals("兵团卫视"))
        {
            intent.putExtra("path""直播流地址XXXXXXX"");
        }
        startActivity(intent);



    }

3、播放界面xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" ><io.vov.vitamio.widget.VideoView
        android:id="@+id/surface_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" /><LinearLayout
        android:id="@+id/video_loading"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center_vertical" ><ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" /><TextView
            android:id="@+id/video_loading_perce"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingTop="7.0dip"
            android:text="@string/video_layout_loading"
            android:textColor="#ffffff"
            android:textSize="20.0sp" /></LinearLayout><FrameLayout
        android:id="@+id/operation_volume_brightness"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:background="#00000000"
        android:orientation="horizontal"
        android:padding="0dip"
        android:visibility="invisible" ><ImageView
            android:id="@+id/operation_bg"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:src="@drawable/video_volumn_bg" /><FrameLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom|center_horizontal"
            android:paddingBottom="25dip" ><ImageView
                android:id="@+id/operation_full"
                android:layout_width="94dip"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:src="@drawable/video_num_bg" /><ImageView
                android:id="@+id/operation_percent"
                android:layout_width="0dip"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:scaleType="matrix"
                android:src="@drawable/video_num_front" /></FrameLayout></FrameLayout></RelativeLayout>
4、播放java代码

import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnCompletionListener;
import io.vov.vitamio.MediaPlayer.OnInfoListener;
import io.vov.vitamio.widget.MediaController;
import io.vov.vitamio.widget.VideoView;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.AudioManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Display;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;@SuppressLint("HandlerLeak")
public class mainVidoPlayer extends Activity implements OnCompletionListener, OnInfoListener {private String mPath;private String mTitle;private VideoView mVideoView;private View mVolumeBrightnessLayout;private ImageView mOperationBg;private ImageView mOperationPercent;private AudioManager mAudioManager;/** 声音 */
    private int mMaxVolume;/** 当前声音 */
    private int mVolume = -1;/** 当前亮度 */
    private float mBrightness = -1f;/** 当前缩放模式 */
    //private int mLayout = VideoView.VIDEO_LAYOUT_ZOOM;
    private int mLayout = VideoView.VIDEO_LAYOUT_SCALE;private GestureDetector mGestureDetector;private MediaController mMediaController;private View mLoadingView;@Override
    public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);if (!LibsChecker.checkVitamioLibs(this))return;// ~~~ 获取播放地址和标
        // ~~~ 绑定控件
        setContentView(R.layout.activity_main);mPath = getIntent().getStringExtra("path");mVideoView = (VideoView) findViewById(R.id.surface_view);mVolumeBrightnessLayout = findViewById(R.id.operation_volume_brightness);mOperationBg = (ImageView) findViewById(R.id.operation_bg);mOperationPercent = (ImageView) findViewById(R.id.operation_percent);mLoadingView = findViewById(R.id.video_loading);// ~~~ 绑定事件
        mVideoView.setOnCompletionListener(this);mVideoView.setOnInfoListener(this);// ~~~ 绑定数据
        mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);mMaxVolume = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
/*
        if (mPath.startsWith("http:")){
            mVideoView.setVideoURI(Uri.parse(mPath));
        }
        else{
            mVideoView.setVideoPath( mPath);
        }*/
        mVideoView.setVideoPath(mPath);//修改1多
        //System.out.println("播放后地址:"+mPath);//测试
        //设置显示名称
        mMediaController = new MediaController(this);mMediaController.setFileName(mTitle);mVideoView.setMediaController(mMediaController);mVideoView.requestFocus();mGestureDetector = new GestureDetector(this, new MyGestureListener());setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);if(!mVideoView.isPlaying())//判断是否播放,没有播放就执行播放。
        {mVideoView.start();}}public void onStart(){   super.onStart();if (!mVideoView.isPlaying())mVideoView.start();}@Override
    protected void onPause() {super.onPause();if (mVideoView != null)mVideoView.pause();}@Override
    protected void onResume() {super.onResume();if (mVideoView != null)mVideoView.resume();}@Override
    protected void onDestroy() {super.onDestroy();if (mVideoView != null)mVideoView.stopPlayback();}@Override
    public boolean onTouchEvent(MotionEvent event) {if (mGestureDetector.onTouchEvent(event))return true;// 处理手势结束
        switch (event.getAction() & MotionEvent.ACTION_MASK) {case MotionEvent.ACTION_UP:endGesture();break;}return super.onTouchEvent(event);}/** 手势结束 */
    private void endGesture() {mVolume = -1;mBrightness = -1f;// 隐藏
        mDismissHandler.removeMessages(0);mDismissHandler.sendEmptyMessageDelayed(0, 500);}private class MyGestureListener extends SimpleOnGestureListener {/** 双击 */
        @Override
        public boolean onDoubleTap(MotionEvent e) {if (mLayout == VideoView.VIDEO_LAYOUT_ZOOM)mLayout = VideoView.VIDEO_LAYOUT_ORIGIN;else
                mLayout++;if (mVideoView != null)mVideoView.setVideoLayout(mLayout, 0);return true;}/** 滑动 */
        @SuppressWarnings("deprecation")@Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {float mOldX = e1.getX(), mOldY = e1.getY();int y = (int) e2.getRawY();Display disp = getWindowManager().getDefaultDisplay();int windowWidth = disp.getWidth();int windowHeight = disp.getHeight();if (mOldX > windowWidth * 4.0 / 5)// 右边滑动
                onVolumeSlide((mOldY - y) / windowHeight);else if (mOldX < windowWidth / 5.0)// 左边滑动
                onBrightnessSlide((mOldY - y) / windowHeight);return super.onScroll(e1, e2, distanceX, distanceY);}}/** 定时隐藏 */
    private Handler mDismissHandler = new Handler() {@Override
        public void handleMessage(Message msg) {mVolumeBrightnessLayout.setVisibility(View.GONE);}};/**
     * 滑动改变声音大小
     *
     * @param percent
     */
    private void onVolumeSlide(float percent) {if (mVolume == -1) {mVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);if (mVolume < 0)mVolume = 0;// 显示
            mOperationBg.setImageResource(R.drawable.video_volumn_bg);mVolumeBrightnessLayout.setVisibility(View.VISIBLE);}int index = (int) (percent * mMaxVolume) + mVolume;if (index > mMaxVolume)index = mMaxVolume;else if (index < 0)index = 0;// 变更声音
        mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, index, 0);// 变更进度
        ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();lp.width = findViewById(R.id.operation_full).getLayoutParams().width * index / mMaxVolume;mOperationPercent.setLayoutParams(lp);}/**
     * 滑动改变亮度
     *
     * @param percent
     */
    private void onBrightnessSlide(float percent) {if (mBrightness < 0) {mBrightness = getWindow().getAttributes().screenBrightness;if (mBrightness <= 0.00f)mBrightness = 0.50f;if (mBrightness < 0.01f)mBrightness = 0.01f;// 显示
            mOperationBg.setImageResource(R.drawable.video_brightness_bg);mVolumeBrightnessLayout.setVisibility(View.VISIBLE);}WindowManager.LayoutParams lpa = getWindow().getAttributes();lpa.screenBrightness = mBrightness + percent;if (lpa.screenBrightness > 1.0f)lpa.screenBrightness = 1.0f;else if (lpa.screenBrightness < 0.01f)lpa.screenBrightness = 0.01f;getWindow().setAttributes(lpa);ViewGroup.LayoutParams lp = mOperationPercent.getLayoutParams();lp.width = (int) (findViewById(R.id.operation_full).getLayoutParams().width * lpa.screenBrightness);mOperationPercent.setLayoutParams(lp);}@Override
    public void onConfigurationChanged(Configuration newConfig) {if (mVideoView != null)mVideoView.setVideoLayout(mLayout, 0);super.onConfigurationChanged(newConfig);}@Override
    public void onCompletion(MediaPlayer player) {Log.e("test", "完成播放");}private void stopPlayer() {if (mVideoView != null)mVideoView.pause();}private void startPlayer() {if (mVideoView != null)mVideoView.start();}private boolean isPlaying() {return mVideoView != null && mVideoView.isPlaying();}/** 是否自动恢复播放,用于自动暂停,恢复播放 */
    private boolean needResume;@Override
    public boolean onInfo(MediaPlayer arg0, int arg1, int down_rate) {switch (arg1) {case MediaPlayer.MEDIA_INFO_BUFFERING_START://缓存,暂停播
                if (!isPlaying()) {stopPlayer();needResume = true;}else
                {startPlayer();needResume=true;}mLoadingView.setVisibility(View.VISIBLE);break;case MediaPlayer.MEDIA_INFO_BUFFERING_END://缓存完成,继续播
                if (needResume)startPlayer();mLoadingView.setVisibility(View.GONE);break;case MediaPlayer.MEDIA_INFO_DOWNLOAD_RATE_CHANGED://显示 下载速度
                Log.e("test","download rate:" + down_rate);

                break;}return true;}
}

 

4、配置文件Manifest.xml中加入

<activity android:name="io.vov.vitamio.activity.InitActivity"></activity>

 

 

这篇关于vitamio直播流编程(供学习)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/304664

相关文章

Python异步编程之await与asyncio基本用法详解

《Python异步编程之await与asyncio基本用法详解》在Python中,await和asyncio是异步编程的核心工具,用于高效处理I/O密集型任务(如网络请求、文件读写、数据库操作等),接... 目录一、核心概念二、使用场景三、基本用法1. 定义协程2. 运行协程3. 并发执行多个任务四、关键

AOP编程的基本概念与idea编辑器的配合体验过程

《AOP编程的基本概念与idea编辑器的配合体验过程》文章简要介绍了AOP基础概念,包括Before/Around通知、PointCut切入点、Advice通知体、JoinPoint连接点等,说明它们... 目录BeforeAroundAdvise — 通知PointCut — 切入点Acpect — 切面

Unity新手入门学习殿堂级知识详细讲解(图文)

《Unity新手入门学习殿堂级知识详细讲解(图文)》Unity是一款跨平台游戏引擎,支持2D/3D及VR/AR开发,核心功能模块包括图形、音频、物理等,通过可视化编辑器与脚本扩展实现开发,项目结构含A... 目录入门概述什么是 UnityUnity引擎基础认知编辑器核心操作Unity 编辑器项目模式分类工程

Python学习笔记之getattr和hasattr用法示例详解

《Python学习笔记之getattr和hasattr用法示例详解》在Python中,hasattr()、getattr()和setattr()是一组内置函数,用于对对象的属性进行操作和查询,这篇文章... 目录1.getattr用法详解1.1 基本作用1.2 示例1.3 原理2.hasattr用法详解2.

C#异步编程ConfigureAwait的使用小结

《C#异步编程ConfigureAwait的使用小结》本文介绍了异步编程在GUI和服务器端应用的优势,详细的介绍了async和await的关键作用,通过实例解析了在UI线程正确使用await.Conf... 异步编程是并发的一种形式,它有两大好处:对于面向终端用户的GUI程序,提高了响应能力对于服务器端应

C# async await 异步编程实现机制详解

《C#asyncawait异步编程实现机制详解》async/await是C#5.0引入的语法糖,它基于**状态机(StateMachine)**模式实现,将异步方法转换为编译器生成的状态机类,本... 目录一、async/await 异步编程实现机制1.1 核心概念1.2 编译器转换过程1.3 关键组件解析

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio

重新对Java的类加载器的学习方式

《重新对Java的类加载器的学习方式》:本文主要介绍重新对Java的类加载器的学习方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、介绍1.1、简介1.2、符号引用和直接引用1、符号引用2、直接引用3、符号转直接的过程2、加载流程3、类加载的分类3.1、显示