简单实现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("暂停");
}
}
0
投稿
猜你喜欢
- 代码实例一 using System; using System.IO; using System.Collections; using S
- 前言不知道你是否参加过拼多多上邀请微信好友砍价功能,这个功能实现首先需要考虑的就是获取微信用户的信息。获取用户信息就是获取公众号下微信用户的
- C#中Invoke的用法()invoke和begininvoke 区别一直对invoke和begininvoke的使用和概念比较混乱,这两天
- 本文实例讲述了C#提取网页中超链接link和text部分的方法。分享给大家供大家参考,具体如下:string s = "..&qu
- LRU:Least Recently Used最近最少使用,当缓存容量不足时,先淘汰最近最少使用的数据。就像JVM垃圾回收一样,希望将存活的
- 在你的jar文件当前目录中建立一个bat文件:内容是:注意文件名要对应@echo offSTART "commandServer&
- spring-mybatis获取mapper方式汇总项目背景:pojo下面有一个user实体类Dao包下面写了usermapper.xml
- (一) collection和collections这两者均位于java.util包下,不同的是:collection是一个集合接口,有Li
- HashMap 在不同的 JDK 版本下的实现是不同的,在 JDK 1.7 时,HashMap 底层是通过数组 + 链表实现的;而在 JDK
- 本文实例为大家分享了Java实现简单贪吃蛇游戏的具体代码,供大家参考,具体内容如下贪吃蛇小游戏制作方法首先需要的准备有:1、掌握Java基础
- 一、为什么要控制当你在项目启动时需要提前做一个业务的初始化工作时,或者你正在开发某个中间件需要完成自动装配时。你会声明自己的Configur
- 1.委托delegate委托delegate也是一种类型,在任何可以声明类的地方都可以声明委托,它将方法当做另一个方法的参数进行传递,这样就
- Java中线程的创建有两种方式: 1. 通过继承Thread类,重写Thread的run()方法,将线程运行的逻辑放在
- 下面的这些都算是比较高级的问题了,面试中一般也很少问到,因为它们可能会把面试者拒之门外。不过你可以自己找个时间来实践一下。 1.
- 这里使用的是dynamic-datasource-spring-boot-starter ,它是一个基于springboot的快速集成多数据
- 今天来分析Configuration初始化的最后一部分mapper的加载。加载方法mapperElementXMLConfigBuilder
- 引言mysql 和 oracle 插入的时候有一个很大的区别是:oracle 支持序列做 id;mysql 本身有一个列可以做自增长字段。m
- 主要完成任务:1.read read 并行化2.read write 不允许3.write write 不允许public class Re
- springboot整合nacos1、引入依赖<dependency> <groupId>
- 案例简述通过C#使用类似QQ窗体的功能,当窗体放置到屏幕的边缘,可以将窗体隐藏,当鼠标再次放置到屏幕边缘时,窗体可再次显示。预备知识导图功能