java jmf 播放背景音乐_使用JMF实现音乐播放(java多媒体编程) | 学步园

博客给出了使用Java和JMF播放背景音乐的代码示例。定义了SimpleAudioPlayer类,包含创建播放接口、播放和停止等方法,通过将本地文件转换为URL实现音乐播放,虽无图形界面,但能正常播放音乐。

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

package com.bird.jmf;

import java.io.File;

import java.net.MalformedURLException;

import java.net.URL;

import javax.media.Manager;

import javax.media.Player;

@SuppressWarnings("restriction")

public class SimpleAudioPlayer {

private Player audioPlayer = null;//建立一个播放接口

public SimpleAudioPlayer(URL url) throws Exception{//创建一个准备Player,准备好播放

audioPlayer = Manager.createRealizedPlayer(url);

}

@SuppressWarnings("deprecation")

public SimpleAudioPlayer(File file) throws MalformedURLException, Exception{//将本地文件改为URL

this(file.toURL());

}

public void play(){//直接调用播放方法就可以

audioPlayer.start();

}

public void stop(){//停止的时候一定要释放资源

audioPlayer.stop();

audioPlayer.close();

}

public static void main(String [] args) throws MalformedURLException, Exception{

File file = new File("d://1.mp3");

SimpleAudioPlayer Player = new SimpleAudioPlayer(file);

Player.play();

}

}

这就是最简单的音乐播放,没有图形界面,但是播放音乐还是没问题的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值