实现流程(基于 K 歌组件)

更新时间: 2023/11/15 09:43:31

K 歌组件包括了 UI Kit 以及 K 歌、演唱评分、逐字逐行歌词、点歌搜索、跳过前奏等能力,您只需要简单调用几个接口,就能为您的应用添加在线 K 歌业务。

开发环境要求

开发环境要求如下:

环境要求 说明
iOS 版本 11.0 及以上
CPU 架构 ARM64、ARMV7
IDE XCode
其他 安装 CocoaPods。

集成 NEKaraokeKit 组件

步骤1 通过CocoaPods集成 Karaoke 组件

请确保您的 Mac 已经安装 Ruby 环境。

  1. 创建项目。

    若您需要创建新项目,在 XCode 里,依次选择 Create a New XCode Project > Single View App > Next 新建工程,再配置工程相关信息和合适的工程本地路径,单击 Create

    image image
  2. 安装 CocoaPods。

    在终端窗口中输入如下命令:

    sudo gem install cocoapods
    
  3. 创建 Podfile 文件。

    从 Terminal 中进入您所创建项目所在路径,运行以下命令创建 Podfile 文件。

    pod init
    
  4. 编辑 Podfile 文件。

    # platform :ios, '9.0' 
    target '{YourApp}' do
        pod 'NEKaraokeKit', '~> {version}'
    end
    
    • YourApp:您的 Target 名称。
    • version:待集成的 NEKaraokeKit 组件的版本号。
  5. 执行以下命令查询本地库版本。

    pod search NEKaraokeKit         
    

    若不是最新版本,可以执行以下命令更新本地库版本。

    pod repo update
    
  6. 执行以下命令安装 SDK。

    pod install        
    

步骤2 配置权限

使用 SDK 的音视频功能,需要授权麦克风使用权限。请在 App 的 info.plist 中设置以下项:

Privacy - Microphone Usage Description,并填入麦克风使用目的提示语。

初始化组件

在调用 SDK 其他接口之前,您首先需要完成初始化操作。

  1. 配置初始化相关参数。

    let config = NEKaraokeKitConfig()
    config.appKey = "你的appKey"
    

    应用appKey 请替换为您的应用对应的 App Key,获取方法请参见获取 App Key

  2. 调用 initialize 方法完成初始化操作。

    该接口无额外回调结果数据。

    NEKaraokeKit.shared().initialize(config: config) { code, msg, _ in 
        if code == 0 {
            print("初始化成功")
        } else {
            print("初始化失败: \(msg ?? "")")
        }         
    }
    
    

登录鉴权

请求 SDK 进行登录鉴权,您只有完成 SDK 登录鉴权才可以创建 K 歌房间。

  1. 请在登录前先获取账号 ID 和 Token。账号信息由 K 歌服务器下发,App 通过业务服务器向 K 歌服务器获取,需要您自行实现相关业务逻辑。

    let accountId: String = "accountId";
    let accountToken: String = "accountToken";
    
  2. 调用 login 方法登录并进行回调处理。该接口无额外回调结果数据。

    NEKaraokeKit.shared().login(accountId, token:accountToken) { code, msg, _ in
        if code == 0 {
            print("登录成功")
        } else {
            print("登录失败: \(msg ?? "")")
        }                 
    }
    
    

实现在线 KTV

步骤1 房主创建房间。

在已经完成 SDK 登录鉴权的状态下,创建并开始一个K歌的房间。

let params = NECreateKaraokeParams()
params.title = "房间名称"
params.nick = "昵称"
params.seatCount = 8
params.singMode = 0
let options = NECreateKaraokeOptions()

NEKaraokeKit.shared().createRoom(params, options: options) { code, msg, data in
    if code == 0 {
        print("创建房间成功")
    } else {
        print("创建房间失败: \(msg ?? "")")
    }
}

NECreateKaraokeParams 的相关参数如下表所示。

参数 是否必选 类型 描述
title String 房间名称
nick String 昵称
seatCount int 麦位个数
singMode int 0:智能合唱, 1: 串行合唱,2:NTP实时合唱,不传默认为智能合唱(根据网络,设备等情况智能选择串行还是实时合唱)
extraData String 预留扩展字段

步骤2 加入房间。

    let params = NEJoinKaraokeParams()
    params.nick = "昵称"
    params.roomUuid = "房间id"
    params.role = 房主 ? .host : .audience
    params.liveRecordId = "直播Id"

    let options = NEJoinKaraokeOptions();
    NEKaraokeKit.shared().joinRoom(params, options: options) { code, msg, roomInfo in
        if code == 0 {
            print("加入房间成功")
        } else {
            print("加入房间失败: \(msg ?? "")")
        }
    }

NEJoinKaraokeParams 的相关参数如下表所示。

参数 是否必选 类型 描述
roomUuid String 房间Id
nick String 昵称
role NEKaraokeRole 角色,创建者为host
liveRecordId long K歌房间直播id
extraData String 预留扩展字段

