android中直播接受的一方

在观看的一方使用的是Vitamio

下载Vitamiosdk,打入打包成lib

新建工程,导入raw,和这个lib

权限

name="android.permission.WAKE_LOCK" />
 name="android.permission.INTERNET" />
 name="android.permission.ACCESS_NETWORK_STATE" />
 name="android.permission.WRITE_EXTERNAL_STORAGE" />

添加activity

 <activity
            android:name="io.vov.vitamio.activity.InitActivity"
            android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
            android:launchMode="singleTop"
            android:theme="@android:style/Theme.NoTitleBar"
            android:windowSoftInputMode="stateAlwaysHidden" />

初始化


Vitamio.initialize(this);

使用方式

mediaPlayer = new MediaPlayer(this);
try {
 mediaPlayer.setDataSource(url);
 mediaPlayer.setDisplay(holder);
 mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
 @Override
 public void onPrepared(MediaPlayer mp) {
 mediaPlayer.start();
 }
 });
mediaPlayer.prepareAsync();
mediaPlayer.setBufferSize(1024 * 1024 * 2);
} catch (IOException e) {
e.printStackTrace();
}

一个案例分析

android中直播接受的一方_第1张图片

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback, View.OnClickListener {


    //String url = "rtmp://zhibo2.whunf.com.cn/xykj/push";
    //下面是一个香港卫视的地址,这里一般是需要放自己推流的地址
String url = "rtmp://live.hkstv.hk.lxdns.com/live/hks";
  //  String url = "http://live121.cnlive.com:1935/live/shijiexiaojie/playlist.m3u8";
    SurfaceView sv;
    ImageView iv;

    MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sv = (SurfaceView) findViewById(R.id.sv);
        iv = (ImageView) findViewById(R.id.iv);
        sv.getHolder().addCallback(this);
        sv.setOnClickListener(this);
        sv.getHolder().setFormat(PixelFormat.RGBA_8888);
    }


    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        mediaPlayer = new MediaPlayer(this);
        try {
            mediaPlayer.setDataSource(url);//设置资源
            mediaPlayer.setDisplay(holder);//设置屏幕
            //设置预加载的监听
            mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mp) {
                    mediaPlayer.start();
                }
            });
            mediaPlayer.prepareAsync();//设置预加载

            mediaPlayer.setBufferSize(1024 * 1024 * 2);//缓存字节
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying())
                mediaPlayer.stop();
            mediaPlayer.release();
            mediaPlayer = null;
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            iv.setVisibility(View.GONE);
        }
    };

    @Override
    public void onClick(View v) {
        //暂停
        if (mediaPlayer != null) {
            if (mediaPlayer.isPlaying()) {
                //暂停
                iv.setVisibility(View.VISIBLE);
                iv.setImageResource(R.mipmap.pause);
                mediaPlayer.pause();
            } else {
                iv.setImageResource(R.mipmap.play);
                mediaPlayer.start();
                handler.sendEmptyMessageDelayed(1, 2000);
            }
        }
    }
}

MyApp

public class MyApp extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        Vitamio.initialize(this);
    }
}

xml


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context="com.example.xwei.zhibo.MainActivity">
<SurfaceView
    android:layout_width="match_parent"
    android:id="@+id/sv"
    android:layout_height="match_parent" />
    <ImageView
        android:id="@+id/iv"
        android:visibility="gone"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:layout_centerInParent="true"
        android:src="@mipmap/pause" />
RelativeLayout>

manifest


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.xwei.zhibo">

    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:name=".MyApp"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.LAUNCHER" />
            intent-filter>
        activity>

        <activity
            android:name="io.vov.vitamio.activity.InitActivity"
            android:configChanges="orientation|screenSize|smallestScreenSize|keyboard|keyboardHidden|navigation"
            android:launchMode="singleTop"
            android:theme="@android:style/Theme.NoTitleBar"
            android:windowSoftInputMode="stateAlwaysHidden" />


    application>

manifest>

你可能感兴趣的:(工具)