View Javadoc

1   /*
2    *  MicroEmulator
3    *  Copyright (C) 2006 John Blackmon
4    *
5    *  This library is free software; you can redistribute it and/or
6    *  modify it under the terms of the GNU Lesser General Public
7    *  License as published by the Free Software Foundation; either
8    *  version 2.1 of the License, or (at your option) any later version.
9    *
10   *  This library is distributed in the hope that it will be useful,
11   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *  Lesser General Public License for more details.
14   *
15   *  You should have received a copy of the GNU Lesser General Public
16   *  License along with this library; if not, write to the Free Software
17   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  
20  package javax.microedition.media;
21  
22  import java.io.BufferedInputStream;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.util.Iterator;
26  import java.util.Vector;
27  
28  import javax.sound.sampled.AudioFormat;
29  import javax.sound.sampled.AudioInputStream;
30  import javax.sound.sampled.AudioSystem;
31  import javax.sound.sampled.Clip;
32  import javax.sound.sampled.DataLine;
33  import javax.sound.sampled.LineEvent;
34  import javax.sound.sampled.LineListener;
35  import javax.sound.sampled.LineUnavailableException;
36  import javax.sound.sampled.UnsupportedAudioFileException;
37  
38  class SampledAudioPlayer implements Player, LineListener 
39  {
40  	private AudioInputStream audioInputStream = null;
41      private AudioInputStream decodedStream = null;
42  	private Clip clip = null;
43  	private Vector vListeners = null;           // All PlayerListeners for this audio
44  	private String strType = null;
45  	
46  	public boolean open( InputStream stream, String type ) 
47  	{
48  	    strType = type;
49  		try 
50  	    {
51  			audioInputStream = AudioSystem.getAudioInputStream( new BufferedInputStream( stream ) );
52  			AudioFormat format = audioInputStream.getFormat();
53  			if (format.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) 
54  			{
55  			    AudioFormat baseFormat = audioInputStream.getFormat();
56  			    AudioFormat decodedFormat = new AudioFormat( AudioFormat.Encoding.PCM_SIGNED,
57  										                     baseFormat.getSampleRate(),
58  										                     16,
59  										                     baseFormat.getChannels(),
60  										                     baseFormat.getChannels() * 2,
61  										                     baseFormat.getSampleRate(),
62  										                     false );
63  			    decodedStream = AudioSystem.getAudioInputStream( decodedFormat, audioInputStream );
64  			    int frameLength = (int) decodedStream.getFrameLength();
65  			    int frameSize = decodedFormat.getFrameSize();
66  			    DataLine.Info info = new DataLine.Info( Clip.class, decodedFormat,
67  			    										frameLength * frameSize);
68  			    clip = (Clip) AudioSystem.getLine( info );
69  		    	clip.open( decodedStream );
70  			}
71  	        else
72  	        {
73  				DataLine.Info info2 = new DataLine.Info( Clip.class, format, AudioSystem.NOT_SPECIFIED );
74  				clip = (Clip) AudioSystem.getLine( info2 );
75  		    	clip.open( audioInputStream );
76  	        }
77  	    } 
78  	    catch( UnsupportedAudioFileException e ){ e.printStackTrace(); return false; }
79  	    catch( IOException e ){ e.printStackTrace(); return false; }
80  	    catch( LineUnavailableException e ){ e.printStackTrace(); return false; }
81  		return true;
82  	}
83  
84  	public void addPlayerListener(PlayerListener playerListener) 
85  	{
86  		if( vListeners == null )
87  			vListeners = new Vector();
88  		vListeners.add( playerListener );
89  	}
90  
91  	public void close() 
92  	{
93  		Manager.mediaDone( this );
94  		if( clip != null )
95  		{
96  			clip.flush();
97  	    	clip.close();
98  		}
99  		
100 		try 
101 	    {
102 			if( decodedStream != null )
103 				decodedStream.close();
104 			if( audioInputStream != null )
105 				audioInputStream.close();
106 	    } 
107 	    catch( IOException e ) { e.printStackTrace(); }
108 	}
109 
110 	public void deallocate() {
111 		if( clip != null )
112 			clip.flush();
113 	}
114 
115 	public String getContentType() {
116 		return strType;
117 	}
118 
119 	public long getDuration() {
120 		// TODO Auto-generated method stub
121 		return 0;
122 	}
123 
124 	public long getMediaTime() {
125 		if( clip != null )
126 			return clip.getMicrosecondPosition();
127 		return 0;
128 	}
129 
130 	public int getState() {
131 		// TODO Auto-generated method stub
132 		return 0;
133 	}
134 
135 	public void prefetch() throws MediaException {
136 		// TODO Auto-generated method stub
137 	}
138 
139 	public void realize() throws MediaException {
140 		// TODO Auto-generated method stub
141 	}
142 
143 	public void removePlayerListener(PlayerListener playerListener) 
144 	{
145 	   if( vListeners == null )
146 		   return;
147 	   for( Iterator it = vListeners.iterator (); it.hasNext (); ) 
148 	   {
149 		    PlayerListener listener = (PlayerListener) it.next ();
150 		    if( listener == playerListener )
151 		    {
152 		    	vListeners.remove( listener );
153 		    	break;
154 	   		}
155 	   }
156 	}
157 
158 	public void setLoopCount(int count) {
159 		if( clip != null )
160 			clip.loop( count );
161 	}
162 
163 	public long setMediaTime(long now) throws MediaException {
164 		if( clip != null )
165 			clip.setMicrosecondPosition( now );
166 		return 0;
167 	}
168 
169 	public void start() throws MediaException {
170 		if( clip != null )
171 		{
172 			clip.addLineListener( this );
173 			clip.start();
174 		}
175 	}
176 
177 	public void stop() throws MediaException {
178 		if( clip != null )
179 			clip.stop();
180 	}
181 
182 	public Control getControl(String controlType) {
183 		// TODO Auto-generated method stub
184 		return null;
185 	}
186 
187 	public Control[] getControls() {
188 		// TODO Auto-generated method stub
189 		return null;
190 	}
191 
192 	public void update( LineEvent event ) 
193 	{
194 		if (event.getType().equals(LineEvent.Type.STOP))
195 		{
196 			close();
197 			if( vListeners != null )
198 			{
199 				for( Iterator it = vListeners.iterator (); it.hasNext (); ) 
200 				{
201 				    PlayerListener listener = (PlayerListener) it.next ();
202 				    listener.playerUpdate( this, PlayerListener.END_OF_MEDIA, null );
203 				}
204 			}
205 		}
206 	}
207 }