To handle the situation you're describing, you'll want to check the current state of the media renderer before sending a new URL. The typical process in UPnP for controlling media playback involves querying the current state and then issuing the appropriate commands to transition the state if necessary. Here's a general outline of the steps you need to follow:
-
Get the Current Transport State: Use the GetTransportInfo action to retrieve the current state of the media renderer. This action will provide information such as whether the renderer is currently playing, paused, or stopped.
-
Handle Different States Appropriately:
- If the device is in the "PLAYING" state, you may want to send a "PAUSE" or "STOP" command before setting a new URI.
- If the device is in the "PAUSED_PLAYBACK" state and you want to switch to a new content, you can directly stop or set a new URI.
- If the device is in the "STOPPED" state, you can set the new URI directly.
-
Sending Control Actions: Before setting a new AVTransport URI, ensure that:
- If it's playing, either pause it or stop it first using appropriate UPnP commands (
AVTransport:Pause, AVTransport:Stop).
- Set the new URI only after ensuring the previous one is stopped/paused.
Here's a simplistic Kotlin approach to guide you through:
// Function to set new media URL
fun setNewMediaUrl(device: RemoteDevice, newUrl: String) {
val avTransportService = device.findService(AVTransport.SERVICE)
if (avTransportService != null) {
// Step 1: Get current transport state
getCurrentTransportState(avTransportService) { currentState ->
when (currentState) {
"PLAYING" -> {
// Send Stop or Pause command
sendControlAction(avTransportService, "Pause") {
// After pausing, set the new URI
setAVTransportUri(avTransportService, newUrl)
}
}
"PAUSED_PLAYBACK", "STOPPED" -> {
// Directly set the new URI
setAVTransportUri(avTransportService, newUrl)
}
else -> {
// Handle other states if necessary
}
}
}
}
}
// Method to send control actions like Stop, Pause
fun sendControlAction(service: Service<*, *>, action: String, callback: () -> Unit) {
val controlAction = when (action) {
"Pause" -> Pause(service)
"Stop" -> Stop(service)
else -> null
}
controlAction?.let { actionCall ->
upnpService.controlPoint.execute(actionCall.setCompletionListener { actionInvocation ->
if (actionInvocation.isFailed) {
// Handle failure
} else {
// Action succeeded, invoke callback
callback()
}
})
}
}
// Function to get the current transport state
fun getCurrentTransportState(service: Service<*, *>, result: (String) -> Unit) {
val getTransportInfoAction = GetTransportInfo(service)
upnpService.controlPoint.execute(getTransportInfoAction.setCompletionListener { actionInvocation ->
if (actionInvocation.isFailed) {
// Handle error
result("UNKNOWN")
} else {
val transportInfo = actionInvocation.getOutput("CurrentTransportState").value.toString()
result(transportInfo)
}
})
}
// Function to set new AV transport URI
fun setAVTransportUri(service: Service<*, *>, uri: String) {
val setTransportURIAction = SetAVTransportURI(service, uri)
upnpService.controlPoint.execute(setTransportURIAction)
}
In this example:
getCurrentTransportState: Queries the current transport state.
sendControlAction: Sends a control command like Pause or Stop.
setAVTransportUri: Sets a new transport URI for media playback.
Note: You need to properly handle asynchronous calls and network errors to ensure robust application behavior. Also, adapt the snippets to your full project setup as needed.