#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <conio.h>
unsigned frequency[100]
char hight[100]
unsigned time[100]
unsigned rate
void main()
{
void set(unsigned f[],char h[],unsigned t[],int r,int num)
void music(unsigned f[],unsigned t[],int num)
int flag=0
FILE *f1
int i,n,menu
char FileName[30]
while(1)
{ f1=NULL
i=n=0
printf("本程序采用哗虚编码的形式播放音乐。\n")
printf("\n用记事本编辑乐谱,然后通过输入文件名播放音乐\n")
printf("乐谱文件可以自创,也可以抄别人的\n")
printf("\n现在可以先输入数字再按者郑回车播放音乐:\n")
printf("1 播放指定音乐music1\n")
printf("2 播放指定音乐music2\n")
printf("3 通过程序文件名播放音乐\n")
printf("乱嫌燃4 退出\n")
while(1)
{
printf("menu=")scanf("%d",&menu)
if(menu==1)
{
strcpy(FileName,"music1.txt")
break
}
if(menu==2)
{
strcpy(FileName,"music2.txt")
break
}
if(menu==3)
{
scanf("%s",FileName)
break
}
if(menu==4)
exit(0)
}
printf("\n该文件的音乐编码如下:\n")
if((f1=fopen(FileName,"r"))==NULL)
{
printf("不能打开文件!\n")
exit(1)
}
fscanf(f1,"%d",&rate)
while(!feof(f1)&&flag!=1)
{
fscanf(f1," %d%c%d",&frequency[i],&hight[i],&time[i])
printf(" %d%c%d",frequency[i],hight[i],time[i])
if(time[i]!=-1)
{
i++n++
}
else
flag=1
}
printf("\n")
set(frequency,hight,time,rate,n)
music(frequency,time,n)
fclose(f1)
}
getch()
}
void set(unsigned f[],char h[],unsigned t[],int r,int num)
{
int i,k
for(i=0i<numi++)
{
t[i]=t[i]*r
switch(h[i])
{
case 'H':k=4break
case 'M':k=2break
case 'L':k=1
}
switch(f[i])
{
case 1: f[i]=262*kbreak
case 2: f[i]=296*kbreak
case 3: f[i]=330*kbreak
case 4: f[i]=349*kbreak
case 5: f[i]=392*kbreak
case 6: f[i]=440*kbreak
case 7: f[i]=494*kbreak
}
}
}
void music(unsigned f[],unsigned t[],int num)
{
int i
for(i=0i<numi++)
{
Beep(f[i],t[i])
}
}
给你个Java音乐播放器代码,你试试!import javax.media.ControllerEvent
import javax.media.ControllerListener
import javax.media.EndOfMediaEvent
import javax.media.PrefetchCompleteEvent
import javax.media.RealizeCompleteEvent
import javax.media.*
import javax.swing.*
import java.awt.*
import java.awt.event.*
public class MediaPlayer extends JFrame implements ActionListener,
ItemListener, ControllerListener {
String title
Player player
boolean first = true, loop = false
Component vc, cc
String currentDirectory=null
// 构造函数,其中包括了设置响应窗口事件的监听器。
MediaPlayer(String title) {
super(title)
/* 关闭按钮的实现。。 */
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
dispose()
}
public void windowClosed(WindowEvent e) {
if (player != null)
player.close()
System.exit(0)
}
})
// 调用程序菜单栏的方法成员完成菜单的布置
setupMenu()
setSize(400, 400)
setVisible(true)
}
// 本方法用以设置程序菜单栏
public void setupMenu() {
// 设置一个菜单
Menu f = new Menu("文件")
// 往设置的菜单添加菜单项
MenuItem mi = new MenuItem("打开")
f.add(mi)
mi.addActionListener(this)
f.addSeparator()
CheckboxMenuItem cbmi = new CheckboxMenuItem("循环", false)
cbmi.addActionListener(this)
f.add(cbmi)
f.addSeparator()
MenuItem ee = new MenuItem("退出")
ee.addActionListener(this)
f.add(ee)
f.addSeparator()
Menu l = new Menu("播放列表")
Menu c = new Menu("播放控制世数誉"毕兄)
MenuItem move = new MenuItem("播放")
move.addActionListener(this)
c.add(move)
c.addSeparator()
MenuItem pause = new MenuItem("暂停")
pause.addActionListener(this)
c.add(pause)
c.addSeparator()
MenuItem stop = new MenuItem("停止"搜段)
stop.addActionListener(this)
c.add(stop)
c.addSeparator()
// 设置一个菜单栏
MenuBar mb = new MenuBar()
mb.add(f)
mb.add?
mb.add(l)
// 将构造完成的菜单栏交给当前程序的窗口
setMenuBar(mb)
}
// 动作时间响应成员;捕捉发送到本对象的各种事件
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String cufile, selectfile, currentDirectory
if (e.getActionCommand().equals("退出")) {
// 调用dispose以便执行windowClosed
dispose()
return
}
// 此事表明拥护选择了“播放”命令
// 如果当前有一个文件可以播放则执行播放命令
if (e.getActionCommand().equals("播放")) {
if (player != null) {
player.start()
}
return
}
// 如果当前正在播放某一文件,则执行暂停
if (e.getActionCommand().equals("暂停")) {
if (player != null) {
player.stop()
}
return
}
// 停止命令的响应
if (e.getActionCommand().equals("停止")) {
if (player != null) {
player.stop()
player.setMediaTime(new Time(0))
}
return
}
// 用户选择要播放的媒体文件
if (e.getActionCommand().equals("打开")) {
FileDialog fd = new FileDialog(this, "打开媒体文件", FileDialog.LOAD)
// fd.setDirectory(currentDirectory)
2008-2-6 02:46 回复
肆方茉莉
62位粉丝
6楼
fd.setVisible(true)
// 如果用户放弃选择文件,则返回
if (fd.getFile() == null) {
return
}
// 保存了所选文件的名称及其路径名称已被稍后使用
// 同时设置当前文件夹路径
selectfile = fd.getFile()
currentDirectory = fd.getDirectory()
cufile = currentDirectory + selectfile
// 将用户选择的文件作为一个菜单项加入播放列表,该菜单项名为该文件名
// 被点击后给出的命令串是该文件的全路径名
MenuItem mi = new MenuItem(selectfile)
mi.setActionCommand(cufile)
MenuBar mb = getMenuBar()
Menu m = mb.getMenu(2)
mi.addActionListener(this)
m.add(mi)
} else {
// 程序逻辑运行到次表示用户选择了一个“播放列表”中的媒体文件
// 此时可以通过如下动作获得该文件的全路径名
cufile = e.getActionCommand()
selectfile = cufile
}
// 如果存在一个播放器,则先将其关闭,稍后再重新创建
// 创建播放器时需要捕捉一些异常
if (player != null) {
player.close()
}
try {
player = Manager.createPlayer(new MediaLocator("file:" + cufile))
} catch (Exception e2) {
System.out.println(e2)
return
}/*
* catch(NoPlayerException e2){ System.out.println("不能找到播放器")
* return }
*/
if (player == null) {
System.out.println("无法创建播放器")
return
}
first = false
setTitle(selectfile)
// 设置处理播放控制器实际的对象;
/**/
player.addControllerListener(this)
player.prefetch()
}
// 菜单状态改变事件的响应函数;
public void itemStateChanged(ItemEvent arg0) {
// TODO Auto-generated method stub
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new MediaPlayer("播放器")
}
// 调用绘图函数进行界面的绘制
// public void update() {
// }
// 绘图函数成员
//public void paint(Graphics g) {
// }
public void controllerUpdate(ControllerEvent e) {
// TODO Auto-generated method stub
Container tainer = getContentPane()
// 调用player.close()时ControllerClosedEvent事件出现
// 如果存在视觉部件,则该部件应该拆除(为了一致起见,我们对控制面版部件也执行同样的 *** 作,下一次需要时再构造)
if (e instanceof ControllerClosedEvent) {
if (vc != null) {
remove(vc)
vc = null
}
if (cc != null) {
remove(cc)
cc = null
}
}
// 播放结束时,将播放指针置于文件之首,如果设定了循环播放,则再次启动播放器;
if (e instanceof EndOfMediaEvent) {
player.setMediaTime(new Time(0))
if (loop) {
player.start()
}
return
}
// PrefetchCompletEvent事件发生后调用start,正式启动播放
if (e instanceof PrefetchCompleteEvent) {
player.start()
return
}
// 本事件表示由于播放的资源已经确定;此时要将媒体的图形conmopnent
// 如果有显示出来,同时将播放器player的控制显示到窗口里;
if (e instanceof RealizeCompleteEvent) {
// 如果媒体中有图像,将对应图像component载入窗体;
vc = player.getVisualComponent()
if (vc != null)
tainer.add(vc, BorderLayout.CENTER)
// 将对应控制器component载入窗体
cc = player.getControlPanelComponent()
cc.setBackground(Color.blue)
if (cc != null)
tainer.add(cc, BorderLayout.SOUTH)
// 有一些特殊媒体在播放时提供另外的控制手段,将控制器一并加入窗口;
/*
* gc=player.getGainControl()gcc=gc.getControlComponent()
* if(gcc!=null) tainer.add(gcc,BorderLayout.NORTH)
*/
// 根据媒体文件中是否有图像,设定相应的窗口大小
if (vc != null) {
pack()
return
} else {
setSize(300, 75)
setVisible(true)
return
}
}
}
}
希望对你有帮助!
package my.android.playersimport java.io.File
import java.io.FilenameFilter
import java.util.ArrayList
import java.util.List
import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import android.content.Intent
import android.media.MediaPlayer
import android.os.Bundle
import android.view.KeyEvent
import android.view.View
import android.view.View.OnClickListener
import android.widget.ImageButton
import android.widget.TextView
public class mainActivity extends Activity {
private List<String>myMusicList=new ArrayList<String>()
//当前播放歌曲的索引
private int currentListItem=0
//音乐的路径
private static final String MUSIC_PATH="/册渣sdcard/mp3"
//播旅扰放对象
private MediaPlayer myMediaPlayer
private TextView m_TextView
//播放按钮
private ImageButton m_start
private ImageButton m_stop
private ImageButton m_next
private ImageButton m_last
/*设定bIsPaused一开始为false */
private boolean bIsRun = false
private boolean isplay
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState)
setContentView(R.layout.main2)
myMediaPlayer=new MediaPlayer()
musicList()
m_TextView=(TextView)findViewById(R.id.mtextview)
m_start=(ImageButton)findViewById(R.id.imgbtn_start)
m_stop=(ImageButton)findViewById(R.id.imgbtn_stop)
m_next=(ImageButton)findViewById(R.id.imgbtn_next)
m_last=(ImageButton)findViewById(R.id.imgbtn_last)
listener()
Intent intent = this.getIntent()
Bundle bundle = intent.getExtras()
currentListItem = bundle.getInt("currentListItem")
isplay=bundle.getBoolean("isplay")
if(isplay==true)
{
bIsRun=false
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem))
}
}
//监听拆姿旦事件
void listener(){
//开始
m_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try
{
if(myMediaPlayer.isPlaying()==true)
{
myMediaPlayer.pause()
m_start.setImageResource(R.drawable.pause)
}
else
{
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem))
}
}
catch (IllegalStateException e)
{
// TODO Auto-generated catch block
e.printStackTrace()
}
}
})
//下一首
m_next.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
nextMusic()
}
})
//上一首
m_last.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
lastMusic()
}
})
//停止
m_stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
stopMusic()
}
})
myMediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
nextMusic()
}
})
}
//播放音乐
void playMusic(String path){
try {
if(bIsRun==false)
{
myMediaPlayer.reset()
myMediaPlayer.setDataSource(path)
myMediaPlayer.prepare()
myMediaPlayer.start()
/*
* 取出歌曲名的.mp3后缀
* */
String str=(myMusicList.get(currentListItem)).toString()
System.out.println(str)
String str1[]=str.split("\\.")
System.out.println(str1[0])
m_TextView.setText(str1[0])
}
else
{
myMediaPlayer.start()
}
m_start.setImageResource(R.drawable.start)
bIsRun=true
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace()
}
}
//下一首
void nextMusic(){
if(++currentListItem>=myMusicList.size()){
currentListItem=0
}
bIsRun=false
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem))
}
//上一首
void lastMusic(){
if(--currentListItem<0)
currentListItem=myMusicList.size()-1
bIsRun=false
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem))
}
//停止
void stopMusic() {
if (myMediaPlayer.isPlaying()) {
m_start.setImageResource(R.drawable.pause)
myMediaPlayer.stop()// 停止
bIsRun=false
}
else
playMusic(MUSIC_PATH+"/"+myMusicList.get(currentListItem))
}
//当用户返回时结束音乐并释放音乐对象
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode==KeyEvent.KEYCODE_BACK){
new AlertDialog.Builder(mainActivity.this).setTitle("message")
.setIcon(android.R.drawable.dialog_frame)
.setMessage("你确定要离开吗?")
.setPositiveButton("确定",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
myMediaPlayer.stop()
myMediaPlayer.release()
finish()
}
}).setNegativeButton("取消",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
}
}).show()
}
return super.onKeyDown(keyCode, event)
}
/**
* 文件过滤器
*
* @author
*
*/
class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
return (filename.endsWith(".mp3"))
}
}
//绑定音乐
void musicList(){
try{
File home=new File(MUSIC_PATH)
if(!home.exists())
home.mkdirs()
if(home.listFiles(new MusicFilter()).length>=0){
for(File file:home.listFiles(new MusicFilter())){
myMusicList.add(file.getName().toString())
}
}
}
catch(Exception ex)
{
ex.printStackTrace()
}
}
}
package my.android.players
import java.io.File
import java.io.FilenameFilter
import java.util.ArrayList
import java.util.List
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
public class Activity01 extends Activity {
//播放列表
private List<String>myMusicList=new ArrayList<String>()
//当前播放歌曲的索引
private int currentListItem
//音乐的路径
private static final String MUSIC_PATH="/sdcard/mp3"
//播放列表
private ListView m_ListView
private boolean isplay=true
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
m_ListView=(ListView)findViewById(R.id.lv_music)
musicList()
//当选择列表项时播放音乐
m_ListView.setOnItemClickListener(new ListView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?>arg0, View arg1, int arg2,
long arg3) {
currentListItem = arg2
Intent intent = new Intent()
Bundle mBundle=new Bundle()
mBundle.putInt("currentListItem", currentListItem)
mBundle.putBoolean("isplay", isplay)
intent.putExtras(mBundle)
intent.setClass(Activity01.this,mainActivity.class)
startActivity(intent)
finish()
}
})
}
/**
* 文件过滤器
*
* @author
*
*/
class MusicFilter implements FilenameFilter {
@Override
public boolean accept(File dir, String filename) {
return (filename.endsWith(".mp3"))
}
}
//绑定音乐
void musicList(){
try{
File home=new File(MUSIC_PATH)
if(!home.exists())
home.mkdirs()
if(home.listFiles(new MusicFilter()).length>=0){
for(File file:home.listFiles(new MusicFilter())){
myMusicList.add(file.getName().toString())
}
ArrayAdapter<String>musicList=new ArrayAdapter<String>
(Activity01.this,android.R.layout.simple_list_item_1, myMusicList)
m_ListView.setAdapter(musicList)
}
}
catch(Exception ex)
{
ex.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)