gpt4 book ai didi

iOS 如何使用nativescript-audio插件播放网络广播流?

转载 作者:行者123 更新时间:2023-12-01 15:55:44 25 4
gpt4 key购买 nike

我正在使用 nativescript 开发网络广播应用程序和 nativescript-audio 插件来读取流。在 Android 上我没有问题,但在 iOS 上方法:

sharedSession.dataTaskWithUrlCompletionHandler(URL, function(data, response, error)) return with error = {}

这是我的Info.plist的一部分

<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>radioking.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
<key>NSTemporaryExceptionMinimumTLSVersion</key>
<string>TLSv1.1</string>
</dict>
</dict>
</dict>

这是我的网址:https://www.radioking.com/play/jobradio码流格式为mp3

插件调用:

private player  = new TNSPlayer();

public initFromUrl(url : string, autoPlay : boolean = false) {
// Initialize player
this.player.initFromUrl({
audioFile: url,
loop: false,
completeCallback: () => {
this.player.dispose().then(() => { });
},
errorCallback: args => { },
infoCallback: args => { }
}).then(() => {
if (autoPlay) this.player.play();
});
}

有人可以解释一下我出了什么问题吗?谢谢

最佳答案

终于,我找到了解决办法。正如前面所写,我用 AVPlayer 替换了 AVAudioPlayer。

Info.plist中的有用信息是:

	<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
<key>NSExceptionDomains</key>
<dict>
<key>radioking.com</key>
<dict>
<key>NSIncludesSubdomains</key>
<true/>
<key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
<true/>
</dict>
</dict>
</dict>

在插件中,我将 playFromUrl 中的所有代码替换为:

    TNSPlayer.prototype.playFromUrl = function (options) {
var _this = this;
_this._completeCallback = options.completeCallback;
_this._errorCallback = options.errorCallback;
_this._infoCallback = options.infoCallback;

return new Promise(function (resolve, reject) {
if (options.autoPlay !== false) {
options.autoPlay = true;
}
try {
var audioSession = AVAudioSession.sharedInstance();
var output = audioSession.currentRoute.outputs.lastObject.portType;
if (output.match(/Receiver/)) {
try {
audioSession.setCategoryError(AVAudioSessionCategoryPlayAndRecord);
audioSession.overrideOutputAudioPortError(AVAudioSessionPortOverrideSpeaker);
audioSession.setActiveError(true);
common_1.TNS_Player_Log("audioSession category set and active");
}
catch (err) {
common_1.TNS_Player_Log("setting audioSession category failed");
}
}
_this._player = AVPlayer.alloc().initWithURL(NSURL.URLWithString(options.audioFile));
if (_this._player) {
_this._player.delegate = _this;
common_1.TNS_Player_Log("this._player", _this._player);
_this._player.enableRate = true;
_this._player.numberOfLoops = options.loop ? -1 : 0;
if (options.metering) {
common_1.TNS_Player_Log("enabling metering...");
_this._player.meteringEnabled = true;
}
if (options.autoPlay) {
_this._player.play();
}
resolve();
} else {
reject();
}
}
catch (ex) {
if (_this._errorCallback) {
_this._errorCallback({ ex: ex });
}
reject(ex);
}
});
};

使用此代码,所有 Controller 操作都将继续工作。在线 mp3 和网络流媒体正常工作。

关于iOS 如何使用nativescript-audio插件播放网络广播流?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48263145/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com