~gryle-devel/gryle/trunk-deleted

« back to all changes in this revision

Viewing changes to src/javazoom/jl/player/jlp.java

  • Committer: Richard Leo Marsh Warburton
  • Date: 2007-01-13 22:08:02 UTC
  • mto: (1.1.6 gryle)
  • mto: This revision was merged to the branch mainline in revision 9.
  • Revision ID: rlmw@viglab-28-20070113220802-6cjjur0hdk1rce47
added src files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * 11/19/04             1.0 moved to LGPL.
 
3
 * 
 
4
 * 06/04/01             Streaming support added. javalayer@javazoom.net
 
5
 * 
 
6
 * 29/01/00             Initial version. mdm@techie.com
 
7
 *-----------------------------------------------------------------------
 
8
 *   This program is free software; you can redistribute it and/or modify
 
9
 *   it under the terms of the GNU Library General Public License as published
 
10
 *   by the Free Software Foundation; either version 2 of the License, or
 
11
 *   (at your option) any later version.
 
12
 *
 
13
 *   This program is distributed in the hope that it will be useful,
 
14
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
15
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
16
 *   GNU Library General Public License for more details.
 
17
 *
 
18
 *   You should have received a copy of the GNU Library General Public
 
19
 *   License along with this program; if not, write to the Free Software
 
20
 *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
21
 *----------------------------------------------------------------------
 
22
 */
 
23
 
 
24
package javazoom.jl.player;
 
25
 
 
26
import java.io.BufferedInputStream;
 
27
import java.io.FileInputStream;
 
28
import java.io.IOException;
 
29
import java.io.InputStream;
 
30
import java.net.URL;
 
31
 
 
32
import javazoom.jl.decoder.JavaLayerException;
 
33
 
 
34
/**
 
35
 * The <code>jlp</code> class implements a simple command-line player for MPEG
 
36
 * audio files.
 
37
 * 
 
38
 * @author Mat McGowan (mdm@techie.com)
 
39
 */
 
40
public class jlp {
 
41
        private String fFilename = null;
 
42
 
 
43
        private boolean remote = false;
 
44
 
 
45
        public static void main(String[] args) {
 
46
                int retval = 0;
 
47
                try {
 
48
                        jlp player = createInstance(args);
 
49
                        if (player != null)
 
50
                                player.play();
 
51
                } catch (Exception ex) {
 
52
                        System.err.println(ex);
 
53
                        ex.printStackTrace(System.err);
 
54
                        retval = 1;
 
55
                }
 
56
                System.exit(retval);
 
57
        }
 
58
 
 
59
        static public jlp createInstance(String[] args) {
 
60
                jlp player = new jlp();
 
61
                if (!player.parseArgs(args))
 
62
                        player = null;
 
63
                return player;
 
64
        }
 
65
 
 
66
        private jlp() {
 
67
        }
 
68
 
 
69
        public jlp(String filename) {
 
70
                init(filename);
 
71
        }
 
72
 
 
73
        protected void init(String filename) {
 
74
                fFilename = filename;
 
75
        }
 
76
 
 
77
        protected boolean parseArgs(String[] args) {
 
78
                boolean parsed = false;
 
79
                if (args.length == 1) {
 
80
                        init(args[0]);
 
81
                        parsed = true;
 
82
                        remote = false;
 
83
                } else if (args.length == 2) {
 
84
                        if (!(args[0].equals("-url"))) {
 
85
                                showUsage();
 
86
                        } else {
 
87
                                init(args[1]);
 
88
                                parsed = true;
 
89
                                remote = true;
 
90
                        }
 
91
                } else {
 
92
                        showUsage();
 
93
                }
 
94
                return parsed;
 
95
        }
 
96
 
 
97
        public void showUsage() {
 
98
                System.out.println("Usage: jlp [-url] <filename>");
 
99
                System.out.println("");
 
100
                System.out.println(" e.g. : java javazoom.jl.player.jlp localfile.mp3");
 
101
                System.out
 
102
                                .println("        java javazoom.jl.player.jlp -url http://www.server.com/remotefile.mp3");
 
103
                System.out
 
104
                                .println("        java javazoom.jl.player.jlp -url http://www.shoutcastserver.com:8000");
 
105
        }
 
106
 
 
107
        public void play() throws JavaLayerException {
 
108
                try {
 
109
                        System.out.println("playing " + fFilename + "...");
 
110
                        InputStream in = null;
 
111
                        if (remote == true)
 
112
                                in = getURLInputStream();
 
113
                        else
 
114
                                in = getInputStream();
 
115
                        AudioDevice dev = getAudioDevice();
 
116
                        Player player = new Player(in, dev);
 
117
                        player.play();
 
118
                } catch (IOException ex) {
 
119
                        throw new JavaLayerException("Problem playing file " + fFilename,
 
120
                                        ex);
 
121
                } catch (Exception ex) {
 
122
                        throw new JavaLayerException("Problem playing file " + fFilename,
 
123
                                        ex);
 
124
                }
 
125
        }
 
126
 
 
127
        /**
 
128
         * Playing file from URL (Streaming).
 
129
         */
 
130
        protected InputStream getURLInputStream() throws Exception {
 
131
 
 
132
                URL url = new URL(fFilename);
 
133
                InputStream fin = url.openStream();
 
134
                BufferedInputStream bin = new BufferedInputStream(fin);
 
135
                return bin;
 
136
        }
 
137
 
 
138
        /**
 
139
         * Playing file from FileInputStream.
 
140
         */
 
141
        protected InputStream getInputStream() throws IOException {
 
142
                FileInputStream fin = new FileInputStream(fFilename);
 
143
                BufferedInputStream bin = new BufferedInputStream(fin);
 
144
                return bin;
 
145
        }
 
146
 
 
147
        protected AudioDevice getAudioDevice() throws JavaLayerException {
 
148
                return FactoryRegistry.systemRegistry().createAudioDevice();
 
149
        }
 
150
 
 
151
}