原 自学JVAVA---(68)--(内功心法【66】)QUARTZ

QUARTZ
【小城贝尔】
定时调度更高级,开源组织在雄起。
初学抓瞎难自己,多看案例勿放弃。

quartz:石英
下载网址:http://www.quartz-scheduler.org/

// 需要定时运行的资源
public  class  QUARTZB  implements Job {

    public void execute(JobExecutionContext context)
            throws JobExecutionException {
        for (int i = 0 ; i < 10; i ++){
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("我是内部类  i am learning  QUARTZ......."+i);
        }
    }
}
 SimpleExample {
  public void run() throws Exception {
    //首先根据工厂获得调度
    // First we must get a reference to a scheduler
    SchedulerFactory sf = new StdSchedulerFactory();
    Scheduler sched = sf.getScheduler();

    //任务调度运行的时间
    // computer a time that is on the next round minute
    Date runTime = evenMinuteDate(new Date());
    //将时间改到五秒后运行
    runTime = new Date(System.currentTimeMillis() + 1000*5);

    //需要运行的任务
    // define the job and tie it to our HelloJob class
    JobDetail job = newJob(QUARTZB.class).withIdentity("job2", "group2").build();

    //触发器 触发在指定的时间运行
    // Trigger the job to run on the next round minute
    Trigger trigger = newTrigger().withIdentity("trigger2", "group2").startAt(runTime).build();

    // Tell quartz to schedule the job using our trigger
    sched.scheduleJob(job, trigger);
    //调度执行
    // Start up the scheduler (nothing can actually run until the
    // scheduler has been started)
    sched.start();

    //等待调度任务执行 一段时间后关闭调度
    // wait long enough so that the scheduler as an opportunity to
    // run the job!
    try {
      // wait 65 seconds to show job
      Thread.sleep(65L * 1000L);
      // executing...
    } catch (Exception e) {
      //
    }
    //关闭调度
    // shut down the scheduler
    sched.shutdown(true);

  }

  public static void main(String[] args) throws Exception {

    SimpleExample example = new SimpleExample();
    example.run();
/*

    我是内部类  i am learning  QUARTZ.......0
    我是内部类  i am learning  QUARTZ.......1
    我是内部类  i am learning  QUARTZ.......2
    我是内部类  i am learning  QUARTZ.......3
    我是内部类  i am learning  QUARTZ.......4
    我是内部类  i am learning  QUARTZ.......5
    我是内部类  i am learning  QUARTZ.......6
    我是内部类  i am learning  QUARTZ.......7
    我是内部类  i am learning  QUARTZ.......8
    我是内部类  i am learning  QUARTZ.......9

*/

  }
}


在Java中获取视频时长通常需要借助第三方库来解析视频文件,因为Java标准库本身并不直接支持视频文件的处理。一个常用的库是Xuggler,它可以被用来获取视频的基本信息,包括时长。以下是使用Xuggler库来获取视频时长的一个基本示例: ```java import com.xuggle.mediatool.IMediaReader; import com.xuggle.mediatool.ToolFactory; import com.xuggle.mediatool.event.IVideoPictureEvent; import com.xuggle.xuggler.IStream; import com.xuggle.xuggler.IStreamCoder; import com.xuggle.xuggler.IVideoResampler; public class VideoDuration { public static void main(String[] args) { // 创建媒体读取器 IMediaReader mediaReader = ToolFactory.makeReader("video_file_path.mp4"); // 设置事件处理器 mediaReader.addListener(new MediaAdapter() { @Override public void onVideoPicture(IVideoPictureEvent event) { // 获取视频流 IStream stream = event.getStream(); // 获取流编解码器 IStreamCoder coder = stream.getStreamCoder(); if (coder.getCodecType() == IStreamCoder.Type.VIDEO) { long duration = stream.getContainer().getDuration(); System.out.println("视频时长: " + duration / 1000 + " 秒"); } } }); // 读取媒体文件 while (mediaReader.readPacket() == null) { // 循环读取直到结束 } } } ``` 在这个示例中,我们创建了一个`IMediaReader`对象来读取视频文件,并且设置了一个事件监听器来处理视频帧事件。当读取到视频帧时,我们检查了帧所属的流,并获取了流的时长信息。需要注意的是,这个示例使用了Xuggler的旧API,而Xuggler已经不再维护。现在推荐使用Xuggler的后续项目——JAVE(Java Audio Video Encoder)。 请确保在项目中添加了所需的依赖库才能运行上述代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值