1

我在尝试将 scaletempo 与 Vala 的 playbin 一起使用时遇到了麻烦。我创建了 playbin,然后创建了一个 bin 来存储替换默认音频接收器的附加插件。下面的示例我从 pyTranscribe 中获取并转换为 Vala,但 Element.link_many 导致错误,我不太确定原因。

我会以正确的方式解决这个问题吗?有人有其他建议吗?

/* SoundPlayerBackend.vala */
/* Modified code from Damien Radtke's site. http://damienradtke.org/ */

using Gst;
public class SoundPlayerBackend {

    //Constants
    const double PLAYBACK_RATE_MODIFIER = 2.0;
    const int SEEK_SECONDS = 10;

    // Method delegates for notifying SoundPlayer about certain events
    protected delegate void NotifyEos();
    protected delegate void NotifyError(string message);

    // Pointer to our EOS delegate
    protected NotifyEos on_eos; 

    // Pointer to our Error delegate
    protected NotifyError on_error; 

    public static void main(string[] args){
        var soundplayer = new SoundPlayerBackend();
        Gst.init(ref args);
        soundplayer.setUri("file:///home/codenomad/Desktop/player-project/hurricane.mp3");
        soundplayer.play();
        string stop = stdin.read_line ();

        while (stop != "stop") {
            if (stop == "pause") { soundplayer.pause(); }
            else if (stop == "play") { soundplayer.play(); }
            stop = stdin.read_line ();
        }
    }

    // Read-only reference to the current sound object
    public dynamic Element sound { get; private set; } 

    // Read-only "is playing" property
    public bool is_playing { get; private set; default = false; } 

    // Read-only "rate" property
    public double rate { get; private set; default = 1; } 

    public void setUri(string uri) {
        // Make sure any existing allocated resources are freed
        if (sound != null)
            sound.set_state(Gst.State.NULL);
        sound = ElementFactory.make("playbin2", "playbin");        
        sound.uri = uri;
        var audiobin = new Bin("audioline");
        var scaletempo = ElementFactory.make("scaletempo", "scaletempo");
        var convert    = ElementFactory.make("audioconvert", "convert");
        var resample   = ElementFactory.make("audioresample", "resample");
        var audiosink = ElementFactory.make("autoaudiosink", "audiosink");

        audiobin.add_many(scaletempo, convert, resample, audiosink);

        //edited based on comment below
        //Element.link_many(scaletempo, convert, resample, audiosink);
        scaletempo.link_many(convert, resample, audiosink);

        var pad = scaletempo.get_pad("sink");
        audiobin.add_pad(new GhostPad("sink", pad));
        sound.set_property("audio-sink", audiobin);    
        sound.get_bus().add_watch(on_event);
    }

    // Play the sound
    public void play() {
        sound.set_state(State.PLAYING);
        print("Playing\n");
        is_playing = true;
    }

    // Pause it
    public void pause() {
        sound.set_state(State.PAUSED);
        is_playing = false;
        print("Paused\n");
    }

    // Event bus, listens for events and responds accordingly
    protected bool on_event(Gst.Bus bus, Message message) {
        switch (message.type) {
            case MessageType.ERROR:
                GLib.Error err;
                string debug;
                sound.set_state(Gst.State.NULL);
                is_playing = false;
                message.parse_error(out err, out debug);
                on_error(err.message);
                break;
            case MessageType.EOS:
                sound.set_state(Gst.State.READY);
                is_playing = false;
                on_eos();
                break;
            default:
                break;
        }
        return true;
    }
}

我尝试使用相同的代码使所有内容都变为静态并在下面收到相同的结果/错误:

SoundPlayerBackend.vala:121.9-121.67: error: Access to instance member `Gst.Element.link_many' denied
Element.link_many(scaletempo, convert, resample, audiosink);

提前致谢!

4

1 回答 1

1

那行应该是

scaletempo.link_many(convert, resample, audiosink);
于 2011-04-25T04:46:10.303 回答