1

isRunning()in和isActibe()in和有什么不一样DataLine?当 line 实际处理数据时,这两个标志似乎都已设置。

更新

那里有2对方法:

1) 打开/关闭

2) 启动/停止

和三面旗帜

1) 是开放的

2) 正在运行

3) 活跃

任何人都可以推断出它们之间和/或彼此之间的一些关系吗?例如,很明显,isRunning 意味着 isOpen,isActive 意味着 isOpen,但 isActive 是否意味着 isRunning,反之亦然?或者他们是独立的?

我猜 Line在被调用isOpen后立即变为。检查是否已调用也是如此open()isOpenopen()

我可以以某种方式检查我是否打过电话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

这表明两个标志是无法区分的。

4

2 回答 2

1

如果您查看 API 文档,它会说:

isRunning():表示线路是否正在运行。默认值为假。当响应 start 方法的调用而呈现第一个数据时,一条开放行开始运行,并一直持续到响应停止调用或播放完成而停止呈现。

isActive():指示线路是否正在进行活动 I/O(例如播放或捕获)。当非活动线路变为活动线路时,它会向其侦听器发送一个 START 事件。类似地,当一条活动线路变为非活动线路时,它会发送一个 STOP 事件。

查看start()方法,我们看到start()允许线路变为非活动状态,因此我们可能会说,isRunning()告诉我们线路是否可以变为活动状态,并isActive()告诉我们线路是否正在使用中(即数据正在以播放的形式传输或记录)

所以,简而言之,

isRunning()start()-在我们使用这条线路之前我们需要打电话吗?

isActive()- 这条线路现在在使用吗?

如果您必须知道何时调用 start(),请使用以下命令:

final HashSet<Line> openLines = new HashSet<Line>();
...
DataLine myLine = getDataLine();
myLine.addListener(new LineListener(){
    public void update(LineEvent evt)
    {
        if(evt.getType().equals(LineEvent.Type.START)) openLines.add(evt.getLine());
        else if(evt.getType().equals(LineEvent.Type.STOP)) openLines.remove(evt.getLine());
    }
});

...
public boolean isLineStarted(DataLine l)
{
    return openLines.contains(l);
}
于 2012-07-08T15:12:18.480 回答
1

为了简化 Ryan 的回答: isRunning()只需检查线路是“在线”还是“运行”,或者基本上处于活动状态

isActive()检查线路是否正在主动写入或读取信息,无论是输入还是输出

日期线 JavaDoc:http ://docs.oracle.com/javase/1.5.0/docs/api/javax/sound/sampled/DataLine.html

isActive JavaDoc:http ://docs.oracle.com/javase/1.5.0/docs/api/javax/sound/sampled/DataLine.html#isActive%28%29

isRunning() JavaDoc:http ://docs.oracle.com/javase/1.5.0/docs/api/javax/sound/sampled/DataLine.html#isRunning%28%29

于 2012-10-05T16:26:14.983 回答