java 对接阿里视频点播服务工具类

package com.kindess.util;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.*;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.aliyuncs.vod.model.v20170321.DeleteVideoResponse;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoRequest;
import com.aliyuncs.vod.model.v20170321.GetPlayInfoResponse;

import java.io.InputStream;
import java.util.List;

/**
 * @author kindess
 * @date 2019-07-29
 * @return VodUtil
 */
public class VodUtil {

    /**
     * AccessKeyId
     */
    private static final String ACCESSKEYID = "你的ACCESSKEYID ";

    /**
     * AccessKeySecret
     */
    private static final String ACCESSKEYSERTET = "你的ACCESSKEYSERTET ";

    /**
     * 初始化点播服务
     *
     * @return
     * @throws ClientException
     */
    public static DefaultAcsClient initVodClient() throws ClientException {
        // 点播服务接入区域
        String regionId = "cn-shanghai";
        DefaultProfile profile = DefaultProfile.getProfile(regionId, ACCESSKEYID, ACCESSKEYSERTET);
        DefaultAcsClient client = new DefaultAcsClient(profile);
        return client;
    }

    /**
     * 获取播放地址函数
     *
     * @param videoId
     * @throws Exception
     */
    public static GetPlayInfoResponse getPlayInfo(String videoId) throws Exception {
        GetPlayInfoRequest request = new GetPlayInfoRequest();
        request.setVideoId(videoId);
        return VodUtil.initVodClient().getAcsResponse(request);
    }

    /**
     * 播放方法
     *
     * @param videoId
     * @return
     */
    public static String getPlay(String videoId) {
        GetPlayInfoResponse response = new GetPlayInfoResponse();
        try {
            response = VodUtil.getPlayInfo(videoId);
            List<GetPlayInfoResponse.PlayInfo> playInfoList = response.getPlayInfoList();
            //播放地址
            for (GetPlayInfoResponse.PlayInfo playInfo : playInfoList) {
                return playInfo.getPlayURL();
            }
        } catch (Exception e) {
            e.getLocalizedMessage();
        }
        return null;
    }

    /**
     * 流式上传接口
     *
     * @param title
     * @param fileName
     * @param inputStream
     */
    public static String uploadStream(String title, String fileName, InputStream inputStream) {
        UploadStreamRequest request = new UploadStreamRequest(ACCESSKEYID, ACCESSKEYSERTET, title, fileName, inputStream);
        //视频标签
        request.setTags("课程视频");
        UploadVideoImpl uploader = new UploadVideoImpl();
        UploadStreamResponse response = uploader.uploadStream(request);
        //请求视频点播服务的请求ID
        if (response.isSuccess()) {
            return response.getVideoId();
        } else {
            return null;
        }
    }

    /**
     * 删除视频
     *
     * @param client 发送请求客户端
     * @return DeleteVideoResponse 删除视频响应数据
     * @throws Exception
     */
    public static DeleteVideoResponse deleteVideo(DefaultAcsClient client, String videoId) throws Exception {
        DeleteVideoRequest request = new DeleteVideoRequest();
        //支持传入多个视频ID,多个用逗号分隔
        request.setVideoIds(videoId);
        return client.getAcsResponse(request);
    }

    /**
     * 删除方法
     *
     * @param videoId
     */
    public static void delVideo(String videoId) {
        DefaultAcsClient client = null;
        DeleteVideoResponse response = new DeleteVideoResponse();
        try {
            client = initVodClient();
            response = deleteVideo(client, videoId);
        } catch (ClientException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实时录音可以通过Java Sound API来实现,同时可以将录音数据传输到阿里云上,实现语音识别等功能。以下是一个简单的示例代码: ```java import javax.sound.sampled.*; import com.aliyun.asr.client.AsrClient; import com.aliyun.asr.client.AsrClientFactory; import com.aliyun.asr.model.StartAsrRequest; import com.aliyun.asr.model.StartAsrResponse; public class RealtimeRecording { public static void main(String[] args) throws Exception { // 创建音频输入流 AudioFormat format = new AudioFormat(16000, 16, 1, true, false); DataLine.Info info = new DataLine.Info(TargetDataLine.class, format); TargetDataLine line = (TargetDataLine) AudioSystem.getLine(info); line.open(format); line.start(); // 创建阿里云语音识别客户端 AsrClient asrClient = AsrClientFactory.getAsrClient(); StartAsrRequest request = new StartAsrRequest(); request.setEnablePunctuation(true); request.setEnableInverseTextNormalization(true); // 开始录音和识别 while (true) { byte[] buffer = new byte[640]; int count = line.read(buffer, 0, buffer.length); if (count > 0) { request.setAudio(buffer); StartAsrResponse response = asrClient.startAsr(request); System.out.println(response.getResult()); } } } } ``` 在这个示例代码中,创建了一个音频输入流,并通过Java Sound API实时录音。然后,创建了一个阿里云语音识别客户端,并在录音过程中不断地将录音数据传输到阿里云进行语音识别。最后,输出识别结果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值