isRunning()
in和isActibe()
in和有什么不一样DataLine
?当 line 实际处理数据时,这两个标志似乎都已设置。
更新
那里有2对方法:
1) 打开/关闭
2) 启动/停止
和三面旗帜
1) 是开放的
2) 正在运行
3) 活跃
任何人都可以推断出它们之间和/或彼此之间的一些关系吗?例如,很明显,isRunning 意味着 isOpen,isActive 意味着 isOpen,但 isActive 是否意味着 isRunning,反之亦然?或者他们是独立的?
我猜 Line在被调用isOpen
后立即变为。检查是否已调用也是如此open()
。isOpen
open()
我可以以某种方式检查我是否打过电话start()
吗?也许其中一些isActive
或者isRunning
是为了那个?但是哪一个?
更新 1
以下代码旨在将音频从默认系统声音输入播放到默认系统声音输出。
public static void main(String[] args) throws LineUnavailableException, InterruptedException {
AudioFormat audioFormat = new AudioFormat(16000f, 16, 1, true, false);
final SourceDataLine outputLine;
final TargetDataLine inputLine;
outputLine = AudioSystem.getSourceDataLine(audioFormat);
inputLine = AudioSystem.getTargetDataLine(audioFormat);
Thread player = new Thread("player") {
@Override
public void run() {
byte[] b = new byte[2048];
int readen;
log.info("Starting transfer");
while( !inputLine.isOpen() ) {
try {
log.info("TargetDataLine is closed, waiting");
sleep(1000);
} catch (InterruptedException e) {
interrupt();
}
}
while( !inputLine.isRunning() ) {
try {
log.info("TargetDataLine is not running, waiting");
sleep(100);
} catch (InterruptedException e) {
interrupt();
}
}
while((readen = inputLine.read(b,0,b.length))>=0 && !isInterrupted()) {
outputLine.write(b,0,readen);
}
}
};
player.start();
outputLine.open();
outputLine.start();
inputLine.open();
inputLine.start();
player.join();
}
代码输出如下:
15:20:01.369 [player] INFO tests.DataLineFlags01 - Starting transfer
15:20:01.371 [player] INFO tests.DataLineFlags01 - TargetDataLine is closed, waiting
15:20:02.371 [player] INFO tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.471 [player] INFO tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.571 [player] INFO tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.671 [player] INFO tests.DataLineFlags01 - TargetDataLine is not running, waiting
15:20:02.771 [player] INFO tests.DataLineFlags01 - TargetDataLine is not running, waiting
...
这证明 TargetDataLine 在start()
被调用后没有进入“运行”状态。
标志也是isActive()
如此。
这意味着
(1) 无法知道是否start()
被调用 (2)isRunning()
并且isActive()
标志是无用的
这对吗?可能它们对SourceDataLine
对象有用吗?
更新 2
以下应用
package testing.Test_TargetDataLine_01;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.TargetDataLine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class App
{
private static Logger log = LoggerFactory.getLogger(App.class);
private final AudioFormat audioFormat;
private final TargetDataLine targetDataLine;
private final Thread observer = new Thread("observer") {
{
setDaemon(true);
}
public void run() {
while( !isInterrupted() ) {
if( targetDataLine != null ) {
log.info("targetDataLine: Opened={}, Active={}, Running={}", targetDataLine.isOpen(), targetDataLine.isActive(), targetDataLine.isRunning());
}
else {
log.info("targetDataLine: is null");
}
try {
sleep(10);
} catch (InterruptedException e) {
}
}
};
};
public App() throws LineUnavailableException, InterruptedException {
byte[] buffer = new byte[160*2]; // 1/100 of second
log.info("App starting");
log.info("Observer starting");
observer.start();
log.info("Sleeping 500");
Thread.sleep(500);
log.info("Creating targetDataLine");
audioFormat = new AudioFormat(16000, 16, 1, true, false);
targetDataLine = AudioSystem.getTargetDataLine(audioFormat);
log.info("Sleeping 500");
Thread.sleep(500);
log.info("Opening targetDataLine");
targetDataLine.open();
log.info("Sleeping 500");
Thread.sleep(500);
log.info("Starting targetDataLine");
targetDataLine.start();
log.info("Sleeping 500");
Thread.sleep(500);
log.info("Reading target data line for 5 seconds");
for(int i=0; i<50; ++i) {
targetDataLine.read(buffer, 0, buffer.length);
log.info("Stage " + i);
}
log.info("Stopping targetDataLine");
targetDataLine.stop();
log.info("Sleeping 500");
Thread.sleep(500);
log.info("Closing targetDataLine");
targetDataLine.close();
log.info("Sleeping 500");
Thread.sleep(500);
//log.info("Interrupting observer");
//observer.interrupt();
}
public static void main( String[] args ) throws LineUnavailableException, InterruptedException
{
new App();
}
}
产生以下输出:
20:58:23,360 0 [main] INFO testing.Test_TargetDataLine_01.App - 应用程序启动 20:58:23,362 2 [main] INFO testing.Test_TargetDataLine_01.App - 观察者开始 20:58:23,362 2 [main] INFO testing.Test_TargetDataLine_01.App - 睡眠 500 20:58:23,362 2 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 20:58:23,372 12 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 ... 20:58:23,842 482 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 20:58:23,852 492 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 20:58:23,862 502 [main] INFO testing.Test_TargetDataLine_01.App - 创建 targetDataLine 20:58:23,862 502 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 20:58:23,872 512 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 ... 20:58:23,912 552 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 20:58:23,922 562 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: 为空 20:58:23,929 569 [main] INFO testing.Test_TargetDataLine_01.App - 睡眠 500 20:58:23,933 573 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:23,943 583 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false ... 20:58:24,413 1053 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:24,423 1063 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:24,430 1070 [main] INFO testing.Test_TargetDataLine_01.App - 打开 targetDataLine 20:58:24,433 1073 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:24,443 1083 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false ... 20:58:24,483 1123 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:24,493 1133 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:24,501 1141 [main] INFO testing.Test_TargetDataLine_01.App - 睡眠 500 20:58:24,503 1143 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:24,513 1153 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false ... 20:58:24,983 1623 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:24,993 1633 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:25,002 1642 [main] INFO testing.Test_TargetDataLine_01.App - 启动 targetDataLine 20:58:25,002 1642 [main] INFO testing.Test_TargetDataLine_01.App - Sleeping 500 20:58:25,003 1643 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:25,013 1653 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false ... 20:58:25,483 2123 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:25,493 2133 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:25,502 2142 [main] INFO testing.Test_TargetDataLine_01.App - 读取目标数据行 5 秒 20:58:25,502 2142 [main] INFO testing.Test_TargetDataLine_01.App - 阶段 0 20:58:25,502 2142 [main] INFO testing.Test_TargetDataLine_01.App - 第一阶段 20:58:25,503 2143 [main] INFO testing.Test_TargetDataLine_01.App - 第 2 阶段 20:58:25,503 2143 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=true, Running=true 20:58:25,503 2143 [main] INFO testing.Test_TargetDataLine_01.App - 第 3 阶段 20:58:25,503 2143 [main] INFO testing.Test_TargetDataLine_01.App - 第 4 阶段 ... 20:58:25,512 2152 [main] INFO testing.Test_TargetDataLine_01.App - 第 48 阶段 20:58:25,512 2152 [main] INFO testing.Test_TargetDataLine_01.App - 第 49 阶段 20:58:25,512 2152 [main] INFO testing.Test_TargetDataLine_01.App - 停止 targetDataLine 20:58:25,513 2153 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=true, Running=true 20:58:25,521 2161 [main] INFO testing.Test_TargetDataLine_01.App - 睡眠 500 20:58:25,523 2163 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:25,533 2173 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false ... 20:58:26,003 2643 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:26,013 2653 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=true, Active=false, Running=false 20:58:26,021 2661 [main] INFO testing.Test_TargetDataLine_01.App - 关闭 targetDataLine 20:58:26,023 2663 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:26,026 2666 [main] INFO testing.Test_TargetDataLine_01.App - Sleeping 500 20:58:26,033 2673 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:26,043 2683 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false ... 20:58:26,513 3153 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false 20:58:26,523 3163 [观察者] INFO testing.Test_TargetDataLine_01.App - targetDataLine: Opened=false, Active=false, Running=false
这表明两个标志是无法区分的。