关于delphi的mediaplayer的问题
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于delphi的mediaplayer的问题相关的知识,希望对你有一定的参考价值。
delphi的程序设计中,用mediaplayer播放音乐,为什么只能播放wav格式的,其他格式的都不能播放,这是怎么回事,还请各位高手帮忙,小弟不胜感激!!
delphi自带的播放器控件只能播window里常见的几种格式。像.rm .rmvb格式等很多格式都不支持。如果你想要播放这些格式的话,建议使用ActiveX控件吧。安装到组件面板的方法是:
Component -> Import ActiveX->列表中选择你需要的播放器组件(这些都是你系统中已经注册过的组件)。比如说,你系统里装过Realone的话,那就可以使用Realone播放器组件播.rm .rmvb格式的视频了。
-----------------------------------------------
试试吧!试完记得把分给我啊! 参考技术A 是不是opendialog 的 filter 设置为 *.wav 了
改为 *.* 参考技术B 能不能打开其他格式的音乐?
你给的扩展名没给够吧???
如何用delphi编写音乐播放器
一、使用Delphi自带的MediaPlayer控件。1.在窗体里添加MediaPlayer1控件.
2.在窗体创建处(TForm1.FormCreate)添加如下代码即可播放
MediaPlayer1.FileName:='<需要播放的MP3文件>';
MediaPlayer1.Open;
二、使用Bass.dll函数库(在www.un4seen.com里下载)。
简单例子:
uses
Bass;
BASS_Init(-1, 44100, 0, 0, nil);//初始化Bass库
var
chan:HSTREAM;//声明流句柄
chan:=BASS_StreamCreateFile(False, PChar('<需要播放的MP3文件>'),0,0,0);//装载媒体文件获取句柄
if chan<>0 then BASS_ChannelPlay(chan, False);//播放
用完后:
BASS_StreamFree(chan);//释放文件
BASS_Free();关闭Bass
以上代码给你做个参考 参考技术A delphi制作音乐播放器完整代码如下:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
ExtCtrls, StdCtrls, MPlayer, ComCtrls,registry,inifiles;
type
TForm1 = class(TForm)
Image2: TImage;
Label1: TLabel;
Label2: TLabel;
openimg: TImage;
playimg: TImage;
stopimg: TImage;
Label3: TLabel;
MediaPlayer1: TMediaPlayer;
Timer1: TTimer;
OpenDialog1: TOpenDialog;
Image1: TImage;
Image3: TImage;
Image4: TImage;
ListBox1: TListBox;
Image5: TImage;
SaveDialog1: TSaveDialog;
TrackBar1: TTrackBar;
repeatimg: TImage;
ListBox2: TListBox;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure playimgClick(Sender: TObject);
procedure openimgClick(Sender: TObject);
procedure stopimgClick(Sender: TObject);
procedure pauseimgClick(Sender: TObject);
procedure Label1Click(Sender: TObject);
procedure Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure Image3Click(Sender: TObject);
procedure Image4Click(Sender: TObject);
procedure Image5Click(Sender: TObject);
procedure ListBox1DblClick(Sender: TObject);
procedure ListBox1Click(Sender: TObject);
procedure openimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure openimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure playimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure playimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure stopimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure stopimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure repeatimgClick(Sender: TObject);
procedure MediaPlayer1Notify(Sender: TObject);
procedure TrackBar1Change(Sender: TObject);
private
Private declarations
public
Public declarations
end;
var
Form1: TForm1;
repeat_bl : boolean = false;
mode : boolean;
musicindex : integer = -1;
exname : string ;
implementation
$R *.DFM
function move(str : string) : string;
//走马灯
var
s : string;
temp : char;
i : integer;
begin
temp := str[1];
s := str;
for i := 1 to length(str)-1 do
begin
s[i] := s[i+1];
str := s;
end;
s[length(str)] := temp;
move := s;
end;
function updatetitle (str : string) : string;
//读出歌名
var
temp , title : string;
i : integer;
begin
i := length(str);
while str[i] <> '\' do
begin
temp := temp + str[i];
i := i-1;
end;
i := length(temp);
while i > 0 do
begin
title := title + temp[i];
i := i - 1;
end;
for i := 1 to 85-length(title) do title := title +' ';
updatetitle := title;
end;
function timefmt(time : integer) : string;
//转换时间格式
begin
time := round(time / 1000);
timefmt := FormatFloat('00' , Trunc(time / 3600)) + ':' +
FormatFloat('00' , (Trunc(time / 60)) MOD 60) + ':' +
FormatFloat('00' , time MOD 60);
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
if MediaPlayer1.Position = MediaPlayer1.Length then
Begin
if repeat_bl then
Begin
MediaPlayer1.Open;
MediaPlayer1.Play;
End;
End;
if mediaplayer1.mode = mpplaying then
begin
if mode then
label1.caption := timefmt(mediaplayer1.position)
else
label1.caption := timefmt(mediaplayer1.Length - mediaplayer1.position );
label3.caption := move(label3.caption);
label2.caption := timefmt(mediaplayer1.length);
trackbar1.max := mediaplayer1.Length div 1000;
trackbar1.Position := mediaplayer1.position div 1000;
end
else
begin
label1.caption := '00:00:00';
label2.caption :='00:00:00';
label3.caption := updatetitle(mediaplayer1.filename);
timer1.Enabled := false;
trackbar1.Position := 0;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
reg : tregistry;
i : integer;
begin
exname := ExtractFilePath(Application.ExeName);
repeat_bl := false;
mode := true;//true 代表时间格式为 elapse,false 为remaining
reg := tregistry.create;
try
reg.rootkey := hkey_current_user;
if reg.openkey('\software\minmediaplayer',true) then
begin
if reg.readstring('filename') <> '' then
begin
mediaplayer1.FileName := reg.readstring('filename');
label3.caption := updatetitle(mediaplayer1.filename);
end;
end;
finally
reg.closekey;
reg.free;
end;
end;
procedure TForm1.playimgClick(Sender: TObject);
begin
if mediaplayer1.FileName <> '' then
case mediaplayer1.Mode of
mpPlaying :
begin
mediaplayer1.Pause;
timer1.Enabled := false;
end;
mpPaused :
begin
mediaplayer1.pause;
timer1.Enabled := true;
end
else
mediaplayer1.open;
mediaplayer1.Play;
timer1.Enabled := true;
end;
end;
procedure TForm1.openimgClick(Sender: TObject);
var
i : integer;
temp , title : string;
reg : tregistry;
begin
if opendialog1.execute then
if opendialog1.FilterIndex <> 4 then
begin
mediaplayer1.filename := opendialog1.filename;
mediaplayer1.open;
mediaplayer1.Play;
timer1.Enabled := true;
label3.caption := updatetitle(mediaplayer1.filename);
for i := 1 to 40-length(label3.caption) do label3.caption := label3.caption+' ';
//将档案路径储存於ms windows 注册表
reg := tregistry.create;
try
reg.rootkey := hkey_current_user;
if reg.openkey('\software\minmediaplayer',true) then
reg.writestring('filename',mediaplayer1.filename);
finally
reg.closekey;
reg.free;
end;
listbox1.Items.Add(updatetitle(mediaplayer1.filename));
listbox2.items.add(mediaplayer1.filename);
listbox1.Itemindex := listbox1.Items.count -1;
listbox2.ItemIndex := listbox2.Items.count -1;
end
else
begin
listbox2.items.LoadFromFile(opendialog1.FileName);
listbox1.Items.Clear;
for i := 0 to listbox2.Items.Count-1 do
listbox1.Items.add(updatetitle(listbox2.items.strings[i]));
end;
end;
procedure TForm1.stopimgClick(Sender: TObject);
begin
if mediaplayer1.Mode = mpplaying then
begin
mediaplayer1.Stop;
trackbar1.Position := 0 ;
timer1.Enabled := false;
label3.caption := updatetitle(mediaplayer1.filename);
label1.caption := '00:00:00';
end;
end;
procedure TForm1.pauseimgClick(Sender: TObject);
begin
mediaplayer1.Pause;
timer1.Enabled := not(timer1.enabled);
end;
procedure TForm1.Label1Click(Sender: TObject);
begin
mode := not(mode);
end;
procedure TForm1.Image1MouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
Const
Sc_DragMove:LongInt=$F012;
begin
if Button = mbLeft then
begin
ReleaseCapture;
SendMessage(handle,wm_SysCommand,Sc_DragMove,0);
end;
end;
procedure TForm1.Image3Click(Sender: TObject);
begin
close;
end;
procedure TForm1.Image4Click(Sender: TObject);
begin
Application.Minimize;
end;
procedure TForm1.Image5Click(Sender: TObject);
begin
if savedialog1.execute then listbox2.Items.SaveToFile(savedialog1.filename);
end;
procedure TForm1.ListBox1DblClick(Sender: TObject);
begin
mediaplayer1.filename := listbox2.Items[listbox1.itemindex];
listbox2.ItemIndex := listbox1.ItemIndex;
label3.caption := updatetitle(mediaplayer1.filename);
mediaplayer1.Open;
mediaplayer1.Play;
timer1.Enabled := true;
end;
procedure TForm1.ListBox1Click(Sender: TObject);
begin
listbox2.itemindex := listbox1.itemindex;
mediaplayer1.FileName := listbox2.Items.Strings[listbox2.itemindex];
listbox1.Hint := listbox2.Items[listbox1.itemindex];
end;
procedure TForm1.openimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
openimg.Picture.LoadFromFile(exname+'image/open_c.bmp');
end;
procedure TForm1.openimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
openimg.Picture.LoadFromFile(exname+'image/open.bmp');
end;
procedure TForm1.playimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
playimg.Picture.LoadFromFile(exname+'image/play_c.bmp');
end;
procedure TForm1.playimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
playimg.Picture.LoadFromFile(exname+'image/play.bmp');
end;
procedure TForm1.stopimgMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
stopimg.Picture.LoadFromFile(exname+'image/stop_c.bmp');
end;
procedure TForm1.stopimgMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
begin
stopimg.Picture.LoadFromFile(exname+'image/stop.bmp');
end;
procedure TForm1.repeatimgClick(Sender: TObject);
begin
repeat_bl := not(repeat_bl);
if repeat_bl then repeatimg.Picture.LoadFromFile(exname+'image/repeat_c.bmp')
else repeatimg.Picture.LoadFromFile(exname+'image/repeat.bmp');
end;
procedure TForm1.MediaPlayer1Notify(Sender: TObject);
begin
if not(repeat_bl) then
if listbox1.ItemIndex < listbox1.items.Count-1 then begin
timer1.Enabled := true;
listbox1.ItemIndex := listbox1.itemindex+1;
listbox2.itemindex := listbox2.itemindex+1;
mediaplayer1.FileName := listbox2.Items.Strings[listbox2.itemindex];
mediaplayer1.Open;
mediaplayer1.Play;
end;
end;
procedure TForm1.TrackBar1Change(Sender: TObject);
begin
if mediaplayer1.Mode = mpplaying then
begin
mediaplayer1.Position := trackbar1.Position*1000;
mediaplayer1.Play;
end;
end; 参考技术B MediaPlayer
也可以调用其他音乐插件 参考技术C 搜索:delphi bass
以上是关于关于delphi的mediaplayer的问题的主要内容,如果未能解决你的问题,请参考以下文章