如何在iPad的Swift Playgrounds中播放自定义音频

In 2016, Apple released an iPad version of Swift Playgrounds. Paired with an external keyboard, it’s almost as good as Xcode and a lot faster, but it’s slightly oversimplified. Sure, you can write code, but what about adding files or playing audio?

2016年,苹果发布了iPad版本的Swift Playgrounds。 与外部键盘搭配使用时,它几乎与Xcode一样好,并且速度更快,但略有简化。 当然,您可以编写代码,但是添加文件或播放音频呢?

When I was coding my playground for this year’s Swift Student Challenge, I started out using my iPad. It was extremely fast. Pressing “Run My Code” would yield the result in less than a second! But I hit some snags — the most significant one being there was no way to play audio! My playground was structured like a game, and what kind of game has no music?

当我为今年的Swift Student Challenge编写操场代码时,我开始使用iPad。 速度非常快。 按“运行我的代码”将在不到一秒钟的时间内产生结果! 但是我遇到了一些障碍-最重要的是无法播放音频! 我的游乐场的结构就像一个游戏,什么样的游戏没有音乐?

I didn’t find a solution in time, so I eventually switched back to Xcode. But when I had time, I did some experiments and it turns out that it’s really, really easy to play audio! I’ll show you how in SwiftUI and also UIKit.

我没有及时找到解决方案,所以最终我切换回了Xcode。 但是当我有时间的时候,我做了一些实验,结果发现播放音频真的非常容易! 我将向您展示如何在SwiftUI和UIKit中使用。

1.将音频文件下载到文件中 (1. Download the Audio File Into Files)

You can get royalty-free audio files like this one from all over the internet:

您可以从整个互联网上获得像这样的免版税音频文件:

iPadOS added download support to Safari, so we can download directly into the built-in Files app. Here’s how:

iPadOS为Safari添加了下载支持,因此我们可以直接下载到内置的“文件”应用中。 这是如何做:

Image for post
Tap “Download”
点击“下载”
Image for post
Tap “mp3”
点按“ mp3”
Image for post
Tap “Download”
点击“下载”
Image for post
Tap the arrow-in-circle icon at the top right of your iPad
点击iPad右上角的圆圈箭头图标
Image for post
Tap the audio file
点按音频文件
Image for post
Tap the “Share” button
点击“共享”按钮
Image for post
Tap “Save to Files”
点击“保存到文件”
Image for post
Choose a location, then tap “Save”
选择一个位置,然后点击“保存”

Besides Safari, you could also Airdrop a file from your computer, use a USB stick, create your own music in GarageBand, etc. But whichever method you use to get an audio file, make sure to remember where you saved it.

除了Safari之外,您还可以从计算机上空投文件,使用USB记忆棒,在GarageBand中创建自己的音乐等。但是,无论使用哪种方法获取音频文件,请务必记住保存文件的位置。

2.将文件导入Swift游乐场 (2. Import the File Into Swift Playgrounds)

Open the Swift Playgrounds app, then either create a new playground or open an existing one. After that, to import the audio file:

打开Swift Playgrounds应用,然后创建一个新的游乐场或打开一个现有的游乐场。 之后,导入音频文件:

  1. Press the “+” icon.

    按下“ +”图标。
  2. Tap the paper icon.

    点击纸张图标。
  3. Tap “Insert From…”

    点击“从...插入”
Image for post

Then, select the file that you downloaded!

然后,选择您下载的文件!

Image for post

This will import your audio file into your playground project.

这会将您的音频文件导入到您的游乐场项目中。

3.播放音频! (3. Play the Audio!)

SwiftUI (SwiftUI)

Here’s the starter code for SwiftUI. Copy-and-paste into the same playground project where you imported your audio file:

这是SwiftUI的入门代码。 复制并粘贴到导入音频文件的同一运动场项目中:

Then, replace slow-spring-board with the name of your audio file. And that’s it! Press “Run My Code,” then tap “Play Audio!” and enjoy the music that you added!

然后,用音频文件的名称替换slow-spring-board 。 就是这样! 按“运行我的代码”,然后点按“播放音频!” 并享受您添加的音乐!

Image for post

The trick here is to use Bundle.main.url(forResource:withExtension:). You must do this. If you try adding a file literal via the “+” button (by tapping an already-imported file), it won’t work.

这里的技巧是使用Bundle.main.url(forResource:withExtension:) 。 您必须这样做。 如果您尝试通过“ +”按钮添加文件文字(通过点击已导入的文件),它将无法正常工作。

The last two lines are just as important:

最后两行同样重要:

let audioPlayerView = SwiftUIAudioPlayerView()
PlaygroundPage.current.setLiveView(audioPlayerView)

This will create an instance of the SwiftUI view and display it (I’ve tried playing audio without displaying a view, and it doesn’t work).

这将创建SwiftUI视图的实例并显示它(我尝试在不显示视图的情况下播放音频,但它不起作用)。

UIKit (UIKit)

The code for playing audio in UIKit is exactly the same as in SwiftUI, but it’s harder to create the UI. Here’s the starter code:

在UIKit中播放音频的代码与在SwiftUI中完全相同,但是创建UI更加困难。 这是入门代码:

Once again, replace slow-spring-board with the name of your audio file. Then press “Run My Code” and enjoy your music!

再次,用音频文件的名称替换slow-spring-board 。 然后按“运行我的代码”并欣赏您的音乐!

Image for post

Again, you must use Bundle.main.url(forResource:withExtension:), and the last two lines are necessary to display the view.

同样,您必须使用Bundle.main.url(forResource:withExtension:) ,最后两行对于显示视图是必需的。

let uikitAudioPlayerView = UIKitAudioPlayerView()
PlaygroundPage.current.setLiveView(uikitAudioPlayerView)

结论 (Conclusion)

Swift Playgrounds for iPad makes coding a quicker and more enjoyable experience, but it sometimes gets too simple. Features like audio playback are extremely hard to find. I scoured the internet and didn’t find a solution, so I had to experiment for myself.

适用于iPad的Swift Playgrounds使编码变得更快,更有趣,但是有时变得太简单了。 像音频播放这样的功能很难找到。 我搜寻了互联网,但没有找到解决方案,所以我不得不自己做实验。

But here it is! Figuring it out took some time, but I can now go back to coding on my iPad… and I got some article content to boot.

但是这里! 解决这个问题花费了一些时间,但是现在我可以回到在iPad上进行编码…,我可以启动一些文章内容。

Thanks for reading!

谢谢阅读!

翻译自: https://medium.com/better-programming/how-to-play-custom-audio-in-swift-playgrounds-for-ipad-77fe9798732b

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值