本文目錄一覽:
- 1、如何用Java實現在線聽音樂?
- 2、我想語音聽書?
- 3、十八不禁在線聽
如何用Java實現在線聽音樂?
package com.test.audio;import java.io.File;import java.awt.BorderLayout;import java.awt.FileDialog;import java.awt.Frame;import java.awt.GridLayout;import java.awt.Label;import java.awt.List;import java.awt.Menu;import java.awt.MenuBar;import java.awt.MenuItem;import java.awt.MenuShortcut;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent; import javax.sound.sampled.AudioFormat;import javax.sound.sampled.AudioInputStream;import javax.sound.sampled.AudioSystem;import javax.sound.sampled.DataLine;import javax.sound.sampled.SourceDataLine; public class MusicPlayer extends Frame { /** * */ private static final long serialVersionUID = -2605658046194599045L; boolean isStop = true;// 控制播放線程 boolean hasStop = true;// 播放線程狀態 String filepath;// 播放文件目錄 String filename;// 播放文件名稱 AudioInputStream audioInputStream;// 文件流 AudioFormat audioFormat;// 文件格式 SourceDataLine sourceDataLine;// 輸出設備 List list;// 文件列表 Label labelfilepath;//播放目錄顯示標籤 Label labelfilename;//播放文件顯示標籤 public MusicPlayer() { // 設置窗體屬性 setLayout(new BorderLayout()); setTitle(“MP3 Music Player”); setSize(350, 370); // 建立菜單欄 MenuBar menubar = new MenuBar(); Menu menufile = new Menu(“File”); MenuItem menuopen = new MenuItem(“Open”, new MenuShortcut(KeyEvent.VK_O)); menufile.add(menuopen); menufile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { open(); } }); menubar.add(menufile); setMenuBar(menubar); // 文件列表 list = new List(10); list.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { // 雙擊時處理 if (e.getClickCount() == 2) { // 播放選中的文件 filename = list.getSelectedItem(); play(); } } }); add(list, “Center”); // 信息顯示 Panel panel = new Panel(new GridLayout(2, 1)); labelfilepath = new Label(“Dir:”); labelfilename = new Label(“File:”); panel.add(labelfilepath); panel.add(labelfilename); add(panel, “North”); // 註冊窗體關閉事件 addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } // 打開 private void open() { FileDialog dialog = new FileDialog(this, “Open”, 0); dialog.setVisible(true); filepath = dialog.getDirectory(); if (filepath != null) { labelfilepath.setText(“Dir:” + filepath); // 顯示文件列表 list.removeAll(); File filedir = new File(filepath); File[] filelist = filedir.listFiles(); for (File file : filelist) { String filename = file.getName().toLowerCase(); if (filename.endsWith(“.mp3”) || filename.endsWith(“.wav”)) { list.add(filename); } } } } // 播放 private void play() { try { isStop = true;// 停止播放線程 // 等待播放線程停止 System.out.print(“Start:” + filename); while (!hasStop) { System.out.print(“.”); try { Thread.sleep(10); } catch (Exception e) { } } System.out.println(“”); File file = new File(filepath + filename); labelfilename.setText(“Playing:” + filename); // 取得文件輸入流 audioInputStream = AudioSystem.getAudioInputStream(file); audioFormat = audioInputStream.getFormat(); // 轉換mp3文件編碼 if (audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { audioFormat = new AudioFormat(AudioFormat.Encoding.PCM_SIGNED, audioFormat.getSampleRate(), 16, audioFormat .getChannels(), audioFormat.getChannels() * 2, audioFormat.getSampleRate(), false); audioInputStream = AudioSystem.getAudioInputStream(audioFormat, audioInputStream); } // 打開輸出設備 DataLine.Info dataLineInfo = new DataLine.Info( SourceDataLine.class, audioFormat, AudioSystem.NOT_SPECIFIED); sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLineInfo); sourceDataLine.open(audioFormat); sourceDataLine.start(); // 創建獨立線程進行播放 isStop = false; Thread playThread = new Thread(new PlayThread()); playThread.start(); } catch (Exception e) { e.printStackTrace(); } } class PlayThread extends Thread { byte tempBuffer[] = new byte[320]; public void run() { try { int cnt; hasStop = false; // 讀取數據到緩存數據 while ((cnt = audioInputStream.read(tempBuffer, 0, tempBuffer.length)) != -1) { if (isStop) break; if (cnt 0) { // 寫入緩存數據 sourceDataLine.write(tempBuffer, 0, cnt); } } // Block等待臨時數據被輸出為空 sourceDataLine.drain(); sourceDataLine.close(); hasStop = true; } catch (Exception e) { e.printStackTrace(); System.exit(0); } } } public static void main(String args[]) { new MusicPlayer(); }}
我想語音聽書?
您好,你的問題,我之前好像也遇到過,以下是我原來的解決思路和方法,希望能幫助到你,若有錯誤,還望見諒!手機聽書本軟體採用java技術,支持多種手機平台,適配機型廣泛。如:Nokia、索愛、dopod、moto、三星、LG、TCL、明基等數百款手機型號,均能完美支持。 (2)、支持在任意章節、任意位置均可發起基於當前位置開始的語音閱讀。 (3)、支持與語音播放同步的歌詞反顯功能。 (4)、預約聽書,一個為聽友自己預約的語音小說區。 (5)、音質流暢,無斷點停頓。 (6)、無需緩衝,即點即聽。 (7)、支持最長2個小時的連續在線播放,無需用戶干預。 (8)、在線收聽+休閑5分鐘遊戲。非常感謝您的耐心觀看,如有幫助請採納,祝生活愉快!謝謝!
十八不禁在線聽
如果播放器正在播放,卻聽不到聲音,請先檢查下收聽設備是否連接正確,或是音效卡是否調成了靜音(雙擊右下角灰色小喇叭).
出現 “準備就緒” 的解決方法:
1.如果以前能播放,現在不能播放,請先在播放器頁面按”CTRL+F5″刷新一次試試.
2.如果剛剛還可以播放,突然網站所有歌曲都提示”準備就緒”,這可能是由於網路問題造成的,請重新啟動次電腦.
3.播放器 Windows XP和Windows 2003的用戶推薦使用Window Media Player 10,或是Window Media Player 11. Windows 98,Me和2000的用戶推薦使用Window Media Player 9
4.請檢查右下角系統托盤裡灰色小喇叭是否正常;如果不正常或是沒有,可能沒有裝音效卡驅動,重新安裝即可
5.打開windows media player,[工具]-〉[選項]-〉選中[連接到internet(忽略其他命令)] 打勾
無法顯示播放器的解決方法:
1.大多是因為Windows Media Player被屏蔽了,如果您裝有卡卡助手3.0 請在插件免疫里 – 必備 中最後一項 Windows Media Player 取消免疫,在重新打開IE就可以恢復了. 卡卡4.0中點擊 高級功能 選擇下面的 插件免疫選項 – 點擊最後一項 必備 旁的加號 – 對最後一項 windows Media Player 取消免疫其它軟體解決方法類似.
2.如無法解決.請查看是否在IE中禁用了Java腳本(Java script),如果是請啟用該功能.(打開IE – 工具 – Internet 選項 – 安全,點擊自定義,然後下拉滾動條,找到Java腳本選項.選擇啟動)
可以單播但是無法連播的解決方法:
如果安裝過其他播放器(如暴風影音等)並把它設為默認播放器的話就,可能就無法正常連播. 解決方法:打開Windows Media Player,選擇工具-選項,切換到文件類型選項,然後全選,確定. 如果仍無法解決,建議先卸載安裝過的播放器,在重新安裝的過程中請注意不要關聯wma文件.
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/184371.html