📄 tvactions.java
字号:
/* * Copyright (C) 2005 by Enrico Chiaretti - enrico.chiaretti@gmail.com This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *//* * TvActions.java * * Created on 29 agosto 2005, 20.59 * * To change this template, choose Tools | Options and locate the template under * the Source Creation and Management node. Right-click the template and choose * Open. You can then make changes to the template in the Source Editor. *//** * * @author enrik */import javax.swing.JOptionPane;import java.io.*;import java.util.*;import java.awt.event.WindowEvent;public class TvActions{ public static final int MODE_PAL_DVD = 1; public static final int MODE_PAL_DVD_LONG_PLAY = 2; public static final int MODE_PAL_DVD_EXTRA_LONG_PLAY =3; // good for VHS public static final int MODE_NTSC_DVD = 4; private String mChanListPath = null; private String mHome = null; private Process mRecorder = null; private Process mPlayer = null; private String mCmdPlayer = null; private int mquality = MODE_PAL_DVD_EXTRA_LONG_PLAY; private int mminutes = 250; /** Creates a new instance of TvActions */ public TvActions() { // eventually place initialization code here, such as reading user home directory mHome = java.lang.System.getProperty("user.home"); System.setProperty("user.dir", mHome); try { Runtime.getRuntime().exec("mkdir " + mHome + "/.jvtv"); } catch (Exception e) { }; mChanListPath = mHome + "/.jvtv/jvtv_channels.txt"; mCmdPlayer = "./watchtv.sh"; //mUser = java.lang.System.getProperty("user.name"); } public void setQuality(int aquality) { mquality = aquality; } public void setDuration(int aminutes) { mminutes = aminutes; } public void setPlayer(String aCmdPlayer) { // use this to let the end user set his own player mCmdPlayer = aCmdPlayer; } /** * this returns the file name of the channel list * @return a String containing the file path. */ public String getChanListPath() { return mChanListPath; } /** * this switch the channel you're watching on TV. * @param aChan this is the TvChannel object you're willing to watch at. * If aChan contains a frequency, that will be used to obtain a fine channel tuning. * If the frequency is missing, the channel number shall be used (suitable for the * most of users) */ public void switchChannel(TvChannel aChan) { Runtime CurrentRuntime = Runtime.getRuntime(); try { if (aChan.freq.longValue() == 0) { CurrentRuntime.exec("ptune.pl -c " + aChan.number ); //System.out.println("ptune.pl -c " + aChan.number); System.out.println("Switched to Channel (-c): " + aChan.toString()); } else { CurrentRuntime.exec("ptune.pl -F " + aChan.freq.toString() ); //System.out.println("ptune.pl -F " + aChan.freq.toString()); System.out.println("Switched to Channel (-F): " + aChan.toString()); } } catch (java.io.IOException e) { System.out.println(e.getMessage()); } } public static boolean testSdl() { boolean bsupport; java.io.InputStream stderr = null; java.io.InputStreamReader isr = null; java.io.BufferedReader br = null; bsupport = false; try { Runtime s = Runtime.getRuntime(); String line = null; String command = "mplayer -vo help"; Process Ps = s.exec(command); stderr = Ps.getInputStream(); isr = new java.io.InputStreamReader(stderr); br = new BufferedReader(isr); while ( (line = br.readLine()) != null) { if ( line.indexOf("sdl") == 1 ) { bsupport = true; break; } } br.close(); isr.close(); stderr.close(); Ps.destroy(); } catch (Exception e) {;} //no SDL support? // close files if needed try { if (br!=null) br.close(); } catch (Exception e) {;} try { if (isr!=null) isr.close(); } catch (Exception e) {;} try { if (stderr!=null) stderr.close(); } catch (Exception e) {;} return bsupport; } /** * this runs the default player. * may be we shall let the user to choose his best player. */ public void runPlayer() { Runtime MyRunTime = Runtime.getRuntime(); Process Ps = null; try { if (mPlayer == null) { mPlayer = MyRunTime.exec(mCmdPlayer); } } catch (Exception e) { e.printStackTrace(); } } public void closePlayer() { if (mPlayer != null) { mPlayer.destroy(); mPlayer = null; } } /** * this reads the channel list from the local settings. */ public java.util.Vector readChannels() { ChannelFactory ChanList = new ChannelFactory(mChanListPath); try { ChanList.save(mChanListPath); } catch (Exception e) {}; return ChanList.getChannels(); } /** * this shows the about form. */ public void about() { javax.swing.JFrame JvtvAbout_panel = new JvtvAbout(); JvtvAbout_panel.setVisible(true); } /** * this shows the info form. */ public void info() { javax.swing.JFrame TvInfo_panel = new TvInfo(); TvInfo_panel.setVisible(true); } /** * this shows the help form */ public void help() { javax.swing.JFrame JvtvHelp_panel = new JvtvHelp(); JvtvHelp_panel.setVisible(true); } public void showRecordOpt() { RecordOpt jRecOpts = new RecordOpt(); jRecOpts.setVisible(true); jRecOpts.addWindowListener(new MyCloseOpts(this)); } /** * this shows the Add channel form. */ public void addch() { javax.swing.JFrame JvtvAdd_channel = new TvChAdd(); JvtvAdd_channel.setVisible(true); } /** * this show modify channel form. */ public void modch() { javax.swing.JFrame JvtvMod_channel = new TvChMod(); JvtvMod_channel.setVisible(true); } public void record(TvChannel aChan, int aminutes, int amode) throws java.io.IOException { // this is a simple recording method. Automatically stops after some minutes. int hei; int wid; int meanRate; int peakRate; String Format = null; // default values: MODE_PAL_DVD; hei = 576; wid = 720; meanRate = 4000000; peakRate = 8000000; Format = "PAL"; switch (amode) { case MODE_PAL_DVD: // nothing to do here break; case MODE_PAL_DVD_LONG_PLAY: // half dvd res hei = 576; wid = 352; meanRate = 3500000; peakRate = 4000000; Format = "PAL"; break; case MODE_PAL_DVD_EXTRA_LONG_PLAY: // quarter dvd res hei = 576; wid = 352; meanRate = 2000000; peakRate = 2200000; Format = "PAL"; break; case MODE_NTSC_DVD: // I hope theese values are correct, since I have never tried them // unfortunately, I can not set the NTSC standard using ptune.pl hei = 480; wid = 720; meanRate = 4000000; peakRate = 8000000; Format = "NTSC"; } // add a 10% tolerance, since the system sleep is very unreliable. // if jvtv crashes or is closed, this will be the ending time record(aChan, aminutes * 110 / 100, hei, wid, meanRate, peakRate, Format); // here I try out the scheduler Timer RecTimer = new Timer(); Calendar EndTime = GregorianCalendar.getInstance(); EndTime.add(Calendar.MINUTE, aminutes); System.out.println("end time: " + EndTime.getTime().toString()); // this is a reliable enough timer. This will stop recording if everything goes well. RecTimer.schedule(new TaskTerminator(mRecorder), EndTime.getTime()); } public void record(TvChannel aChan, int aminutes, int ahei, int awid, long ameanRate, long apeakRate, String aFormat) throws java.io.IOException { // this is a highly customizable recording method. Runtime CurrRuntime = null; String ChanNum = "36"; //default channel number String Command = null; // overriding default settings: if (aChan != null) ChanNum = aChan.number; Command = "record-v4l2.pl -s " + aFormat + " --streamtype 10 --channel " + ChanNum + " -H " + ahei + " -W "+ awid + " --bitrate " + ameanRate + " --peakbitrate " + apeakRate + " -t " + aminutes * 60 + " -D " + System.getProperty("user.dir"); CurrRuntime = Runtime.getRuntime(); mRecorder = CurrRuntime.exec(Command); } public boolean stopRecording() throws java.io.IOException { // this stops recording, any method you called to start it. boolean result = false; if (mRecorder != null) { mRecorder.destroy(); mRecorder = null; result = true; } return result; } public void startRecording(TvChannel aChan) throws java.io.IOException { // this records for 250 minutes!! that means record until the user calls stopRecording() record(aChan, mminutes, mquality); } public void saveList(java.util.Vector aChannels, String aDestFile) throws Exception { new ChannelFactory(aChannels).save(aDestFile); } public void saveList(java.util.Vector aChannels) { // this asks for a file name and saves the tv station list. java.io.File SelectedFile; boolean bUserCancel = false; javax.swing.JDialog DialogWindow = new javax.swing.JDialog(); javax.swing.JFileChooser FileChooser = new javax.swing.JFileChooser(); FileChooser.showSaveDialog(DialogWindow); try { SelectedFile = FileChooser.getSelectedFile(); saveList(aChannels, SelectedFile.getAbsolutePath()); } catch (NullPointerException e) { // user cancel! System.out.println("User canceled selection!"); } catch (Exception e) { JOptionPane.showMessageDialog(DialogWindow, e.getMessage(), "Unexpected error", JOptionPane.ERROR_MESSAGE); } }}class MyCloseOpts implements java.awt.event.WindowListener{ private TvActions mActs = null; public MyCloseOpts(TvActions aAct) { mActs = aAct; } public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) { } public void windowClosing(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { RecordOpt MyRecOpts = (RecordOpt) e.getSource(); if (MyRecOpts.isSaved()) { mActs.setQuality(MyRecOpts.getQuality()); mActs.setDuration(MyRecOpts.getMinutes()); //javax.swing.JOptionPane.showMessageDialog(MyRecOpts, " aaaa"); } MyRecOpts.setVisible(false); } public void windowDeiconified(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowOpened(WindowEvent e) {} }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -