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

Ken*_*Ken 2 ios nativescript

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

sharedSession.dataTaskWithUrlCompletionHandler(URL, function(data, response, 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>
Run Code Online (Sandbox Code Playgroud)

这是我的网址: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();
    });
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释我有什么问题吗?谢谢

Ken*_*Ken 5

最后,我找到了解决方案。正如前面所写,我用 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>
Run Code Online (Sandbox Code Playgroud)

在插件中,我将 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);
            }
        });
    };
Run Code Online (Sandbox Code Playgroud)

使用此代码,所有控制器操作都可以继续工作。在线 mp3 和网络流媒体工作正常。