Android使用libvlc将实时视频流保存到存储中

时间:2019-06-03 11:51:25

标签: android xamarin streaming libvlc libvlcsharp

我正在尝试使用libvlc将实时视频流保存到Android App中的存储中。 我可以使用命令行在PC上完成此操作,并且可以正常工作,我可以记录文件并随后查看。

但是在应用程序中,文件记录的大小只有151B,可能是空的,如果我尝试打开它,则会收到消息“无法播放此视频格式”

我的问题是,是否可以使用libvlc在Android中进行记录存储?

我对编程还很陌生,所以任何建议都会有所帮助

    const string VIDEO_URL = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";

    public MainPage()
    {
        InitializeComponent();
        Core.Initialize();           
        using (var libvlc = new LibVLC())
        using (var mediaPlayer = new MediaPlayer(libvlc))
        {
            var media = new Media(libvlc, VIDEO_URL, FromType.FromLocation);
            var currentDirectory = "/storage/emulated/0/dcim/";
            var destination = Path.Combine(currentDirectory, "record4.mp4");

            // Define stream output options. 
            // In this case stream to a file with the given path and play locally the stream while streaming it.
            media.AddOption(":sout=#transcode{vcodec=h264}:std{access=file,dst=" + destination + "}");

            // Start recording
             mediaPlayer.Play(media);
        }
    }   

2 个答案:

答案 0 :(得分:0)

必须关闭Mediaplayer才能关闭该应用程序。这是一个临时解决方案。 只要有适当的解决方案,便会对其进行更新。

   public partial class MainPage : ContentPage
{
    const string VIDEO_URL = "rtsp://184.72.239.149/vod/mp4:BigBuckBunny_175k.mov";

    public MainPage()
    {
        InitializeComponent();
        Core.Initialize();           
        using (var libvlc = new LibVLC())
        using (var mediaPlayer = new MediaPlayer(libvlc))
        {
            var media = new Media(libvlc, VIDEO_URL, FromType.FromLocation);
            var currentDirectory = "/storage/emulated/0/dcim/";
            var destination = Path.Combine(currentDirectory, "record7.mp4");

            // Define stream output options. 
            // In this case stream to a file with the given path and play locally the stream while streaming it.

            media.AddOption(":sout=#file{dst=" + destination + "}");
            media.AddOption(":sout-keep");


            // Start recording
             mediaPlayer.Play(media);

            *// Added these lines that create event handler error for Xamarin.forms, 
            // but at least it stops mediaplayer before closing App. and the 
            // recording can be play afterwards.*
            Console.WriteLine($"Recording in {destination}");
            Console.WriteLine("Press any key to exit");
            Console.ReadKey();
        }
    }
}

答案 1 :(得分:0)

首先创建目标文件,然后还将URL解析为URI而不是路径, 将以下arg添加到libvlc

Final ArrayList<String> args = new ArrayList<>();
args.add (" &ndash; aout=opensles");
args.add (" &ndash; vout=android_display");
args.add (" &ndash; audio-time-stretch");
args.add (" &ndash; no-sub-autodetect-file");
args.add (" &ndash; swscale-mode=0");
args.add (" &ndash; network-caching=500");
args.add (" &ndash; no-drop-late-frames");
args.add (" &ndash; no-skip-frames");
args.add (" &ndash; avcodec-skip-frame");
args.add (" &ndash; avcodec-hw=any");
mLibVLC = new LibVLC (mContext, args);