Search in sources :

Example 1 with YoutubeDLException

use of com.yausername.youtubedl_android.YoutubeDLException in project MPlayer by abh80.

the class Function2 method download.

public static void download(String directory, String id, Function2<Float, Boolean, Void> callback) {
    System.out.println(id);
    Thread t = new Thread(() -> {
        YoutubeDLRequest request = new YoutubeDLRequest("https://youtube.com/watch?v=" + id);
        request.addOption("-o", directory + "/" + id + ".mp3");
        request.addOption("--extract-audio");
        request.addOption("--audio-format", "mp3");
        try {
            YoutubeDL.getInstance().execute(request, (progress, etaInSeconds, line) -> {
                System.out.println(line);
                callback.apply(progress, false);
            });
        } catch (YoutubeDLException | InterruptedException e) {
            e.printStackTrace();
        }
    });
    t.start();
    Timer timer = new Timer();
    timer.schedule(new TimerTask() {

        @Override
        public void run() {
            if (!t.isAlive()) {
                callback.apply(100f, true);
                timer.cancel();
            }
        }
    }, 0, 2000);
}
Also used : YoutubeDLException(com.yausername.youtubedl_android.YoutubeDLException) Timer(java.util.Timer) TimerTask(java.util.TimerTask) YoutubeDLRequest(com.yausername.youtubedl_android.YoutubeDLRequest)

Example 2 with YoutubeDLException

use of com.yausername.youtubedl_android.YoutubeDLException in project androidtv-news by anenasa.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    audioManager = (AudioManager) getApplicationContext().getSystemService(Context.AUDIO_SERVICE);
    preferences = getSharedPreferences("io.github.anenasa.news", MODE_PRIVATE);
    channelNum = preferences.getInt("channelNum", 0);
    defaultFormat = preferences.getString("defaultFormat", "best");
    defaultVolume = preferences.getString("defaultVolume", "1.0");
    try {
        YoutubeDL.getInstance().init(getApplication());
    } catch (YoutubeDLException e) {
        Log.e(TAG, Log.getStackTraceString(e));
        errorMessageView.setText(e.toString());
    }
    player = new SimpleExoPlayer.Builder(this).build();
    player.addListener(new Player.Listener() {

        @Override
        public void onPlayerError(@NonNull PlaybackException error) {
            Log.e(TAG, Log.getStackTraceString(error));
            errorMessageView.setText(error.toString());
            if (channel.get(channelNum).needParse() != Channel.NEEDPARSE_NO) {
                // Force parse by removing video url
                channel.get(channelNum).setVideo("");
            }
            play(channelNum);
        }

        @Override
        public void onPlaybackStateChanged(int state) {
            if (state == Player.STATE_READY) {
                errorMessageView.setText("");
            }
        }
    });
    playerView = findViewById(R.id.playerView);
    player.setVideoSurfaceView(playerView);
    textView = findViewById(R.id.textView);
    errorMessageView = findViewById(R.id.errorMessage);
    readChannelList();
    if (channel == null) {
        return;
    }
    if (channelNum >= channel.size()) {
        resetChannelNum();
    }
    timer = new Timer();
    timerTask = new TimerTask() {

        @Override
        public void run() {
            for (int i = 0; i < channel.size(); i++) {
                if (!channel.get(i).isHidden()) {
                    try {
                        channel.get(i).parse();
                    } catch (IOException | YoutubeDLException | JSONException | InterruptedException e) {
                        Log.e(TAG, Log.getStackTraceString(e));
                    }
                }
            }
            saveSettings();
        }
    };
    // Delay timer for one second because if two requests are sent to
    // Hami Video at the same time, one of them will fail.
    timer.scheduleAtFixedRate(timerTask, 1000, 3600000);
}
Also used : PlaybackException(com.google.android.exoplayer2.PlaybackException) Player(com.google.android.exoplayer2.Player) SimpleExoPlayer(com.google.android.exoplayer2.SimpleExoPlayer) YoutubeDLException(com.yausername.youtubedl_android.YoutubeDLException) Timer(java.util.Timer) TimerTask(java.util.TimerTask)

Example 3 with YoutubeDLException

use of com.yausername.youtubedl_android.YoutubeDLException in project MPlayer by abh80.

the class MainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    YTDL.readManifest(getApplicationInfo().dataDir);
    String[] perms = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
    if (!EasyPermissions.hasPermissions(this, perms)) {
        EasyPermissions.requestPermissions(this, "hey", 1, perms);
    }
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
    height = displayMetrics.heightPixels;
    width = displayMetrics.widthPixels;
    try {
        YoutubeDL.getInstance().init(getApplication());
        FFmpeg.getInstance().init(getApplication());
    } catch (YoutubeDLException e) {
        Log.e("YTDL", "failed to initialize youtubedl-android", e);
    }
    initView();
    mView = findViewById(R.id.main);
    requestQueue = Volley.newRequestQueue(getApplicationContext());
    if (!isMyServiceRunning(Player.class)) {
        startService(new Intent(this, Player.class));
    }
}
Also used : YoutubeDLException(com.yausername.youtubedl_android.YoutubeDLException) Intent(android.content.Intent) DisplayMetrics(android.util.DisplayMetrics)

Aggregations

YoutubeDLException (com.yausername.youtubedl_android.YoutubeDLException)3 Timer (java.util.Timer)2 TimerTask (java.util.TimerTask)2 Intent (android.content.Intent)1 DisplayMetrics (android.util.DisplayMetrics)1 PlaybackException (com.google.android.exoplayer2.PlaybackException)1 Player (com.google.android.exoplayer2.Player)1 SimpleExoPlayer (com.google.android.exoplayer2.SimpleExoPlayer)1 YoutubeDLRequest (com.yausername.youtubedl_android.YoutubeDLRequest)1