简单实现Android本地音乐播放器
作者:安娇德 时间:2021-09-04 19:28:28
音乐播放需要调用service,在此,只是简单梳理播放流程。
public class PlayMusicService extends Service {
//绑定服务 调用服务的方法。
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<EditText
android:id="@+id/et_path"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="请输入要播放文件的路径" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="@+id/bt_play"
android:onClick="play"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="播放" />
<Button
android:id="@+id/bt_pause"
android:onClick="pause"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="暂停" />
<Button
android:id="@+id/bt_stop"
android:onClick="stop"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="停止" />
<Button
android:id="@+id/bt_replay"
android:onClick="replay"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="重播" />
</LinearLayout>
</LinearLayout>
public class MainActivity extends Activity {
private EditText et_path;
private MediaPlayer mediaPlayer;
private Button bt_play,bt_pause,bt_stop,bt_replay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_path = (EditText) findViewById(R.id.et_path);
bt_play = (Button) findViewById(R.id.bt_play);
bt_pause = (Button) findViewById(R.id.bt_pause);
bt_stop = (Button) findViewById(R.id.bt_stop);
bt_replay = (Button) findViewById(R.id.bt_replay);
}
/**
* 播放
* @param view
*/
public void play(View view) {
String filepath = et_path.getText().toString().trim();
File file = new File(filepath);
if(file.exists()){
try {
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filepath);//设置播放的数据源。
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.prepare();//准备开始播放 播放的逻辑是c代码在新的线程里面执行。
mediaPlayer.start();
bt_play.setEnabled(false);
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
bt_play.setEnabled(true);
}
});
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, "播放失败", 0).show();
}
}else{
Toast.makeText(this, "文件不存在,请检查文件的路径", 0).show();
}
}
/**
* 暂停
* @param view
*/
public void pause(View view) {
if("继续".equals(bt_pause.getText().toString())){
mediaPlayer.start();
bt_pause.setText("暂停");
return;
}
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.pause();
bt_pause.setText("继续");
}
}
/**
* 停止
* @param view
*/
public void stop(View view) {
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer = null;
}
bt_pause.setText("暂停");
bt_play.setEnabled(true);
}
/**
* 重播
* @param view
*/
public void replay(View view) {
if(mediaPlayer!=null&&mediaPlayer.isPlaying()){
mediaPlayer.seekTo(0);
}else{
play(view);
}
bt_pause.setText("暂停");
}
}
标签:Android,播放器
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C#计算字符串哈希值(MD5、SHA)的方法小结
2022-07-02 15:47:55
![](https://img.aspxhome.com/file/2023/8/68628_0s.png)
Java二维数组实战案例
2022-08-13 08:59:25
![](https://img.aspxhome.com/file/2023/6/63996_0s.png)
Java+MySQL 图书管理系统
2023-11-28 19:28:43
![](https://img.aspxhome.com/file/2023/7/60757_0s.png)
WPF弹出自定义窗口的方法
2023-10-18 20:52:04
java安全编码指南之:Mutability可变性详解
2023-11-11 06:30:24
![](https://img.aspxhome.com/file/2023/3/59073_0s.jpg)
Java循环队列原理与用法详解
2023-11-13 20:05:36
![](https://img.aspxhome.com/file/2023/4/61574_0s.png)
java基于swing实现的五子棋游戏代码
2023-09-24 17:31:17
![](https://img.aspxhome.com/file/2023/1/63381_0s.jpg)
java中Integer包装类装箱的一个细节详解
2023-09-24 18:35:32
SpringDataElasticsearch与SpEL表达式实现ES动态索引
2021-11-27 01:33:03
![](https://img.aspxhome.com/file/2023/8/61558_0s.png)
让Java后台MySQL数据库能够支持emoji表情的方法
2022-12-30 04:24:45
我用java实现了王者荣耀的皮肤和英雄技能
2022-01-13 13:44:09
![](https://img.aspxhome.com/file/2023/1/63491_0s.png)
java文件上传下载代码实例
2023-11-10 05:06:14
SpringSceurity实现短信验证码登陆
2023-06-23 00:37:35
![](https://img.aspxhome.com/file/2023/2/77102_0s.jpg)
Java由浅入深带你了解什么是包package
2022-04-17 02:33:39
![](https://img.aspxhome.com/file/2023/4/76594_0s.png)
Java截取字符串的方法
2023-11-07 09:30:02
JAVA中数组从小到大排序的2种方法实例
2021-10-09 09:46:18
Spring Boot下的Job定时任务
2021-10-23 05:16:14
ssm框架下web项目,web.xml配置文件的作用(详解)
2021-07-31 14:42:11
Java深入了解数据结构中常见的排序算法
2021-11-02 23:33:53
![](https://img.aspxhome.com/file/2023/8/68138_0s.png)
Android DragVideo实现播放视频时任意拖拽的方法
2022-05-24 03:45:48
![](https://img.aspxhome.com/file/2023/5/88115_0s.png)