码迷,mamicode.com
首页 > 移动开发 > 详细

Android实例-MediaPlayer播放音乐和视频(XE8+小米2)

时间:2015-09-06 21:42:50      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

结果:

1.播放视频需要手动放入MediaPlayerControl1控件,设置MediaPlayerControl1.MediaPlayer := MediaPlayer1;

2.播放声音文件正常,但播放器视时发现,第一边正常第二边就报错了(哪位大神知道的,请M我哦,先谢谢了)。

3.打包时需要打入MP3与MP4文件,路径为"assets\internal\"。

 

实例代码:

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  8   FMX.Controls.Presentation, FMX.StdCtrls, FMX.Media, FMX.Layouts;
  9 
 10 type
 11   TForm1 = class(TForm)
 12     MediaPlayer1: TMediaPlayer;
 13     Label1: TLabel;
 14     Timer1: TTimer;
 15     Layout2: TLayout;
 16     Layout3: TLayout;
 17     Button1: TButton;
 18     Button2: TButton;
 19     Button3: TButton;
 20     TrackBar1: TTrackBar;
 21     TrackBar2: TTrackBar;
 22     Label2: TLabel;
 23     Label3: TLabel;
 24     Layout4: TLayout;
 25     MediaPlayerControl1: TMediaPlayerControl;
 26     Layout1: TLayout;
 27     procedure Button1Click(Sender: TObject);
 28     procedure Button2Click(Sender: TObject);
 29     procedure TrackBar1Change(Sender: TObject);
 30     procedure Timer1Timer(Sender: TObject);
 31     procedure Button3Click(Sender: TObject);
 32     procedure TrackBar2Change(Sender: TObject);
 33     procedure FormCreate(Sender: TObject);
 34   private
 35     { Private declarations }
 36   public
 37     { Public declarations }
 38   end;
 39 
 40 var
 41   Form1: TForm1;
 42 
 43 implementation
 44 uses
 45   System.IOUtils;//需要引入
 46 {$R *.fmx}
 47 {$R *.NmXhdpiPh.fmx ANDROID}
 48 
 49 //执行暂停按钮
 50 procedure TForm1.Button1Click(Sender: TObject);
 51 begin
 52   MediaPlayer1.Stop;
 53   Timer1.Enabled := false;
 54   Button2.Enabled := true;
 55   Button1.Enabled := false;
 56 end;
 57 
 58 //执行播放按钮
 59 procedure TForm1.Button2Click(Sender: TObject);
 60 begin
 61   MediaPlayer1.FileName := TPath.GetDocumentsPath + /abc.mp3;//指定要打开的文件当然,指定 SD 卡中其它目录是文件也是可以的。
 62   MediaPlayer1.CurrentTime := trunc((TrackBar1.Value * MediaPlayer1.Duration) / 100);// 播放进度调整为进度条指示位置
 63   MediaPlayer1.Volume := TrackBar2.Value;//音量也调整到音量指示值
 64   MediaPlayer1.Play;//播放
 65   Timer1.Enabled := true;
 66   Button2.Enabled := false;
 67   Button1.Enabled := true;
 68 end;
 69 
 70 procedure TForm1.Button3Click(Sender: TObject);
 71 begin
 72   MediaPlayer1.FileName := IncludeTrailingPathDelimiter(TPath.GetDocumentsPath) + abc.mp4;//指定要播放的视频文件
 73   MediaPlayer1.Play;
 74   Timer1.Enabled := true;
 75 end;
 76 
 77 //设置视频在哪里显示
 78 procedure TForm1.FormCreate(Sender: TObject);
 79 begin
 80   MediaPlayerControl1.MediaPlayer := MediaPlayer1;
 81 end;
 82 
 83 procedure TForm1.Timer1Timer(Sender: TObject);
 84 var
 85   handler: TNotifyEvent;
 86 begin
 87   handler := TrackBar1.OnChange;
 88   TrackBar1.OnChange := nil;//暂时取消其 OnChange 事件
 89   TrackBar1.Value := int((MediaPlayer1.CurrentTime / MediaPlayer1.Duration) * 100);
 90   //更新进度条的指示
 91   Label2.Text := inttostr(trunc(MediaPlayer1.CurrentTime / 60000)) + : +
 92   inttostr(trunc(MediaPlayer1.CurrentTime / 1000 -
 93   trunc(MediaPlayer1.CurrentTime / 60000) * 60));
 94   //显示声音文件所剩下的播放时间,分:秒
 95   Label3.Text := inttostr(trunc(MediaPlayer1.Duration / 60000)) + : +
 96   inttostr(trunc(MediaPlayer1.Duration / 1000 - trunc(MediaPlayer1.Duration /
 97   60000) * 60));//显示声音文件总的播放时间,分:秒
 98   TrackBar1.OnChange := handler; //还原其 OnChange 事件
 99   //播放完毕后的处理
100   if MediaPlayer1.CurrentTime >= MediaPlayer1.Duration - 1000 then//如果播放完了
101   begin
102     MediaPlayer1.Stop;
103     TrackBar1.Value:=0;
104     Button2.Enabled := true;
105     Button1.Enabled := false;
106     Timer1.Enabled := false;
107   end;
108 end;
109 
110 //滑动进度条时,改变播放进度
111 procedure TForm1.TrackBar1Change(Sender: TObject);
112 begin
113   MediaPlayer1.CurrentTime := trunc((TrackBar1.Value * MediaPlayer1.Duration) / 100);
114 end;
115 
116 //滑动音量调节条时,改变播放音量
117 procedure TForm1.TrackBar2Change(Sender: TObject);
118 begin
119   MediaPlayer1.Volume := TrackBar2.Value;
120 end;
121 
122 end.

 

Android实例-MediaPlayer播放音乐和视频(XE8+小米2)

标签:

原文地址:http://www.cnblogs.com/FKdelphi/p/4787029.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!