Libvlc android异常处理

时间:2016-05-17 11:39:05

标签: android libvlc

我找不到任何有关如何处理错误的信息:

E/VLC: [7126d274] core input: Your input can't be opened E/VLC: [7126d274] core input: VLC is unable to open the MRL ...

当我尝试在此错误后立即加载另一个URI时,这会导致活动崩溃,所以我想释放并重新启动播放器。

由于

修改 这是我在onResume

中创建一次播放器的方法
    private void createPlayer() {
    releasePlayer();
    try {
        ArrayList<String> options = new ArrayList<String>();
        options.add("--aout=opensles");
        options.add("--audio-time-stretch");
        options.add("-vvv"); // verbosity
        libvlc = new LibVLC(options);

        libvlc.setOnNativeCrashListener(this);
        mSurfaceHolder.setKeepScreenOn(true);

        // Create media player
        mMediaPlayer = new MediaPlayer(libvlc);
        mMediaPlayer.setEventListener(mPlayerListener);

        // Set up video output
        final IVLCVout vout = mMediaPlayer.getVLCVout();
        vout.setVideoView(mSurfaceView);
        vout.addCallback(StandardPlayerMain.this);
        vout.attachViews();

    } catch (Exception e) {
        Toast.makeText(this, "Error creating player!", Toast.LENGTH_LONG).show();
    }
}

这就是我开始播放每个URI的方式:

private void playMedia(String mediaString)
{
    if(mMediaPlayer == null) {
        createPlayer();
        return;
    }

    if(mMediaPlayer.isPlaying()) {
        mMediaPlayer.stop();
    }
    Uri mUri = Uri.parse(mediaString);
    Media m = new Media(libvlc, mUri);
    m.setHWDecoderEnabled(false, false);
    try{
        mMediaPlayer.setMedia(m);
        mMediaPlayer.play();
    }
    catch (Exception e)
    {
        Log.v("myapp", e.getMessage());
    }
}

1 个答案:

答案 0 :(得分:0)

我的解决方案就是通过这种方式使用logcat。

一个简单的日志读取器类

 public static final class MyLibVLCLogReader {
        private static final String TAG = MyLibVLCLogReader.class.getCanonicalName();

        private static String[] runClearCMD = new String[]{"logcat", "-c"};
        private static String[] runCMD = new String[]{"logcat", "*:D", "-e", "libvlc"};

        private Thread thread;
        private boolean isRun = true;

        private OnLogListener logListener;

        public MyLibVLCLogReader() {
            thread = new Thread(runnable);
        }

        public MyLibVLCLogReader(OnLogListener listener) {
            logListener = listener;
            thread = new Thread(runnable);
        }

        public void Start() {
            thread.start();
        }

        public void Stop() {
            isRun = false;
        }

        private Runnable runnable = new Runnable() {
            @Override
            public void run() {
                Log.v(TAG, "Begin listening logger");
                try {
                    // Clear logs
                    Runtime.getRuntime().exec(runClearCMD);

                    // Capture logs
                    Process process = Runtime.getRuntime().exec(runCMD);
                    BufferedReader bufferedReader = new BufferedReader(
                            new InputStreamReader(process.getInputStream()));

                    String line;
                    while (isRun && (line = bufferedReader.readLine()) != null) {
                        if (logListener != null) logListener.onLogCapture(line);
                    }

                    bufferedReader.close();
                    process.destroy();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                Log.v(TAG, "Exit listening logger");
            }
        };

        public interface OnLogListener {
            public void onLogCapture(String line);
        }
    }

使用日志时,您需要获得读取日志的权限。

    <uses-permission
        android:name="android.permission.READ_LOGS"
        tools:ignore="ProtectedPermissions" />