如何在Xamarin Android

时间:2017-11-22 11:10:24

标签: xamarin.android android-mediaplayer rtsp

public class MainActivity : Activity, MediaPlayer.IOnPreparedListener,ISurfaceHolderCallback
    {

   // rtsp://admin:Idcube@123@idcube-noida-dlink.dlinkddns.com:554/Streaming/Channels/101
    public static string userName = "admin";
    public static string password = "vinod@123";
    public static string RTSP_URL = "rtsp://192.168.1.64:554/live.sdp";

    private MediaPlayer _mediaPlayer;
    private ISurfaceHolder _surfaceHolder;
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
       // RequestWindowFeature(WindowFeatures.NoTitle);
        //Window window = (Application.Context);
      //  this.Window.SetFlags(WindowManagerFlags.Fullscreen, WindowManagerFlags.Fullscreen);
     //   this.Window.SetBackgroundDrawableResource(Android.Resource.Color.HoloRedDark);
        SetContentView(Resource.Layout.Main);
        SurfaceView surfaceView = FindViewById<SurfaceView>(Resource.Id.surfaceView);
        _surfaceHolder = surfaceView.Holder;
        _surfaceHolder.AddCallback(this);
        _surfaceHolder.SetFixedSize(320, 240);
    }

    public void SurfaceChanged(ISurfaceHolder holder, [GeneratedEnum] Format format, int width, int height)
    {
       // _mediaPlayer.Start();
    }

    public void SurfaceCreated(ISurfaceHolder holder)
    {
        _mediaPlayer = new MediaPlayer();
        _mediaPlayer.SetDisplay(_surfaceHolder);
        Android.Content.Context context = Android.App.Application.Context;

        // Context context = AndApplicationContext();
        Dictionary<String, String> headers = getRtspHeaders();
        Android.Net.Uri source = Android.Net.Uri.Parse(RTSP_URL);  

        try
        {
            // Specify the IP camera's URL and auth headers.
            _mediaPlayer.SetDataSource(context, source);
            //_mediaPlayer.VideoHeight = 100;
                _mediaPlayer.Prepare();//.PrepareAsync();
                _mediaPlayer.SetOnPreparedListener(this);

            // Begin thdev

        }
        catch (Exception e)
        {

        }
    }

    public void SurfaceDestroyed(ISurfaceHolder holder)
    {
        _mediaPlayer.Release();
    }

    public void OnPrepared(MediaPlayer mp)
    {
        mp.Start();
    }

    private Dictionary<String, String> getRtspHeaders()
    {
        Dictionary<String, String> headers = new Dictionary<String, String>();
        String basicAuthValue = getBasicAuthValue(userName, password);
        headers.Add("Authorization", basicAuthValue);
        return headers;
    }

    private String getBasicAuthValue(String usr, String pwd)
    {
        string credentials = usr + ":" + pwd;
        Base64Flags flags =( Base64Flags.UrlSafe | Base64Flags.NoWrap);
        byte[] bytes = System.Text.Encoding.ASCII.GetBytes(credentials);
        return "Basic " + Base64.EncodeToString(bytes, flags);
    }
}

0 个答案:

没有答案