⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mediaplayerlauncher.java

📁 java语言开发的P2P流媒体系统
💻 JAVA
字号:
/* 
 * P2P-Radio - Peer to peer streaming system
 * Project homepage: http://p2p-radio.sourceforge.net/
 * Copyright (C) 2003-2004 Michael Kaufmann <hallo@michael-kaufmann.ch>
 * 
 * ---------------------------------------------------------------------------
 * 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
 * ---------------------------------------------------------------------------
 */

package p2pradio.players;

import p2pradio.*;
import p2pradio.event.*;
import p2pradio.logging.Logger;
import java.io.*;
import java.net.*;


public class MediaPlayerLauncher extends Player implements WaitForLeadListener
{
	private boolean cancelPlaying = false;
	private HttpPlayer httpPlayer;
		
	public MediaPlayerLauncher(ListenBuffer buffer, HttpPlayer httpPlayer)
	{
		super(buffer, Messages.getString("MediaPlayerLauncher.THREAD_NAME")); //$NON-NLS-1$
		this.httpPlayer = httpPlayer;
	}

	public String getMIMEType()
	{
		return "*/*"; //$NON-NLS-1$
	}
	
	public void waitForLead(WaitForLeadEvent e)
	{
		Logger.info("MediaPlayerLauncher", "MediaPlayerLauncher.STARTING_MEDIA_PLAYER_IN", new Integer(buffer.getTimeToWaitForLead() / 1000)); //$NON-NLS-1$ //$NON-NLS-2$
		buffer.waitForLead();
	}
	
	public void run()
	{
		if (cancelPlaying)
		{
			return;
		}

		buffer.setLead(HttpPlayerServer.MILLISECONDS_IN_MEDIAPLAYER_BUFFER, true);
		
		// Meldung ausgeben vor dem Warten
		buffer.addWaitForLeadListener(this);
		
		buffer.getReady();

		if (cancelPlaying)
		{
			return;
		}
		
		// Versuchen, einen Media-Player zu starten
		if (startMediaPlayer(httpPlayer))
		{
			Logger.info("MediaPlayerLauncher", "MediaPlayerLauncher.TRIED_TO_START"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		else
		{
			Logger.info("MediaPlayerLauncher", "MediaPlayerLauncher.COULD_NOT_START"); //$NON-NLS-1$ //$NON-NLS-2$
		}
	}
	
	public void stopPlayer()
	{
		cancelPlaying = true;
	}
	
	public static boolean startMediaPlayer(HttpPlayer httpPlayer)
	{
		try
		{
			String osName = System.getProperty("os.name").toLowerCase(); //$NON-NLS-1$
			
			boolean isWindows = osName.indexOf("windows") != -1; //$NON-NLS-1$
			boolean isMacOS = osName.indexOf("mac os") != -1; //$NON-NLS-1$
			
			InputStream playlistInputStream = null;
			OutputStream playlistOutputStream = null;
			File playlistFile = null;
			Process process = null;
			
			if (!isMacOS)
			{
				try
				{
					playlistInputStream = new URL(httpPlayer.getPlaylistURL()).openStream();
				
					playlistFile = File.createTempFile("Playlist", ".pls"); //$NON-NLS-1$ //$NON-NLS-2$
					playlistOutputStream = new FileOutputStream(playlistFile);
					
					byte[] buffer = new byte[1024];
					
					int bytesWritten;
					
					while ((bytesWritten = playlistInputStream.read(buffer)) != -1)
					{
						playlistOutputStream.write(buffer, 0, bytesWritten); 
					}
				}
				finally
				{
					try
					{
						if (playlistInputStream != null)
						{
							playlistInputStream.close(); 
						}
					}
					catch (IOException e)
					{
					}
					
					try
					{
						if (playlistOutputStream != null)
						{
							playlistOutputStream.close();
						}
					}
					catch (IOException e)
					{
					}
				}
				
				playlistFile.deleteOnExit();
			}
				
			if (isWindows)
			{
				// Standard-Anwendung f黵 "pls"-Dateien starten
				
				String[] params = new String[3];
				params[0] = "rundll32.exe"; //$NON-NLS-1$
				params[1] = "url.dll,FileProtocolHandler"; //$NON-NLS-1$
				params[2] = playlistFile.getAbsolutePath();
				
				process = Runtime.getRuntime().exec(params);
			}
			else if (isMacOS)
			{
				// iTunes mit AppleScript starten und Strom-URL 鰂fnen
				
				String params[] = new String[3];
				params[0] = "osascript"; //$NON-NLS-1$
				params[1] = "-e"; //$NON-NLS-1$
				params[2] = "tell application \"iTunes\" to open location \"" + httpPlayer.getStreamURL() + "\""; //$NON-NLS-1$ //$NON-NLS-2$
								
				process = Runtime.getRuntime().exec(params); 
			}
			else
			{
				// Linux, SunOS und andere Betriebssysteme:
				// xmms starten
				
				String[] params = new String[2];
				params[0] = "xmms"; //$NON-NLS-1$
				params[1] = playlistFile.getAbsolutePath();
				
				process = Runtime.getRuntime().exec(params);
			}
			
			// Dem Programm ein bisschen Zeit geben zu starten
			Thread.yield(); 
			
			try
			{
				return (process.exitValue() == 0);
			}
			catch (IllegalThreadStateException e)
			{
				// Programm l鋟ft noch
				return true;
			}
		}	
		catch (IOException e)
		{
			return false;
		}
	}
	
	public HttpPlayer getHttpPlayer()
	{
		return httpPlayer;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -