001/*
002 *   Copyright 2024 Vonage
003 *
004 *   Licensed under the Apache License, Version 2.0 (the "License");
005 *   you may not use this file except in compliance with the License.
006 *   You may obtain a copy of the License at
007 *
008 *        http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *   Unless required by applicable law or agreed to in writing, software
011 *   distributed under the License is distributed on an "AS IS" BASIS,
012 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *   See the License for the specific language governing permissions and
014 *   limitations under the License.
015 */
016package com.vonage.client.conversations;
017
018import com.fasterxml.jackson.annotation.JsonIgnore;
019import java.net.URI;
020import java.util.UUID;
021
022/**
023 * Represents an {@link EventType#AUDIO_PLAY} event.
024 */
025public final class AudioPlayEvent extends AudioOutEvent<AudioPlayEventBody> {
026
027    AudioPlayEvent() {}
028
029    private AudioPlayEvent(Builder builder) {
030        super(builder);
031        body = new AudioPlayEventBody(builder);
032    }
033
034    /**
035     * Unique audio play identifier.
036     *
037     * @return The play ID, or {@code null} if unknown.
038     */
039    @JsonIgnore
040    public UUID getPlayId() {
041        return body != null ? body.playId : null;
042    }
043
044    /**
045     * Source URL of the audio to play.
046     *
047     * @return The stream URL, or {@code null} if unspecified.
048     */
049    @JsonIgnore
050    public URI getStreamUrl() {
051        return body != null && body.streamUrl != null && body.streamUrl.length > 0 ? body.streamUrl[0] : null;
052    }
053
054    /**
055     * Entry point for constructing an instance of this class.
056     *
057     * @return A new Builder.
058     */
059    public static Builder builder() {
060        return new Builder();
061    }
062
063    public static final class Builder extends AudioOutEvent.Builder<AudioPlayEvent, Builder> {
064        URI[] streamUrl;
065
066        Builder() {
067            super(EventType.AUDIO_PLAY);
068        }
069
070        /**
071         * Source URL of the audio to play.
072         *
073         * @param streamUrl The stream URL as a string.
074         *
075         * @return This builder.
076         */
077        public Builder streamUrl(String streamUrl) {
078            return streamUrl(URI.create(streamUrl));
079        }
080
081        /**
082         * Source URL of the audio to play.
083         *
084         * @param streamUrl The stream URL.
085         *
086         * @return This builder.
087         */
088        public Builder streamUrl(URI streamUrl) {
089            this.streamUrl = new URI[]{streamUrl};
090            return this;
091        }
092
093        @Override
094        public AudioPlayEvent build() {
095            return new AudioPlayEvent(this);
096        }
097    }
098}