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

标签 ios nativescript

我正在使用 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/

相关文章:

ios - 在不更改状态栏颜色的情况下呈现 View Controller ,例如 UIAlertController

ios - 设置 UIPrintPageRenderer/UIPrintInteractionController 要打印的页面

javascript - 如何将视频通话添加到 nativescript 应用程序

java - 如何将 Mapbox GL Native Android Activity 示例 Java 应用程序翻译成 NativeScript?

ios - 使用“使用应用程序专用密码登录”到iOS商店发布失败

iphone - 在 UITableViewCell 的父类中使用 XIB

ios - UIbarbuttonitem 出现导致崩溃

ios - 防止在 shouldChangeTextInRange 期间删除前缀

css - Nativescript 如何溢出文本?

ios - Angular Nativescript - 在 AbsoluteLayout 中使用 ScrollView