2014年12月15日 星期一

Android-播放串流影音( Play RTSP Vedio )( 一 )

(更新日期 : 20150430)
目前有看到幾種方法
1 . Media Player 直接撥放
2 . 利用HttpClient連結
3 . Vitamio

實驗結果
如果直接使用方法 1 :
常常因為硬體廠商有無對軟體矯正,LG目前可行,星星星、$ony目前都會出現Error ( 100 , 0 )


直接說明 Vitamio
他有 MediaPlayer 功能,但是相較之下,Vitamio設計的 MediaPlayer 功能更為強大。
但請注意若App內有包含Vitamio和MediaPlayer必須明確指向路徑!!!
以下是 Rtsp 範例 :

若程式碼內有 " 請更改為 "

1 . 首先下載 : https://www.vitamio.org/Download/






























2 . Sample的內容非常實用















3 . 若無方向,這整理出一個範例

4 . 找下載好的 Vitamio 裡面的Lib















5 . 建立一個新的專案並且匯入上一步的Lib( 專案要在同層比較不會有錯誤 )

















6 . activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SurfaceView
        android:id="@+id/surface"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

    <TextView
        android:id="@+id/sub1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerInParent="true" />

</RelativeLayout>

7 . MainActivity.java

<pre style="font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; color: #000000; background-color: #eee;font-size: 12px;border: 1px dashed #999999;line-height: 14px;padding: 5px; overflow: auto; width: 100%"><code>package com.example.rstp_sample;

import io.vov.vitamio.LibsChecker;
import io.vov.vitamio.MediaPlayer;
import io.vov.vitamio.MediaPlayer.OnPreparedListener;
import io.vov.vitamio.MediaPlayer.OnTimedTextListener;

import java.io.IOException;

import android.app.Activity;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends Activity implements Callback, OnPreparedListener, OnTimedTextListener {

    SurfaceView splayer;
    SurfaceHolder sholder;
    TextView tv;
    private MediaPlayer mediaPlayer;
    private String path = "http://mirror.aarnet.edu.au/pub/TED-talks/911Mothers_2010W-480p.mp4";
    private boolean first = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!LibsChecker.checkVitamioLibs(this))
            return;
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.sub1);
        splayer = (SurfaceView) findViewById(R.id.surface);
        sholder = splayer.getHolder();
        sholder.setFormat(PixelFormat.RGBA_8888);
        sholder.addCallback(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(first==true){
            first =false;    
        }else {
            relaMediaPlay();
            playVideo();
        }
    }

    private void playVideo() {
        try {
            if (path == "") {
                // Tell the user to provide an audio file URL.
                return;
            }
            mediaPlayer = new MediaPlayer(this);
            mediaPlayer.setDataSource(path);
            mediaPlayer.setDisplay(sholder);
            mediaPlayer.prepareAsync();
            mediaPlayer.setOnPreparedListener(this);

            mediaPlayer.setOnTimedTextListener(this);

            // TODO Auto-generated catch block
        } catch (IllegalArgumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalStateException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SecurityException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        playVideo();
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub

    }

    private void startVPback() {

        mediaPlayer.start();
    }

    @Override
    public void onPrepared(MediaPlayer arg0) {

        // TODO Auto-generated method stub
        startVPback();
        mediaPlayer.addTimedTextSource(Environment.getExternalStorageDirectory() + "/12.srt");
        mediaPlayer.setTimedTextShown(true);
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        relaMediaPlay();
    }

    private void relaMediaPlay() {
        // TODO Auto-generated method stub
        if (mediaPlayer != null) {
            mediaPlayer.release();
            mediaPlayer = null;
        }

    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        relaMediaPlay();

    }

    @Override
    public void onTimedText(String text) {
        // TODO Auto-generated method stub
        tv.setText(text);
    }

    @Override
    public void onTimedTextUpdate(byte[] pixels, int width, int height) {
        // TODO Auto-generated method stub

    }

}

</code></pre>

8 . 重點( AndroidManifest.xml ) :
要記得新增頁面
<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" />

9 . 權限
<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.READ_EXTERNAL_STORAGE" />

10 . 完成






























歡迎轉載,請註明出處。

沒有留言:

張貼留言