简单实现Android本地音乐播放器
作者:安娇德 发布时间:2021-09-04 19:28:28
标签:Android,播放器
音乐播放需要调用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("暂停");
}
}


猜你喜欢
- 详解Kotlin中的面向对象(二)在Kotlin中的面向对象(一)中,介绍了Kotlin类的相关操作,本文将在上文的基础上,继续介绍属性、接
- 引言native image是GraalVM中提供的一个命令,可以把字节码文件或Jar包编译成为一个二进制可执行文件,同时它自己也是用Jav
- 当我们使用swagger,进行接口测试,怕接口不安全,担心暴露。可采用两种方式1.环境权限配置对swagger文档配置只在测试环境可访问,生
- 背景描述通常如果需要一次更新多条数据有两个方式:(1)在业务代码中循环遍历逐条更新。(2)一次性更新所有数据(更准确的说是一条sql语句来更
- 下拉刷新对于一个app来说是必不可少的一个功能,在早期大多数使用的是chrisbanes的PullToRefresh,或是修改自该框架的其他
- 本文实例讲述了Android滑动按钮事件。分享给大家供大家参考,具体如下:今天纪录一下滑动按钮功能。。首先效果图:然后是分别建立三个文件,第
- 新项目Android和ios要做成统一样式,年龄,性别,时间,要做成滚轮效果,Android没有原生控件,只能自己定义,于是我较劲脑汁,终于
- 这篇文章主要介绍了Java String的intern用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,
- 前言为什么要动态设置字体大小?由于项目面对的是中老年客户项目中自带的字体无法满足客户需求。Android应用字体大小默认随系统设置的字体大小
- 前言Spring官方最近宣布,将在Spring Framework 5.0版本中正式支持Kotlin语言。这意味着Spring Boot 2
- 一、题目描述题目实现:网络通信,实现信息的发送和接收。二、解题思路创建一个服务器类:ServerSocketFrame,继承JFrame类写
- Android中socket通信简单实现,供大家参考,具体内容如下socket通信需要有一个服务器和客户端,可以把同一个APP作为服务器跟客
- Java中的引用类型有哪几种?Java中的引用类型分成 强引用 , 软引用 , 弱引用 , 虚引用 。1、强引用没有引用指向这个对象,垃圾回
- public class PersonAdapter extends BaseAdapter { private List per
- 我们要使用java来操作redis什么是Jedis?什么是Jedis 是Redis官方推荐的java连接开发工具!使用Java操作Redis
- 最近写到了一个秒杀的功能模块,为了保证高并 * 况下不会宕机,要从多方面去考虑,当前的限流操作只是其中的一个方面,具体操作如下。导入所需依赖&
- Intro做项目的时候,页面上有一些敏感信息,需要用“*”隐藏一些比较重要的信息,于是打算写一个通用的方法。Let's do it
- 本文实例讲述了C#使用HttpDownLoadHelper下载文件的方法。分享给大家供大家参考。具体实现方法如下:using System;
- 在winform里拖入一个datagridview控件,跟一个openfiledialog控件using System;using Syst
- Android内部没有控件来直接显示文档,跳转WPS或其他第三方文档App体验性不好,使用腾讯X5内核能很好的解决的这一问题。一、下载腾讯X