步骤3 上麦

  1. 观众申请上麦。

    NEKaraokeKit.shared().requestSeat { code, msg, _ in
        if code == 0 {
            print("上麦成功")
        } else {
            print("上麦失败: \(msg ?? "")")
        }
    }
    
  2. 房主同意上麦。

    let account = “用户id”
    NEKaraokeKit.shared().approveRequestSeat(account: account) { code, msg, _ in
        if code == 0 {
            print("同意上麦成功")
        } else {
            print("同意上麦失败: \(msg ?? "")")
        }        
    }
    
  3. (可选)观众取消上麦。

    NEKaraokeKit.shared().cancelRequestSeat { code, msg, _ in
        if code == 0 {
            print("取消上麦成功")
        } else {
            print("取消上麦失败: \(msg ?? "")")
        }    
    }
    
  4. 查看麦位信息。

    NEKaraokeKit.shared().getSeatInfo { code, msg, seatInfo in
        if code == 0 {
            print("查看麦位信息成功")
        } else {
            print("查看麦位信息失败: \(msg ?? "")")
        }    
    }
    

步骤4 播放音乐并体验 K 歌

  1. 开始播放。

    let originPath: String = "原唱文件地址"
    let accompanyPath: String = “伴奏文件地址”
    let volumn: Int = 50 // 播放音量
    let anchorAccount: String = “主唱用户账号”
    let chorusAccount: String = “合唱用户账号”
    let startTimeStamp: Int64 = 3000 // 延迟多少秒开始播放
    let anchor: Bool = true // 当前paly用户是否时主唱
    let mode: NEKaraokeSongMode = .intelligencce // 唱歌模式
    NEKaraokeKit.share().requestPlaySong(originPath: originPath,
                                   accompanyPath: accompanyPath,
                                   volumn: volumn,
                                   anchorAccount: anchorAccount,
                                   chorusAccount: chorusAccount,
                                   startTimeStamp: startTimeStamp,
                                   anchor: anchor,
                                   mode: mode) { code, msg, _ in
        if code == 0 {
            print("开始播放成功")
        } else {
            print("开始播放失败: \(msg ?? "")")
        } 
    }
    
  2. 停止播放。

    NEKaraokeKit.shared().requestStopPlayingSong { code, msg, _ in
        if code == 0 {
            print("停止播放成功")
        } else {
            print("停止播放失败: \(msg ?? "")")
        } 
    }
    
    

步骤5 发送和接收礼物

  1. 观众调用 NEKaraokeKit.sendGift 接口发送礼物。参数说明如下表所示。

    参数 是否必选 类型 描述
    gift int 礼物 ID
    NEKaraokeKit.shared().sendGift(giftId) { code, msg, _ in 
        if code == 0 {
            print("发送礼物成功")
        } else {
            print("发送礼物失败: \(msg ?? "")")
        } 
    })
    
    
  2. 接收礼物。

    通过注册 NEKaraokeListener 监听,在回调方法onReceiveGift 接收礼物相关信息。

    NEKaraokeKit.shared().addKaraokeListener(self)
    
    
    func onReceiveGift(giftModel: NEKaraokeGiftModel) {
        /// 接收礼物
    }
    

步骤6 发送和接收文字聊天消息

  1. 调用 NEKaraokeKit.sendTextMessage 接口发送聊天消息。

    let msg: String = “消息”
    NEKaraokeKit.shared().sendTextMessage(msg) { code, msg, _ in
        if code == 0 {
            print("发送文字消息成功")
        } else {
            print("发送文字消息失败: \(msg ?? "")")
        } 
    }
    
    
  2. 调用 NEKaraokeListener 接口,通过 onReceiveTextMessage 回调方法接收聊天消息。

    NEKaraokeKit.getInstance().addKaraokeListener(self)
    
    func onReceiveTextMessage(_ message: NEKaraokeChatTextMessage) {
        // 文本消息
    }
    

步骤7 离开房间。

    NEKaraokeKit.shared().leaveRoom { code, msg, _ in
        if code == 0 {
            print("离开房间成功")
        } else {
            print("离开房间失败: \(msg ?? "")")
        }
    }

步骤8 结束房间。

只有房主有该权限。

NEKaraokeKit.shared().endRoom { code, msg, _ in
    if code == 0 {
        print("结束房间成功")
    } else {
        print("结束房间失败: \(msg ?? "")")
    }
}
此文档是否对你有帮助?
有帮助
去反馈
  • 开发环境要求
  • 集成 NEKaraokeKit 组件
  • 步骤1 通过CocoaPods集成 Karaoke 组件
  • 步骤2 配置权限
  • 初始化组件
  • 登录鉴权
  • 实现在线 KTV
  • 步骤1 房主创建房间。
  • 步骤2 加入房间。
  • 步骤3 上麦
  • 步骤4 播放音乐并体验 K 歌
  • 步骤5 发送和接收礼物
  • 步骤6 发送和接收文字聊天消息
  • 步骤7 离开房间。
  • 步骤8 结束房间。