JavaWeb項(xiàng)目音頻資源播放實(shí)現(xiàn)方法詳解
一、方式1:登陸系統(tǒng)后進(jìn)行播放,即在瀏覽器端
需要在JSP頁面編寫相關(guān)代碼
<div id="midea" style="display: none;">
<object id='player' height='100' width='200' classid='CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6'>
<param name='AutoStart' value='0' />
<param name='Balance' value='0' />
<param name='enabled' value='-1' />
<param name='EnableContextMenu' value='-1' />
<param name='url' value='${pageContext.request.contextPath}/player/bestduanxin.wav' />
<param name='volume' value='100' />
<param name='uiMode' value='mini' />
</object>
</div>
在js腳本里調(diào)用
document.getElementById('player').controls.play();
setTimeout(stopPlayer,6000);
function stopPlayer(){
document.getElementById('player').controls.stop();
}
二、方式2:不用登陸即可播放,即在服務(wù)器端
需在java后臺編寫相關(guān)代碼
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.servlet.http.HttpServletRequest;
public class AudioPlayer {
public static void player(HttpServletRequest request){
URL audioUrl=null;
try {
//項(xiàng)目URL根路徑
String path = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath();
//音頻資源地址
audioUrl = new URL(path+"/player/bestduanxin.wav");
//創(chuàng)建音頻播放類
final AudioClip audioClass=Applet.newAudioClip(audioUrl);
audioClass.loop();
//new一個線程執(zhí)行停止播放
TimerTask task = new TimerTask() {
@Override
public void run() {
audioClass.stop();
}
};
//new一個定時器,指定播放若干秒后執(zhí)行停止播放
Timer timer = new Timer();
timer.schedule(task, GetPropertiesValue.getTimes()*GetPropertiesValue.getDuration());
} catch (Exception e) {
e.printStackTrace();
}
}
}
附獲取音頻文件時長的方法
File file = new File(url.substring(0, url.indexOf("WEB-INF"))+"player/bestduanxin.wav");<br>Clip clip = AudioSystem.getClip();
AudioInputStream ais = AudioSystem.getAudioInputStream(file);
clip.open(ais);<br>//得到的時間為微秒(μs),除以1000得到毫秒(ms)數(shù)
System.out.println(clip.getMicrosecondLength()/1000);
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot定時任務(wù)的實(shí)現(xiàn)詳解
這篇文章主要介紹了SpringBoot定時任務(wù)的實(shí)現(xiàn)詳解,定時任務(wù)是企業(yè)級開發(fā)中最常見的功能之一,如定時統(tǒng)計訂單數(shù)、數(shù)據(jù)庫備份、定時發(fā)送短信和郵件、定時統(tǒng)計博客訪客等,簡單的定時任務(wù)可以直接通過Spring中的@Scheduled注解來實(shí)現(xiàn),需要的朋友可以參考下2024-01-01
SpringBoot整合WebSocket實(shí)現(xiàn)聊天室流程全解
WebSocket協(xié)議是基于TCP的一種新的網(wǎng)絡(luò)協(xié)議。本文將通過SpringBoot集成WebSocket實(shí)現(xiàn)簡易聊天室,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,感興趣的可以了解一下2023-01-01
Java實(shí)現(xiàn)操作JSON的便捷工具類完整實(shí)例【重寫Google的Gson】
這篇文章主要介紹了Java實(shí)現(xiàn)操作JSON的便捷工具類,基于重寫Google的Gson實(shí)現(xiàn),涉及java針對json數(shù)據(jù)的各種常見轉(zhuǎn)換操作實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-10-10
mybatis-plus分頁查詢的實(shí)現(xiàn)示例
這篇文章主要介紹了mybatis-plus分頁查詢的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
Springboot注解@Value讀取配置文件參數(shù)詳解
Spring Boot提供了靈活的配置文件讀取機(jī)制,主要有兩種方式,第一種是使用@Value注解直接在類屬性上讀取application.yml文件中的配置,這種方式簡單直接,但需要為每個配置項(xiàng)單獨(dú)設(shè)置屬性,第二種方式是通過@PropertySource注解讀取自定義的Properties文件2024-11-11
詳解Spring Boot + Mybatis 實(shí)現(xiàn)動態(tài)數(shù)據(jù)源
這篇文章主要介紹了Spring Boot + Mybatis 實(shí)現(xiàn)動態(tài)數(shù)據(jù)源,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04

