7

我有一个应用程序将捆绑的 m4a 音频文件加载为本地资源,并且它已经运行了很多年。我正在将应用程序更新到 iOS 11.3/XCode 9.3,当我按下播放按钮时,它现在在 iPad 上失败(适用于 iPhone):

2018-05-13 20:45:24.437626-0700 my app[6175:218735] App Transport Security has blocked a cleartext HTTP (http://) resource load since it is insecure. Temporary exceptions can be configured via your app's Info.plist file.
2018-05-13 20:45:24.437791-0700 my app[6175:218735] Cannot start load of Task <117E064E-ABB3-45F2-8D64-76397B140092>.<0> since it does not conform to ATS policy
2018-05-13 20:45:24.437948-0700 my app[6175:218732] NSURLConnection finished with error - code -1022

我的代码:

NSURL *medURL   = [[NSBundle mainBundle] URLForResource: @"MyFile"
                                          withExtension: @"m4a"];
NSError *playerError = nil;
AVAudioPlayer *newPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL: medURL error: &playerError];
self.appSoundPlayer = newPlayer;

// "Preparing to play" attaches to the audio hardware and ensures that playback
//      starts quickly when the user taps Play
[appSoundPlayer prepareToPlay];

它在prepareToPlay.

阅读其他答案我发现了很多关于 ATS 错误的信息,所以我将它添加到我的应用程序的 plist 文件中,清理、重建并运行它——但我仍然收到错误:

<key>NSAppTransportSecurity</key>
<dict>
    <key>NSAllowsLocalNetworking</key>
    <true/>
    <key>NSAllowsArbitraryLoadsForMedia</key>
    <true/>
    <key>NSAllowsArbitraryLoads</key>
    <true/>
    <key>NSAllowsArbitraryLoadsInWebContent</key>
    <true/>
    <key>NSExceptionAllowsInsecureHTTPLoads</key>
    <true/> </dict>

我不能使用 NSExceptionDomain 键,因为这是本地资源,没有域。我怎样才能解决这个问题?

注意:需要明确的是,此文件已捆绑到应用程序中,并且在任何时候都没有下载。这不是“http”网址,而是“文件:”网址。

更新:查看CFNetworkLog我意识到NSURLConnectionAppDelegate. 当我删除这个电话时,一切都开始工作了。由于两者都是独立工作的,在我看来存在某种冲突NSURLConnection——可能是 API 中的错误?很高兴知道正确的解决方法是什么,我已经通过删除prepareToPlay上面的调用让它工作得很好——这并不理想,因为当用户去播放音频文件时需要更长的时间。

另一个更新:该应用程序今天无缘无故开始工作,自上次尝试以来我没有更改任何内容。不幸的是,我无法确定此时修复是否有效!可以肯定的是,我的 plist 键似乎在我的 iOS 11.3 模拟器中工作。

4

1 回答 1

5

在我的项目中是使用 AVAudioPlayer 播放音频,而我的源代码:

NSString *soundFilePath = [[NSBundle mainBundle] pathForResource:@"MyFile"  ofType:@"m4a"];
NSURL *soundFileURL = [NSURL fileURLWithPath:soundFilePath];
AVAudioPlayer* audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:soundFileURL error:nil];
audioPlayer.numberOfLoops = -1; //Infinite
[audioPlayer setVolume:1];
[audioPlayer play];
于 2018-05-25T01:20:59.780 回答