### Using gRPC Client for Advanced Control with go-yaynison in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt This example initializes a gRPC client from the ynison_grpc package for handling protobuf messages in Yandex Music control. It connects with token and device ID, monitors player state like progress and tracks on messages, and toggles play/pause on connect. Dependencies include protobuf structures; inputs are state updates; outputs track details; runs for a fixed duration with potential disconnect handling. ```go package main import ( "fmt" "log" "time" ynisonGrpc "github.com/bulatorr/go-yaynison/ynison_grpc" "github.com/bulatorr/go-yaynison/ynisonstate" ) func main() { token := "your_oauth_token" deviceID := "grpc_controller_001" client := ynisonGrpc.NewClient(token, deviceID) defer client.Close() var progressMs, durationMs int64 var isPaused bool client.OnMessage(func(state *ynisonstate.PutYnisonStateResponse) { progressMs = state.PlayerState.Status.ProgressMs durationMs = state.PlayerState.Status.DurationMs isPaused = state.PlayerState.Status.Paused playableList := state.GetPlayerState().GetPlayerQueue().GetPlayableList() if len(playableList) > 0 { idx := state.GetPlayerState().GetPlayerQueue().GetCurrentPlayableIndex() track := playableList[idx] fmt.Printf("Now playing: %s\n", track.GetTitle()) fmt.Printf("Album: %s\n", track.GetAlbumIdOptional().GetValue()) fmt.Printf("Cover: %s\n", track.GetCoverUrlOptional().GetValue()) } }) client.OnConnect(func() { log.Println("gRPC client connected") // Toggle play/pause client.UpdatePlayingStatus(&ynisonstate.PlayingStatus{ ProgressMs: progressMs, DurationMs: durationMs, Paused: !isPaused, PlaybackSpeed: 1, }) }) client.OnDisconnect(func() { log.Println("gRPC client disconnected") }) if err := client.Connect(); err != nil { log.Fatal(err) } time.Sleep(5 * time.Second) } ``` -------------------------------- ### Retrieve Queue and Playlist Information using Go Yanison Client Source: https://context7.com/bulatorr/go-yaynison/llms.txt Shows how to connect to Yanison, listen for state messages, and print detailed queue and playlist information. Requires the go-yaynison library and a valid token. Outputs entity details, repeat mode, current index, track list, and shuffle status. ```go package main import ( "fmt" "log" "github.com/bulatorr/go-yaynison/ynison" ) func main() { client := ynison.NewClient("token") defer client.Close() client.OnMessage(func(state ynison.PutYnisonStateResponse) { queue := state.PlayerState.PlayerQueue fmt.Printf("Entity: %s (%s)\n", queue.EntityID, queue.EntityType) fmt.Printf("Repeat Mode: %s\n", queue.Options.RepeatMode) fmt.Printf("Current Index: %d\n", queue.CurrentPlayableIndex) fmt.Printf("Total Tracks: %d\n", len(queue.PlayableList)) // Display all tracks in queue for i, track := range queue.PlayableList { prefix := " " if i == queue.CurrentPlayableIndex { prefix = "▶ " } fmt.Printf("%s%d. %s (ID: %s)\n", prefix, i+1, track.Title, track.PlayableID) } // Show shuffle information if len(queue.ShuffleOptional.PlayableIndices) > 0 { fmt.Printf("Shuffle enabled with %d indices\n", len(queue.ShuffleOptional.PlayableIndices)) } }) if err := client.Connect(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Switching Active Device with go-yaynison in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt This code handles switching the active playback device in Yandex Music using the ynison library. It initializes a client with a token and device ID, logs active and available devices on messages, and switches via UpdateActiveDevice on connect. Inputs are device IDs; outputs include console prints of device info; requires online devices and valid token. ```go package main import ( "fmt" "log" "github.com/bulatorr/go-yaynison/ynison" ) func main() { client := ynison.NewClientWithDeviceID("token", "controller") defer client.Close() client.OnMessage(func(state ynison.PutYnisonStateResponse) { fmt.Printf("Active device: %s\n", state.ActiveDeviceIDOptional) // List all available devices for i, device := range state.Devices { fmt.Printf("%d: %s (%s) - Online: %v\n", i, device.Info.Title, device.Info.DeviceID, !device.IsOffline) } }) client.OnConnect(func() { // Switch to specific device newDeviceID := "target_device_id" client.UpdateActiveDevice(newDeviceID) log.Printf("Switched to device: %s\n", newDeviceID) }) if err := client.Connect(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Update Player State with Complete Queue via Go Yanison gRPC Client Source: https://context7.com/bulatorr/go-yaynison/llms.txt Creates a full player state object, including a playlist queue and playback status, then sends it to Yanison using the gRPC client. Requires the ynison_grpc and ynisonstate packages and a valid token/device ID. Logs confirmation after the update. ```go package main import ( "log" ynisonGrpc "github.com/bulatorr/go-yaynison/ynison_grpc" "github.com/bulatorr/go-yaynison/ynisonstate" ) func main() { client := ynisonGrpc.NewClient("token", "device_id") defer client.Close() client.OnConnect(func() { // Create player state update playerState := &ynisonstate.PlayerState{ PlayerQueue: &ynisonstate.PlayerQueue{ CurrentPlayableIndex: 0, EntityType: ynisonstate.PlayerQueue_PLAYLIST, PlayableList: []*ynisonstate.Playable{ { PlayableId: "12345", PlayableType: ynisonstate.Playable_TRACK, Title: "Song Title", }, }, Options: &ynisonstate.PlayerStateOptions{ RepeatMode: ynisonstate.PlayerStateOptions_ONE, }, EntityContext: ynisonstate.PlayerQueue_BASED_ON_ENTITY_BY_DEFAULT, }, Status: &ynisonstate.PlayingStatus{ Paused: false, ProgressMs: 0, DurationMs: 180000, PlaybackSpeed: 1, }, } client.UpdatePlayerState(playerState) log.Println("Updated player state") }) if err := client.Connect(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Monitor Yandex Music Playback in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt Creates a basic WebSocket client to monitor Yandex Music playback state in real-time without remote control capabilities. Requires a Yandex OAuth token for authentication. Outputs event-driven updates on track info, progress, pause status, and connection events. Ideal for logging or displaying current playback state from Yandex devices. ```go package main import ( "fmt" "log" "os" "os/signal" "time" "github.com/bulatorr/go-yaynison/ynison" ) func main() { // Create client with OAuth token token := "your_yandex_oauth_token" client := ynison.NewClient(token) defer client.Close() // Set up message handler for playback updates client.OnMessage(func(state ynison.PutYnisonStateResponse) { current, _ := time.ParseDuration(state.PlayerState.Status.ProgressMs + "ms") total, _ := time.ParseDuration(state.PlayerState.Status.DurationMs + "ms") fmt.Printf("Request ID: %s\n", state.Rid) fmt.Printf("Paused: %v\n", state.PlayerState.Status.Paused) if len(state.PlayerState.PlayerQueue.PlayableList) > 0 { track := state.PlayerState.PlayerQueue.PlayableList[state.PlayerState.PlayerQueue.CurrentPlayableIndex] fmt.Printf("Track: %s (ID: %s)\n", track.Title, track.PlayableID) fmt.Printf("Source: %s\n", track.From) } fmt.Printf("Progress: %v / %v\n", current, total) }) // Set up connection handlers client.OnConnect(func() { log.Println("Connected to Ynison") }) client.OnDisconnect(func() { log.Println("Disconnected from Ynison") }) // Connect and wait for interrupt if err := client.Connect(); err != nil { log.Fatal(err) } interrupt := make(chan os.Signal, 1) signal.Notify(interrupt, os.Interrupt) <-interrupt } ``` -------------------------------- ### Monitoring Device Capabilities with go-yaynison in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt This snippet queries and displays device capabilities and session info for Yandex Music using the ynison client. It connects with a token, processes device states on messages to print details like ID, type, capabilities, volume, and session. Requires a valid token; outputs formatted device info to console; limited to connected devices. ```go package main import ( "fmt" "log" "github.com/bulatorr/go-yaynison/ynison" ) func main() { client := ynison.NewClient("token") defer client.Close() client.OnMessage(func(state ynison.PutYnisonStateResponse) { for _, device := range state.Devices { fmt.Printf("\nDevice: %s\n", device.Info.Title) fmt.Printf(" ID: %s\n", device.Info.DeviceID) fmt.Printf(" Type: %s\n", device.Info.Type) fmt.Printf(" App: %s v%s\n", device.Info.AppName, device.Info.AppVersion) fmt.Printf(" Can Be Player: %v\n", device.Capabilities.CanBePlayer) fmt.Printf(" Can Be Remote: %v\n", device.Capabilities.CanBeRemoteController) fmt.Printf(" Volume: %.2f\n", device.VolumeInfo.Volume) fmt.Printf(" Offline: %v\n", device.IsOffline) fmt.Printf(" Session: %s\n", device.Session.ID) } }) if err := client.Connect(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Handle Connection Events and Automatic Reconnection in Go Yanison Client Source: https://context7.com/bulatorr/go-yaynison/llms.txt Demonstrates setting up connection, disconnection, and message handlers with reconnection logic for transient failures. Utilizes go-yaynison client, logs status updates, and caps reconnection attempts. Suitable for resilient long‑running applications. ```go package main import ( "log" "time" "github.com/bulatorr/go-yaynison/ynison" ) func main() { token := "token" var client *ynison.Client reconnectAttempts := 0 maxReconnects := 5 connect := func() error { client = ynison.NewClient(token) client.OnConnect(func() { log.Println("✓ Connected to Ynison") reconnectAttempts = 0 }) client.OnDisconnect(func() { log.Println("✗ Disconnected from Ynison") if reconnectAttempts < maxReconnects { reconnectAttempts++ waitTime := time.Duration(reconnectAttempts*2) * time.Second log.Printf("Reconnecting in %v (attempt %d/%d)...\n", waitTime, reconnectAttempts, maxReconnects) time.Sleep(waitTime) if err := connect(); err != nil { log.Printf("Reconnection failed: %v\n", err) } } else { log.Println("Max reconnection attempts reached") } }) client.OnMessage(func(state ynison.PutYnisonStateResponse) { if !client.IsConnected() { log.Println("Warning: received message but not connected") return } // Process message }) return client.Connect() } if err := connect(); err != nil { log.Fatal(err) } defer func() { if client != nil { client.Close() } }() } ``` -------------------------------- ### Control Remote Yandex Music Devices in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt Initializes a gRPC-based client with a custom device ID for remote control capabilities in Yandex Music. Requires both a Yandex OAuth token and a unique device ID string. Enables advanced remote interactions such as controlling playback on other devices. Handles connection events and allows checking connection status post-initialization. ```go package main import ( "log" "github.com/bulatorr/go-yaynison/ynison" ) func main() { token := "your_yandex_oauth_token" deviceID := "custom_device_id_123" // Client with device ID supports remote control client := ynison.NewClientWithDeviceID(token, deviceID) defer client.Close() client.OnConnect(func() { log.Println("Remote controller connected") }) if err := client.Connect(); err != nil { log.Fatal(err) } // Check connection status if client.IsConnected() { log.Println("Successfully connected as remote controller") } } ``` -------------------------------- ### Send Custom Raw Protocol Message in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt This Go code snippet demonstrates how to send a custom raw protocol message using the go-yaynison gRPC client. It constructs a `PutYnisonStateRequest` to update an active device and sends it over the established client connection. This is useful for advanced use cases requiring direct manipulation of device states or sending specific commands not covered by higher-level abstractions. Ensure the `go-yaynison` library and its dependencies are correctly imported. ```go package main import ( "log" ynisonGrpc "github.com/bulatorr/go-yaynison/ynison_grpc" "github.com/bulatorr/go-yaynison/ynisonstate" "google.golang.org/protobuf/types/known/wrapperspb" ) func main() { client := ynisonGrpc.NewClient("token", "device") defer client.Close() client.OnConnect(func() { // Create custom request request := &ynisonstate.PutYnisonStateRequest{ Parameters: &ynisonstate.PutYnisonStateRequest_UpdateActiveDevice{ UpdateActiveDevice: &ynisonstate.UpdateActiveDevice{ DeviceIdOptional: wrapperspb.String("target_device"), }, }, Rid: "custom-request-id-001", PlayerActionTimestampMs: "1699200000000", ActivityInterceptionType: "DO_NOT_INTERCEPT_BY_DEFAULT", } client.Send(request) log.Println("Sent raw request") }) if err := client.Connect(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Adjusting Device Volume with go-yaynison in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt This snippet demonstrates controlling volume levels on Yandex Music devices using the ynison client. It requires an OAuth token and device ID for initialization, connects to the service, and updates volume via the UpdateVolumeInfo method. Inputs include volume values (0.0 to 1.0); outputs are log messages confirming changes; limitations include needing an active connection and available devices. ```go package main import ( "log" "github.com/bulatorr/go-yaynison/ynison" ) func main() { client := ynison.NewClientWithDeviceID("token", "controller_device") defer client.Close() var targetDeviceID string client.OnMessage(func(state ynison.PutYnisonStateResponse) { // Get first available device if len(state.Devices) > 0 { targetDeviceID = state.Devices[0].Info.DeviceID } }) client.OnConnect(func() { // Set volume to 50% client.UpdateVolumeInfo(targetDeviceID, 0.5) log.Println("Set volume to 50%") // Set volume to 100% client.UpdateVolumeInfo(targetDeviceID, 1.0) log.Println("Set volume to 100%") // Mute (set to 0%) client.UpdateVolumeInfo(targetDeviceID, 0.0) log.Println("Muted device") }) if err := client.Connect(); err != nil { log.Fatal(err) } } ``` -------------------------------- ### Update Yandex Music Playback Status in Go Source: https://context7.com/bulatorr/go-yaynison/llms.txt Demonstrates updating playback state in Yandex Music, including pause, resume, and seek operations using a remote controller client. Assumes a connected client with device ID; listens for state messages to track progress and duration. Sends commands to control playback remotely, such as pausing, resuming, or seeking to a specific time. Limitations include reliance on existing connection and accurate state reception for updates. ```go package main import ( "log" "github.com/bulatorr/go-yaynison/ynison" ) func main() { client := ynison.NewClientWithDeviceID("token", "my_remote_device") defer client.Close() var currentProgress, currentDuration string client.OnMessage(func(state ynison.PutYnisonStateResponse) { currentProgress = state.PlayerState.Status.ProgressMs currentDuration = state.PlayerState.Status.DurationMs }) client.OnConnect(func() { // Pause playback client.UpdatePlayingStatus(true, currentProgress, currentDuration) log.Println("Paused playback") // Resume playback client.UpdatePlayingStatus(false, currentProgress, currentDuration) log.Println("Resumed playback") // Seek to 30 seconds client.UpdatePlayingStatus(false, "30000", currentDuration) log.Println("Seeked to 30 seconds") }) if err := client.Connect(); err != nil { log.Fatal(err) } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.