黑马程序员安卓教程音乐播放器.docx

上传人:b****3 文档编号:1056180 上传时间:2022-10-16 格式:DOCX 页数:11 大小:244.69KB
下载 相关 举报
黑马程序员安卓教程音乐播放器.docx_第1页
第1页 / 共11页
黑马程序员安卓教程音乐播放器.docx_第2页
第2页 / 共11页
黑马程序员安卓教程音乐播放器.docx_第3页
第3页 / 共11页
黑马程序员安卓教程音乐播放器.docx_第4页
第4页 / 共11页
黑马程序员安卓教程音乐播放器.docx_第5页
第5页 / 共11页
点击查看更多>>
下载资源
资源描述

黑马程序员安卓教程音乐播放器.docx

《黑马程序员安卓教程音乐播放器.docx》由会员分享,可在线阅读,更多相关《黑马程序员安卓教程音乐播放器.docx(11页珍藏版)》请在冰豆网上搜索。

黑马程序员安卓教程音乐播放器.docx

黑马程序员安卓教程音乐播放器

音乐播放器

1.5.1准备工作

Android官方提供了MediaPlayer核心类,用于播放音乐,其状态流程如图1-12所示。

MediaPlayer必

须严格按照状态图操作,否那么就会出现错误,这些错误都是底层抛出,严格按照状态图操作的话一般就不会出问题。

使用MediaPlayer播放音乐的核心方法如下所示:

1.

MediaPlayer

player

=

new

MediaPlayer();

创立对象

2.

player.reset();

重置为初始状态

3.player.setAudioStreamType(AudioManager.STREAM_MUSIC);声音流类型

4.player.setDataSource(“/mnt/sdcard/test.mp3〞);设置音频源

5.

player.prepare();

准备

6.

player.start();

开场或恢复播放

7.

player.pause();

暂停播放

8.

player.start();

恢复播放

9.

player.stop();

停顿播放

10.

player.release();

释放资源

为了演示MediaPlayer的使用,我们需要提前准备一个mp3文件放到sdcard中。

需求:

制作一个播放器,能够播放/暂停/停顿音乐文件,并且添加一个SeekBar〔可以拖拽的ProgressBar〕,当音乐播放时SeekBar也会不断的跟新当前的进度,当用户拖动SeekBar时可以更改播放的进度。

 

23

 

图1-12MediaPlayer状态流程图

 

1.5.2编写布局

 

【文件1-7】activity_main.xml

1.

android="schemas.android./apk/res/android"

2.android:

layout_width="match_parent"

3.android:

layout_height="match_parent"

4.android:

orientation="vertical">

5.

6.

7.android:

layout_width="match_parent"

24

 

8.android:

layout_height="wrap_content"

9.android:

orientation="horizontal"

10.>

11.

12.android:

onClick="play"

13.android:

layout_width="0dp"

14.android:

layout_weight="1"

15.android:

layout_height="wrap_content"

16.android:

text="播放"

17./>

18.

19.android:

onClick="pause"

20.android:

layout_width="0dp"

21.android:

layout_weight="1"

22.android:

layout_height="wrap_content"

23.android:

text="暂停"

24./>

25.

26.android:

onClick="stop"

27.android:

layout_width="0dp"

28.android:

layout_weight="1"

29.android:

layout_height="wrap_content"

30.android:

text="停顿"

31./>

32.

33.

34.

35.

36.android:

layout_width="match_parent"

37.android:

layout_height="wrap_content"

38.android:

id="+id/sb"

39./>

40.

 

1.5.3代码实现

 

【文件1-8】MainActivity.java

1.package.example.musicplayer;

2.

3.importjava.util.Timer;

4.importjava.util.TimerTask;

5.importandroid.media.AudioManager;

6.importandroid.media.MediaPlayer;

25

 

7.importandroid.os.Bundle;

8.importandroid.view.View;

9.importandroid.widget.SeekBar;

10.importandroid.widget.SeekBar.OnSeekBarChangeListener;

11.importandroid.widget.Toast;

12.importandroid.app.Activity;

13./**

14.*实现音乐播放器

15.*

16.*authorwzy2016-1-28

17.*

18.*/

19.publicclassMainActivityextendsActivityimplementsOnSeekBarChangeListener{

20.

21.privateSeekBarsb;

22.privateMediaPlayerplayer;

23.privateintduration;

24.//播放器的几个状态

25.privatestaticfinalintPLAYING=1;//播放状态

26.privatestaticfinalintPAUSING=2;//暂停状态

27.privatestaticfinalintSTOPPING=3;//停顿状态

28.privatevolatileintCURRENT=0;//当前状态

29.privateTimertimer;

30.

31.Override

32.protectedvoidonCreate(BundlesavedInstanceState){

33.super.onCreate(savedInstanceState);

34.setContentView(R.layout.activity_main);

35.sb=(SeekBar)findViewById(R.id.sb);

36.//设置拖动监听

37.sb.setOnSeekBarChangeListener(this);

38.}

39./**

40.*播放

41.*/

42.publicvoidplay(Viewview){

43.if(player!

=null){

44.if(CURRENT==PLAYING){

45.Toast.makeText(this,"音乐已经在播放了",Toast.LENGTH_SHORT).show();

46.return;

47.}elseif(CURRENT==PAUSING){

48.player.start();

49.CURRENT=PLAYING;

50.return;

26

 

51.}

52.}

53.try{

54.//创立一个播放器对象

55.player=newMediaPlayer();

56.//给播放器设置音乐路径

57.player.setDataSource("/mnt/sdcard/test.mp3");

58.//设置音乐格式

59.player.setAudioStreamType(AudioManager.STREAM_MUSIC);

60.//准备

61.player.prepare();

62.//获取音乐最大长度〔毫秒单位〕

63.duration=player.getDuration();

64.//给SeekBar设置最大值

65.sb.setMax(duration);

66.//音乐开场播放

67.player.start();

68.//设置当前的状态为播放

69.CURRENT=PLAYING;

70.if(timer==null){

71.//创立定时器

72.timer=newTimer();

73.}

74./**

75.*参数1:

匿名内部类,相当于Runnable类

76.*参数2:

第一次延时多长时间〔毫秒〕后执行,0那么代表立即执行

77.*参数3:

每隔多长时间(毫秒)执行一次

78.*/

79.timer.schedule(newTimerTask(){

80.

81.Override

82.publicvoidrun(){//该方法每1秒被调用一次

83.

if

(CURRENT==PLAYING)

{

84.

runOnUiThread(new

Runnable()

{

85.

86.

Override

87.

public

void

run()

{

88.

//

双重判断,尽可能防止线程问题,因为该段代码时在主线程中的,

89.

//

第一次判断是在子线程中进展的

90.

if

(player!

=null&&CURRENT==PLAYING)

{

91.

//

获取当前的播放进度

92.

int

currentPosition

=

player.getCurrentPosition();

93.

//

设置给

SeekBar

94.

sb.setProgress(currentPosition);

95.

}

27

 

96.

}

97.});

98.}

99.

100.}

101.

},

0,

1000);

102.

103.}catch(Exceptione){

104.e.printStackTrace();

105.Toast.makeText(this,"音乐播放失败"+e,0).show();

106.}

107.}

108./**

109.*暂停

110.*/

111.publicvoidpause(Viewview){

112.if(player!

=null&&CURRENT==PLAYING){

113.player.pause();

114.CURRENT=PAUSING;

115.}

116.}

117./**

118.*停顿

119.*/

120.publicvoidstop(Viewview){

121.if(pla

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > IT计算机 > 电脑基础知识

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1