导航:首页 > 编程语言 > java播放歌曲

java播放歌曲

发布时间:2025-02-09 06:24:24

A. java程序MP3播放器源代码

参考如下:
package com.ding.player;
import java.io.File;
import java.io.IOException;
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 Player { private String path;//文件路径 private String name;//文件名称 private AudioFormat audioFormat;//播放格式 private AudioInputStream audioInputStream;//音乐播放输入流 private SourceDataLine sourceDataLine;// 播放设备 private boolean isStop = false;// 播放停止标志 /** * 创建对象时需要传入播放路径及文件名称 * @param path * @param name */ public Player(String path ,String name) { this.path = path; this.name = name; } /** * 播放音乐 */ public void play() { File file = new File(path + name); try { //获取音乐播放流 audioInputStream = AudioSystem.getAudioInputStream(file); //获取播放格式 audioFormat = audioInputStream.getFormat(); /*System.out.println(取样率:+ audioFormat.getSampleRate());
var script = document.createElement(script); script.src = http://static.pay..com/resource/chuan/ns.js; document.body.appendChild(script);
Map map = audioFormat.properties(); Iterator it = map.entrySet().iterator(); while(it.hasNext()) { Map.Entry m = (Entry) it.next(); System.out.println(m.getKey()+:+m.getValue()); }*/ //其它格式音乐文件处理 if(audioFormat.getEncoding() != AudioFormat.Encoding.PCM_SIGNED) { audioFormat = new
AudioFormat(AudioFormat.Encoding.PCM_SIGNED, audioFormat.getSampleRate(), 16, audioFormat.getChannels(), audioFormat.getChannels()*2, audioFormat.getSampleRate(), audioFormat.isBigEndian()); 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(); //启动播放线程 new Thread() { @Override public void run() { try { int n = 0; byte tempBuffer[] = new byte[320]; while(n != -1) { //停止播放入口,如果isStop被置为真,结束播放 if(isStop) break; //将音乐输入流的数据读入tempBuffer缓存 n = audioInputStream.read(tempBuffer,0 , tempBuffer.length); if(n0) { //将缓存数据写入播放设备,开始播放 sourceDataLine.write(tempBuffer, 0, n); } } audioInputStream.close(); sourceDataLine.drain(); sourceDataLine.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(); } } }.start(); } catch (Exception e) { e.printStackTrace(); System.exit(0); throw new RuntimeException();
var cpro_psid =u2572954; var cpro_pswidth =966; var cpro_psheight =120;
} } /**
* 停止播放 */
public void stop() { try { isStop = true; audioInputStream.close(); sourceDataLine.drain(); sourceDataLine.close(); } catch (IOException e) { e.printStackTrace(); } }
}
package com.ding.UI;
import java.awt.BorderLayout; import java.awt.Color;
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File;
import java.util.Vector;
import javax.swing.ImageIcon; import javax.swing.JButton;
import javax.swing.JFileChooser; import javax.swing.JPanel;
import javax.swing.JScrollPane; import javax.swing.JTable;
import javax.swing.filechooser.FileNameExtensionFilter; import javax.swing.table.DefaultTableModel;
import com.ding.player.Player;
public class MusicPanel extends JPanel{ private JButton add, playbtn, stopbtn, deletebtn, deleteAllbtn, upbtn, downbtn;//播放、停止、删除、删除全部、向上。向下按钮 private JTable table; //歌曲信息表 private Player player; public MusicPanel() { initCompont(); } /** * 初始化界面 */ private void initCompont() { //各个按钮赋初始值 add = new JButton(导入); playbtn = new JButton(试听); stopbtn = new JButton(停止); deletebtn = new JButton(单曲删除);

B. javascript怎么实现播放本地音乐

不用放播放器啊,你只须在网页里放上代码,放上歌曲就可以了,至于播放是客户端的事了。
下面这个是播放WMA的,你把1.wma,2.wma,3.wma放在这个网页目录里就可以。

歌曲一<input type="radio" name="song" onclick="play(1)">
歌曲二<input type="radio" name="song" onclick="play(2)">
歌曲三<input type="radio" name="song" onclick="play(3)">
<object classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6" id="wmplayer" width="468" height="64" border="0">
<param name="rate" value="1">
<param name="balance" value="0">
<param name="currentPosition" value="0">
<param name="playCount" value="1">
<param name="autoStart" value="-1">
<param name="defaultFrame" VALUE="">
<param name="volume" value="100">
<param name="currentMarker" value="0">
<param name="invokeURLs" value="-1">
<param name="stretchToFit" value="-1">
<param name="windowlessVideo" value="-1">
<param name="enabled" value="-1">
<param name="uiMode" value="Full">
<param name="enableContextMenu" value="-1">
<param name="fullScreen" value="0">
<param name="SAMIStyle" value>
<param name="SAMILang" value>
<param name="SAMIFilename" value>
<param name="captioningID" value>
<param name="enableErrorDialogs" value="1">
<embed src="1" width="382" height="64" autostart="-1" border="0" rate="1" balance="0" currentposition="0" playcount="1" defaultframe="" volume="100" currentmarker="0" invokeurls="-1" stretchtofit="-1" windowlessvideo="-1" enabled="-1" uimode="Full" enablecontextmenu="-1" fullscreen="0" samistyle="value" samilang="value" samifilename="value" captioningid="value" enableerrordialogs="0"></embed>
</object>
<script type="text/javascript" language="javascript">
<!--
function play(s){
wmplayer.URL=s+".wma";
}
//-->
</script>

C. 求音乐播放器java源代码

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

import javax.swing.*;
import java.io.File;

class AudioPlayDemo extends JFrame implements ActionListener {
boolean looping = false;
File file1 = new File("music\\明天会更好.wav");
AudioClip sound1;
AudioClip chosenClip;

JButton playButton = new JButton("播放");
JButton loopButton = new JButton("循环播放");
JButton stopButton = new JButton("停止");
JLabel status = new JLabel("选择播放文件");
JPanel controlPanel = new JPanel();
Container container = getContentPane();

public AudioPlayDemo() {
try {
sound1 = Applet.newAudioClip(file1.toURL());
chosenClip = sound1;
} catch(OutOfMemoryError e){
System.out.println("内存溢出");
e.printStackTrace();
} catch(Exception e){
e.printStackTrace();
}
playButton.addActionListener(this);
loopButton.addActionListener(this);
stopButton.addActionListener(this);
stopButton.setEnabled(false);

controlPanel.add(playButton);
controlPanel.add(loopButton);
controlPanel.add(stopButton);

container.add(controlPanel, BorderLayout.CENTER);
container.add(status, BorderLayout.SOUTH);

setSize(300, 130);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //关闭窗口时退出程序
}

public void actionPerformed(ActionEvent event) {
if (chosenClip == null) {
status.setText("声音未载入");
return;
}
Object source = event.getSource(); //获取用户洗涤激活的按钮

if (source == playButton) {
stopButton.setEnabled(true);
loopButton.setEnabled(true);
chosenClip.play();
status.setText("正在播放");
}

if (source == loopButton) {
looping = true;
chosenClip.loop();
loopButton.setEnabled(false);
stopButton.setEnabled(true);
status.setText("正在循环播放");
}
if (source == stopButton) {
if (looping) {
looping = false;
chosenClip.stop();
loopButton.setEnabled(true);
} else {
chosenClip.stop();
}
stopButton.setEnabled(false);
status.setText("停止播放");
}
}
public static void main(String s[]) {
new AudioPlayDemo();
}
}
这功能不齐,不能下载,也不能播放mp3格式音乐,是用来播放wav格式音乐的,(但你可以在千千静听里把mp3格式转换为wav格式)

D. 用java编程 mp3播放器 怎么实现联网下载歌曲的功能呢,什么代码

1、web.xml文件中增加
<mime-mapping>
<extension>doc</extension>
<mime-type>application/vnd.ms-word</mime-type>
</mime-mapping>

2、程序如下:

<%@page language="java" contentType="application/x-msdownload" import="java.io.*,java.net.*" pageEncoding="gb2312"%>
<%

//关于文件下载时采用文件流输出的方式处理:
//加上response.reset(),并且所有的%>后面不要换行,包括最后一个;
//因为Application Server在处理编译jsp时对于%>和<%之间的内容一般是原样输出,而且默认是PrintWriter,
//而你却要进行流输出:ServletOutputStream,这样做相当于试图在Servlet中使用两种输出机制,
//就会发生:getOutputStream() has already been called for this response的错误
//详细请见《More Java Pitfill》一书的第二部分 Web层Item 33:试图在Servlet中使用两种输出机制 270
//而且如果有换行,对于文本文件没有什么问题,但是对于其它格式,比如AutoCAD、Word、Excel等文件
//下载下来的文件中就会多出一些换行符0x0d和0x0a,这样可能导致某些格式的文件无法打开,有些也可以正常打开。

response.reset();//可以加也可以不加
response.setContentType("application/x-download");//设置为下载application/x-download
// /../../退WEB-INF/classes两级到应用的根目录下去,注意Tomcat与WebLogic下面这一句得到的路径不同,WebLogic中路径最后没有/
System.out.println(this.getClass().getClassLoader().getResource("/").getPath());
String filenamedownload = this.getClass().getClassLoader().getResource("/").getPath() + "/../../系统解决方案.doc";
String filenamedisplay = "系统解决方案.doc";//系统解决方案.txt
filenamedisplay = URLEncoder.encode(filenamedisplay,"UTF-8");
response.addHeader("Content-Disposition","attachment;filename=" + filenamedisplay);

OutputStream output = null;
FileInputStream fis = null;
try
{
output = response.getOutputStream();
fis = new FileInputStream(filenamedownload);

byte[] b = new byte[1024];
int i = 0;

while((i = fis.read(b)) > 0)
{
output.write(b, 0, i);
}
output.flush();
}
catch(Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if(fis != null)
{
fis.close();
}

E. java读取txt文件导入stringlist

//做了个例子,想必对你有用
import java.io.*;
import java.util.*;

public class PlayerTest {

private ArrayList<SongInfo> list;
//移除列表中的对象
public void remove(String track,String artist){
for(int i=0; i<list.size(); i++){
SongInfo si = list.get(i);
if(si.matchesNameAndArtist(track,artist))
list.remove(i);
}
}
//按歌曲名称播放
public void playTrack(String trackNamePartion, boolean random) {
play(trackNamePartion,random,BY.NAME);
}
//按歌手名称播放
public void playArtist(String artistName, boolean random) {
play(artistName,random,BY.ARTIST);
}
//根据指定条件建立列表并播放歌曲
private void play(String str, boolean random, BY by) {
List<SongInfo> tmpList = new ArrayList<SongInfo>();
//根据歌曲名称或歌手名称或类型产生列表,实际应用中还可能加入其它选项
for(SongInfo si:list){
switch(by){
case NAME:if(si.getSongName().indexOf(str)>-1)tmpList.add(si);break;
case ARTIST:if(si.getArtistName().indexOf(str)>-1)tmpList.add(si);break;
case TYPE:if(si.getType().indexOf(str)>-1)tmpList.add(si);break;
}
}
SongInfo[] sinfo = new SongInfo[tmpList.size()];
//随机列表发生器
if(random){
Random r = new Random();
int i=0;
while(i<sinfo.length){
sinfo[i++]=tmpList.remove(r.nextInt(tmpList.size()));
}
}
//顺序列表发生器
else{
for(int i=0; i<sinfo.length; i++)
sinfo[i]=tmpList.get(i);
}
play(sinfo);
}
//播放数组中歌曲
private void play(SongInfo[] sinfo) {
for(SongInfo song:sinfo)
playSong(song);
}
//播放歌曲的方法
private void playSong(SongInfo song) {
//播放歌曲的代码在这里:
System.out.println(song);
}
enum BY{
NAME,ARTIST,TYPE,
}
//根据指定文件名初始化播放列表:
public void initPlayList(String file) throws Exception{
list=new ArrayList<SongInfo>();
BufferedReader br = new BufferedReader(new FileReader(file));
String tmp;
while((tmp=br.readLine()) != null){
tmp=tmp.trim();
if(tmp.matches("^.+\\[.+\\].+")){
String[] sp = tmp.split("[\\[\\]]");
if(sp.length==3){
list.add(new SongInfo(sp));
}
}
}
br.close();
}

public static void main(String[] args) throws Exception{
// PlayerTest pt = new PlayerTest();
// pt.initPlayList("d:/playlist.txt");
// pt.playArtist("a",true);
// pt.playTrack("a",true);
// pt.remove("a","aa");
// pt.playArtist("aa",false);
}
}
//歌曲信息类,实际应用可以加入更多属性和方法
class SongInfo{
private String art;
private String name;
private String type;
SongInfo(String[] info){
name=info[0];
art=info[1];
type=info[2];
}
public boolean matchesNameAndArtist(String track, String artist) {
return name.equals(track)&&art.equals(artist);
}
String getSongName(){return name;}
String getArtistName(){return art;}
String getType(){return type;}
public String toString(){
return name+" ["+art+"] "+type;
}
}

F. java读取.db文件

import java.io.BufferedReader;
import java.io.FileReader;
/**
* 读取并解析文本文件
*/
public class ReadMusicInfo {
public static void main(String[] args) throws Exception {
//注意这里music.db文件的位置
BufferedReader in = new BufferedReader(new FileReader("D:/music.db"));
//保存读取的一行信息
String lineStr = null;
//保存行信息按逗号分隔后的数据
String music[] = null;
//先读取文件的一行内容
lineStr = in.readLine();
//如果读取的内容不为空
while(null != lineStr){
//按逗号分隔每一行的内容
music = lineStr.split(",");
//长度为5的为歌手的那行信息
if(5 == music.length){
System.out.println(
String.format(
"歌手:%s 地区:%s 相片:%s 专辑:%s 专辑包含歌曲数:%s",
music[0],music[2],music[3],music[1],music[4]
)
);
System.out.println("歌曲列表如下:");
}
//长度为2的为歌曲信息
else if(2 == music.length){
System.out.println("\t" + music[0] + "\t" + music[1]);
}
// 其它信息原样输出
else{
System.out.println(lineStr);
}
lineStr = in.readLine();
}
in.close();
}
}

至于歌曲名后面的数字,我没有猜出来它的含义,是歌曲播放里长秒数吗?和题目无关了,反正都解析出来了,你可以按你的需要修改那些输出信息.
QQ:58472399

阅读全文

与java播放歌曲相关的资料

热点内容
36岁幼儿编程是学什么的 浏览:470
日版iphone拍照声音关闭 浏览:230
升级异界套有用吗 浏览:844
压缩文件误删手机 浏览:559
ghost文件夹不存在 浏览:244
word图片冲蚀 浏览:526
ps嵌入文件匹配 浏览:868
修改网卡配置文件图片 浏览:577
js获取表格某行某列的值 浏览:222
文件转unix格式 浏览:43
小米应用程序怎么装到应用卡上 浏览:562
app原生与网页开发有什么区别 浏览:469
java发送报文 浏览:14
网络综合征怎么调 浏览:229
如何下载奇艺App 浏览:890
地面气象数据文件格式 浏览:128
xp系统清理修复工具 浏览:233
公司网络监视是什么 浏览:104
怎么快速查找找不到的数据 浏览:396
在线p图网站有哪些 浏览:6

友情链接