question

newuser-98a88166-9c07-45bd-a735-2ed52e7371a4 avatar image
newuser-98a88166-9c07-45bd-a735-2ed52e7371a4 asked ·

Playback nearly finsihed function updates the list of mp3 links when the second request is initiated and that affects the playlist of the other user

I have a variable in the playback nearly finished function in the lambda which gets data from the external API. This variable is then played from its first link to last but when a new user launches the request, this variable gets updated with that value and even the first user then gets that different playlist of the second user.


class PlaybackNearlyFinishedEventHandler(AbstractRequestHandler):

"""AudioPlayer.PlaybackNearlyFinished Directive received.

Replacing queue with the URL again. This should not happen on live streams.

"""

def can_handle(self, handler_input):

# type: (HandlerInput) -> bool

return is_request_type("AudioPlayer.PlaybackNearlyFinished")(handler_input)


def handle(self, handler_input):

# type: (HandlerInput) -> Response

logger.info("In PlaybackNearlyFinishedHandler")

#global total_links

try_var=ob1.get_x()


persistent_attr = handler_input.attributes_manager.persistent_attributes

playback_info = persistent_attr.get("playback_info")

playback_setting = persistent_attr.get("playback_setting")


if playback_info.get("next_stream_enqueued"):

return handler_input.response_builder.response



test_var=handler_input.attributes_manager.persistent_attributes["total_links"]


user_id = handler_input.request_envelope.context.system.user.user_id

user_var=""

user_var+=user_id

response = table_user.get_item(

Key={

'UserID': user_id,

'links': 'test'


}

)

item = response['Item']

print(item)

# This value gets updated when a second user initiates the alexa request.

a_dict={}

a_dict[user_id]=item['info']

print(a_dict[user_id])


test_link=item['info']

total_links=test_link



playback_info["next_stream_enqueued"] = True




count_local= int(playback_info.get("token"))

count_local+=1

expected_previous_token = int(playback_info.get("token"))


playback_info["token"] = int(playback_info.get("token"))

playback_info["token"]+=1

enqueue_token = playback_info.get("token")

play_behavior = PlayBehavior.ENQUEUE

url = a_dict[user_id][count_local]


offset_in_ms = 0


handler_input.response_builder.add_directive(

PlayDirective(

play_behavior=play_behavior,

audio_item=AudioItem(

stream=Stream(

token=enqueue_token,

url=url,

offset_in_milliseconds=offset_in_ms,

expected_previous_token=expected_previous_token),

metadata=None)))


return handler_input.response_builder.response





lambdaaudioplayerplaybackcontroller
10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

Andy Whitworth avatar image
Andy Whitworth answered ·

I'm not a python developer but this sounds like a simple variable scope problem. You need to ensure that you're not updating global variables as they will be shared across multiple user invocations of your lambda. Store playlists etc in the persistent storage associated with each individual user request.

10 |2000 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.