mirror of
https://github.com/DSheirer/sdrtrunk.git
synced 2026-11-12 08:37:25 -05:00
111 lines
1.7 KiB
Java
111 lines
1.7 KiB
Java
package alias.action.clip;
|
|
|
|
import java.io.File;
|
|
|
|
import javax.sound.sampled.AudioInputStream;
|
|
import javax.sound.sampled.AudioSystem;
|
|
import javax.sound.sampled.Clip;
|
|
import javax.xml.bind.annotation.XmlTransient;
|
|
|
|
import message.Message;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import alias.Alias;
|
|
import alias.action.AliasActionType;
|
|
import alias.action.RecurringAction;
|
|
|
|
public class ClipAction extends RecurringAction
|
|
{
|
|
private final static Logger mLog = LoggerFactory.getLogger( ClipAction.class );
|
|
|
|
private String mFilePath;
|
|
|
|
@XmlTransient
|
|
private Clip mClip;
|
|
|
|
public ClipAction()
|
|
{
|
|
}
|
|
|
|
@Override
|
|
public AliasActionType getType()
|
|
{
|
|
return AliasActionType.CLIP;
|
|
}
|
|
|
|
public String getPath()
|
|
{
|
|
return mFilePath;
|
|
}
|
|
|
|
public void setPath( String path )
|
|
{
|
|
mFilePath = path;
|
|
}
|
|
|
|
@Override
|
|
public void performAction( Alias alias, Message message )
|
|
{
|
|
try
|
|
{
|
|
play();
|
|
}
|
|
catch( Exception e )
|
|
{
|
|
mLog.error( "Couldn't play audio clip", e );
|
|
}
|
|
}
|
|
|
|
public void play() throws Exception
|
|
{
|
|
try
|
|
{
|
|
if( mFilePath != null )
|
|
{
|
|
if( mClip == null )
|
|
{
|
|
mClip = AudioSystem.getClip();
|
|
|
|
AudioInputStream ais =
|
|
AudioSystem.getAudioInputStream( new File( mFilePath ) );
|
|
|
|
mClip.open( ais );
|
|
}
|
|
|
|
if( mClip.isRunning() )
|
|
{
|
|
mClip.stop();
|
|
}
|
|
|
|
mClip.setFramePosition( 0 );
|
|
|
|
mClip.start();
|
|
}
|
|
}
|
|
catch( Exception e )
|
|
{
|
|
mClip = null;
|
|
|
|
mLog.error( "Error playing sound clip [" + mFilePath + "] - " + e.getMessage() );
|
|
|
|
throw e;
|
|
}
|
|
}
|
|
|
|
|
|
@Override
|
|
public String toString()
|
|
{
|
|
if( mFilePath == null )
|
|
{
|
|
return "Play Clip";
|
|
}
|
|
else
|
|
{
|
|
return "Play Clip: " + mFilePath;
|
|
}
|
|
}
|
|
}
|