iOS如何使用nativescript-audio插件播放webradio流媒体?

时间:2018-01-15 12:33:26

标签: ios nativescript

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

  

sharedSession.dataTaskWithUrlCompletionHandler(URL,function(data,   响应,错误))返回错误= {}

以下是 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();
    });
}

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

1 个答案:

答案 0 :(得分:1)

最后,我找到了解决方案。如前所述,我用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);
            }
        });
    };

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