Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to convert the binary obtained from the server into a playable Data type? #578

Open
koi646 opened this issue Jun 18, 2024 · 2 comments
Labels
kind/support Adopter support requests. status/triage Collecting information required to triage the issue.

Comments

@koi646
Copy link

koi646 commented Jun 18, 2024

Question

When I use URLSession, there are many examples,butI looked at the official example, didn't find how to convert the binary downloaded from the server into a data structure that can be used by AudioPlayer.
image
image
When I printed out audio_mpeg, I found that it is of type OpenAPIRuntime.HTTPBody.
How can I convert OpenAPIRuntime.HTTPBody type to be used by AudioPlayer?

@koi646 koi646 added kind/support Adopter support requests. status/triage Collecting information required to triage the issue. labels Jun 18, 2024
@czechboy0
Copy link
Collaborator

czechboy0 commented Jun 18, 2024

Hi @koi646,

you turn an HTTPBody (a stream of byte chunks) into Data by accumulating it using this initializer: https://swiftpackageindex.com/apple/swift-openapi-runtime/1.4.0/documentation/openapiruntime/foundation/data/init(collecting:upto:)

So something like:

let audioData = Data(collecting: audio_mpeg, upTo: 10 * 1024 * 1024) // chose max of 10MB, adjust as needed

From this point on, converting the Data value to something compatible with AudioPlayer, the story is the same as when using URLSession directly. So for example: https://developer.apple.com/documentation/avfaudio/avaudioplayer/1388809-init

@koi646
Copy link
Author

koi646 commented Jun 19, 2024

thx, I will give it a try.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Adopter support requests. status/triage Collecting information required to triage the issue.
Projects
None yet
Development

No branches or pull requests

2 participants