android中MediaPlayer类的用法
用法直接看sample
package com.turtle920.androidaudioprocess; import android.media.MediaPlayer;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button; import java.io.IOException; public class MainActivity extends AppCompatActivity { MediaPlayer mp = new MediaPlayer();
float leftVol=0.5f, rightVol=0.5f;
String song = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/tf.mp3"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); try {
mp.setDataSource(song);
mp.prepare();
mp.setVolume(leftVol,rightVol);
} catch (Exception e) {
Log.e("DEBUG", "" + e.toString());
e.printStackTrace();
} Button button1 = (Button) findViewById(R.id.button_start);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.start();
}
}); Button button2 = (Button) findViewById(R.id.button_pause);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
}); Button button3 = (Button) findViewById(R.id.button_replay);
button3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.stop();
try {
mp.prepare();
} catch (IOException e) {
e.printStackTrace();
}
}
}); Button button4 = (Button)findViewById(R.id.button_l);
button4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (leftVol+0.05f<=1.0f) leftVol+=0.05f;
if (rightVol-0.05f>=0f) rightVol-=0.05f;
Log.e("DEBUG",""+leftVol+" "+rightVol);
mp.setVolume(leftVol, rightVol);
}
}); Button button5 = (Button)findViewById(R.id.button_r);
button5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (leftVol-0.05f>=0f) leftVol-=0.05f;
if (rightVol+0.05f<=1.0f) rightVol+=0.05f;
Log.e("DEBUG",""+leftVol+" "+rightVol);
mp.setVolume(leftVol, rightVol);
}
}); }
}
注意要在manifest里申明权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
另外如果真机环境API 23+ 还需要在机子上给app读取SD卡得权限
android中MediaPlayer类的用法的更多相关文章
- android中Handle类的用法
android中Handle类的用法 当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无 ...
- [转]Android中Application类的用法
原文链接:http://www.cnblogs.com/renqingping/archive/2012/10/24/Application.html Application类 Application ...
- Android中Calendar类的用法总结
Calendar是Android开发中需要获取时间时必不可少的一个工具类,通过这个类可以获得的时间信息还是很丰富的,下面做一个总结,以后使用的时候就不用总是去翻书或者查资料了. 在获取时间之前要先获得 ...
- android中PreferenceScreen类的用法
PreferenceScreen preference是偏好,首选的意思,PreferenceScreen个人翻译成 “偏好显示”,明白这个意思就好,就是说根据特点灵活的定义显示内容风格,一个屏幕可以 ...
- 三、android中Handle类的用法
当我们在处理下载或是其他需要长时间执行的任务时,如果直接把处理函数放Activity的OnCreate或是OnStart中,会导致执行过程中整个Activity无响应,如果时间过长,程序还会挂掉.Ha ...
- Android中Cursor类的概念和用法[转]
首页 > 程序开发 > 移动开发 > Android > 正文 Android中Cursor类的概念和用法 2011-09-07 0个评论 收藏 ...
- Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法
Android中View类OnClickListener和DialogInterface类OnClickListener冲突解决办法 如下面所示,同时导入这两个,会提示其中一个与另一个产生冲突. 1i ...
- C#中timer类的用法
C#中timer类的用法 关于C#中timer类 在C#里关于定时器类就有3个 1.定义在System.Windows.Forms里 2.定义在System.Threading.Timer类 ...
- Android中一个类实现的接口数不能超过七个
近期一段时间,在开发Android应用程序的过程中,发现Android中一个类实现的接口数超过七个的时候,常常会出现超过第7个之后的接口不能正常使用.
随机推荐
- 使用net.sf.json包提供的JSONObject.toBean方法时,日期转化错误解决办法
解决办法: 需要在toBean之前添加配置 String[] dateFormats = new String[] {"yyyy-MM-dd HH:mm:ss"}; JSONUti ...
- Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists).
Git fetch和git pull的区别, 解决Git报错:error: You have not concluded your merge (MERGE_HEAD exists). Git fet ...
- 工作队列work queues 公平分发(fair dispatch) And 消息应答与消息持久化
生产者 package cn.wh.work; import cn.wh.util.RabbitMqConnectionUtil; import com.rabbitmq.client.Channel ...
- Spring Boot的核心
1.1.1. 入口类和@SpringBootApplication Spring Boot的项目一般都会有*Application的入口类,入口类中会有main方法,这是一个标准的Java应用程序 ...
- SQL使用事务
BEGIN TRANSACTION--开始事务 DECLARE @errorSun INT --定义错误计数器 SET @errorSun=0 --没错为0 UPDATE a SET id=232 W ...
- LeetCode第[55]题(Java):Jump Game
题目:跳跳游戏 难度:Medium 题目内容: Given an array of non-negative integers, you are initially positioned at the ...
- python线程、进程和协程
链接:http://www.jb51.net/article/88825.htm 引言 解释器环境:python3.5.1 我们都知道python网络编程的两大必学模块socket和socketser ...
- 二 web爬虫,scrapy模块以及相关依赖模块安装
当前环境python3.5 ,windows10系统 Linux系统安装 在线安装,会自动安装scrapy模块以及相关依赖模块 pip install Scrapy 手动源码安装,比较麻烦要自己手动安 ...
- OpenCV几种边缘检测的简例
简单记录一下OpenCV的几种边缘检测函数的用法. 边缘检测算法 以Sobel边缘检测算法为例. Sobel卷积核模板为: 偏导公式为: Gx(i,j)=[f(i+1,j−1)+2f(i+1,j)+f ...
- 51nod 1640 MST+二分
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1640 1640 天气晴朗的魔法 题目来源: 原创 基准时间限制:1 秒 ...