php 七牛云实现直播功能(二)

本文详细介绍如何使用七牛云SDK实现直播功能,包括创建直播空间、获取推流和播放地址,以及通过PHP代码更新直播间状态的过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天域名备案终于好了,开始搞第二步,实现直播功能,移动端可以参考七牛云SDK,下面是服务端推流案例,本次使用的是rtmp流实现直播,在控制台找到直播云服务,创建直播云空间
在这里插入图片描述
创建好直播空间后会生成几个二级域名,按需要将域名解析出来,然后就到了下面的样子
在这里插入图片描述
代码运行起来后会在直播流中看到你说创建的直播流播放历史等信息
在这里插入图片描述
安装composer包

php composer.phar require qiniu/php-sdk

再vendor/pili-engineering/pili-sdk-php.v2里能找到两个案例,一个是直播的,一个是连麦的,这次先实现直播,下一篇再更新一下连麦

<?php

namespace App\Modules\Api\Http\Controllers;

use App\Modules\Live\Models\Broadcast;
use App\Modules\Live\Repositories\BroadcastRepositoryEloquent;
use Illuminate\Http\Request;
use Qiniu\Pili\Client;
use Qiniu\Pili\Mac;
use function Qiniu\Pili\RTMPPlayURL;
use function Qiniu\Pili\RTMPPublishURL;
use function Qiniu\Pili\SnapshotPlayURL;

class LiveController extends ApiBaseController
{
    private $auth;
    private $accessKey;
    private $secretKey;
    private $hubName;

    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->accessKey = config("qiniu.accessKey");
        $this->secretKey = config("qiniu.secretKey");
        $this->hubName = config("qiniu.bucket");
        parent::__construct();
    }
    /**
     *开启直播
     */
    public function liveStart(Request $request)
    {
        $userInfo = parent::getAuthenticatedUser($msg);
        if (isset($userInfo['user']) && !empty($userInfo['user'])) {
            $request->offsetSet('user_id', $userInfo['user']['id']);
        } else {
            return $this->sendResponse($msg, 'error', '', 401);
        }
        $data = $request->all();
        $broadcast = app(BroadcastRepositoryEloquent::class)->findWhere(['type' => $data['type'], 'user_id' => $data['user_id']])->first();
        if (empty($broadcast)) {
            return $this->sendResponse(trans('admin.operate_failed') . '未找到直播间');
        }
        $broadcast['name'] = $data['name'];
        //创建hub
        $mac = new Mac($this->accessKey, $this->secretKey);
        $client = new Client($mac);
        $hub = $client->hub($this->hubName);
        //获取stream
        $streamKey = $broadcast['show_id'];
        $stream = $hub->stream($streamKey);
        $list = $hub->listStreams($streamKey, 1, "");
        //如果没找到对应的直播流创建新直播流
        if (count($list['keys']) == 0) {
            //获取stream
            $hub->create($streamKey);
        }
        if ($data['type'] == 0) {
            $result = $this->updateShop($broadcast, $streamKey, $msg);
            if ($result == false) {
                return $this->sendResponse(trans('admin.operate_failed') . $msg);
            }
        } else {
            $result = $this->updateCurriculum($broadcast, $streamKey, $msg);
            if ($result == false) {
                return $this->sendResponse(trans('admin.operate_failed') . $msg);
            }
        }
        return $this->sendResponse(trans('admin.operate_succeeded'), 'succ', ['p_href' => $broadcast['p_href']]);
    }

    //更新商城直播间
    public function updateShop($broadcast, $streamKey, &$msg = '')
    {
        //获取推流地址
        $p_href = RTMPPublishURL("pili-publish.chengdulihong.com", $this->hubName, $streamKey, 3600, $this->accessKey, $this->secretKey);
        //获取播放地址
        $g_href = RTMPPlayURL("pili-publish.chengdulihong.com", $this->hubName, $streamKey);
        //截图直播地址
        $pic = SnapshotPlayURL("pili-publish.chengdulihong.com", $this->hubName, $streamKey);
        //更新直播间状态
        $u_broadcast = $broadcast->fill(['name' => $broadcast['name'], 'chatroom_status' => 0, 'p_href' => $p_href, 'g_href' => $g_href, 'pic' => $pic])->save();
        if ($u_broadcast == false) {
            return $this->sendResponse(trans('admin.operate_failed') . '更新直播间出错');
        }
        return true;
    }

 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值