Android开发学习之路--MediaPlayer之简单音乐播放器初体验
很多时候我们都会用手机来播放音乐,播放视频,那么具体地要怎么实现呢,其实主要是MediaPlayer类来完成的。下面通过简单的例子来实现一首歌曲的播放吧。新建工程MediaPlayerStudy,这里我们来简单地实现个音乐播放器,首先来简单布局下:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
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:stretchColumns="0"
tools:context="com.example.jared.mediaplayerstudy.MainActivity"> <TableRow>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:layout_gravity="center"
android:orientation="horizontal"> <TextView
android:id="@+id/musicName"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textSize="22dp"
android:layout_gravity="center"
android:text="Music Player"/>
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"> <TextView
android:id="@+id/playingTime"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="00:00"/>
<SeekBar
android:id="@+id/seek"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:max="100"/>
<TextView
android:id="@+id/totalTime"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="00:00"/>
</LinearLayout>
</TableRow>
<TableRow>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="10dp"
android:orientation="horizontal"> <Button
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="播放"/>
<Button
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="暂停"/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="停止"/>
</LinearLayout>
</TableRow>
</TableLayout>
效果如下所示:
package com.example.jared.mediaplayerstudy; import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.TextView; public class MainActivity extends AppCompatActivity { private Button playMusic;
private Button pauseMusic;
private Button stopMusic; private TextView totalTime_text;
private TextView playingTime_text; private SeekBar playingProcess; private int totalTime = 0; private MediaPlayer mediaPlayer = new MediaPlayer(); private Handler hangler = new Handler();
private boolean flag=true; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); playMusic = (Button)findViewById(R.id.play);
playMusic.setOnClickListener(new myOnClickListener()); pauseMusic = (Button)findViewById(R.id.pause);
pauseMusic.setOnClickListener(new myOnClickListener()); playingProcess = (SeekBar)findViewById(R.id.seek);
playingProcess.setOnSeekBarChangeListener(new mySeekBarListener()); totalTime_text = (TextView)findViewById(R.id.totalTime);
playingTime_text = (TextView)findViewById(R.id.playingTime); initMediaPlayer();
} public void setTotalTime() {
totalTime = mediaPlayer.getDuration() / 1000;
Log.d("MediaPlayerTest", String.valueOf(totalTime));
String pos = String.valueOf(totalTime/60/10)+String.valueOf(totalTime/60%10)
+':'+String.valueOf(totalTime%60/10)+String.valueOf(totalTime%60%10);
totalTime_text.setText(pos);
playingProcess.setProgress(0);
playingProcess.setMax(totalTime);
} public void updateTimepos() {
int timepos = playingProcess.getProgress()+1;
if(timepos >= totalTime-1) {
timepos = 0;
flag = false;
}
playingProcess.setProgress(timepos);
String pos = String.valueOf(timepos/60/10)+String.valueOf(timepos/60%10)
+':'+String.valueOf(timepos%60/10)+String.valueOf(timepos%60%10);
playingTime_text.setText(pos); } public void initMediaPlayer() {
try {
String file_path = "/sdcard/qqmusic/song/daoxiang.mp3";
//File file = new File(Environment.getExternalStorageDirectory(), "music.mp3");
mediaPlayer.setDataSource(file_path);
mediaPlayer.prepare();
setTotalTime();
} catch (Exception e) {
e.printStackTrace();
}
} private void refreshTimepos() {
new Thread(new Runnable() {
@Override
public void run() {
while(flag && playingProcess.getProgress()<totalTime-1) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
hangler.post(new Runnable() {
@Override
public void run() {
updateTimepos();
}
});
}
}
}).start();
} private class mySeekBarListener implements SeekBar.OnSeekBarChangeListener {
@Override
public void onProgressChanged(SeekBar seekBar, int i, boolean b) { } @Override
public void onStartTrackingTouch(SeekBar seekBar) { } @Override
public void onStopTrackingTouch(SeekBar seekBar) {
mediaPlayer.seekTo( playingProcess.getProgress()*1000);
updateTimepos();
}
} private class myOnClickListener implements View.OnClickListener {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.play:
setPlayMusic();
break;
case R.id.pause:
setPauseMusic();
break;
}
}
} public void setPlayMusic() {
if (!mediaPlayer.isPlaying()) {
mediaPlayer.start();
flag = true;
refreshTimepos();
}
} public void setPauseMusic() {
if(mediaPlayer.isPlaying()) {
mediaPlayer.pause();
flag = false;
}
}
}
对于按键的监听就不多说了,这里还学了seekbar,seekbar的话其实和一般的也差不多。当seek的点改变的时候,那么就会调用onProgressChanged,当拖动的时候手指还没有弹起的时候调用onStartTrackingTouch,当拖动手指弹起的时候调用onStopSTrackingTouch。
Android开发学习之路--MediaPlayer之简单音乐播放器初体验的更多相关文章
- Android开发学习之路--UI之简单聊天界面
学了很多的ui的知识,这里就来实现个聊天的界面,首先来实现个layout的xml,代码如下: <?xml version="1.0" encoding="utf-8 ...
- Android开发学习之路--RxAndroid之简单原理
学习了RxAndroid,其实也就是RxJava了,但是还是不是非常清楚到底RxAndroid有什么用呢?为什么要使用RxAndroid呢?这篇文章讲得不错,RxJava的原理.但是这里还是把整个 ...
- Web开发学习之路--Eclipse+Tomcat+mysql之初体验
学习了一段时间android,正好要用到android和服务器之间的交互,既然要学习android,那么就涉猎下服务器端的开发了,以前学过php,用thinkphp很快可以搭建起来,但是android ...
- Android开发学习之路--基于vitamio的视频播放器(二)
终于把该忙的事情都忙得差不多了,接下来又可以开始good good study,day day up了.在Android开发学习之路–基于vitamio的视频播放器(一)中,主要讲了播放器的界面的 ...
- Android开发学习之路-RecyclerView滑动删除和拖动排序
Android开发学习之路-RecyclerView使用初探 Android开发学习之路-RecyclerView的Item自定义动画及DefaultItemAnimator源码分析 Android开 ...
- Android开发学习之路--Android Studio cmake编译ffmpeg
最新的android studio2.2引入了cmake可以很好地实现ndk的编写.这里使用最新的方式,对于以前的android下的ndk编译什么的可以参考之前的文章:Android开发学习之路– ...
- Android开发学习之路--网络编程之xml、json
一般网络数据通过http来get,post,那么其中的数据不可能杂乱无章,比如我要post一段数据,肯定是要有一定的格式,协议的.常用的就是xml和json了.在此先要搭建个简单的服务器吧,首先呢下载 ...
- Android开发学习之路--Activity之初体验
环境也搭建好了,android系统也基本了解了,那么接下来就可以开始学习android开发了,相信这么学下去肯定可以把android开发学习好的,再加上时而再温故下linux下的知识,看看androi ...
- Android开发学习之路--Android系统架构初探
环境搭建好了,最简单的app也运行过了,那么app到底是怎么运行在手机上的,手机又到底怎么能运行这些应用,一堆的电子元器件最后可以运行这么美妙的界面,在此还是需要好好研究研究.这里从芯片及硬件模块-& ...
随机推荐
- Delphi 打印 Tprinter
打印 打印对于许多 Windows 程序员来说是十分棘手的问题. Delphi 简化了打印时用户所必须了解的大部分内容.用户可以很轻松地写出简单的打印程序来输出文本和位图化了的图像. ...
- 转载:使用Math.floor和Math.random取随机整数
Math.random():获取0~1随机数 Math.floor() method rounds a number DOWNWARDS to the nearest integer, and ret ...
- JFinal 极速开发框架的优点和不足的地方
http://www.360doc.com/content/16/1226/10/31460730_617731802.shtml http://www.sohu.com/a/122571150_46 ...
- 索引法则--IS NULL, IS NOT NULL 也无法使用索引
Mysql 系列文章主页 =============== 1 数据准备 1.1 建表 DROP TABLE IF EXISTS staff; CREATE TABLE IF NOT EXISTS st ...
- EM vs REM vs PX,为什么你不应该”只用px“”
Actually this artical is from other person's opnion ,i just put it into chinese,and this means a ver ...
- 聊聊并发(一)深入分析Volatile的实现原理
本文属于作者原创,原文发表于InfoQ:http://www.infoq.com/cn/articles/ftf-java-volatile 引言 在多线程并发编程中synchronized和Vola ...
- 关于一些基础的Java问题的解答(二)
6. Hashcode的作用 官方对于hashCode的解释如下: Whenever it is invoked on the same object more than once during an ...
- 基于babylon3D模型研究3D骨骼动画(1)
3D骨骼动画是实现较为复杂3D场景的重要技术,Babylon.js引擎内置了对骨骼动画的支持,但Babylon.js使用的骨骼动画的模型多是从3DsMax.Blender等3D建模工具转换而来,骨骼动 ...
- Java不走弯路教程(3.用户验证与文件内容查询)
3.用户验证与文件内容查询 在上一章中,我们完成了对指定文件内容的输出操作. 我们现在有如下格式的文件product.db id,product_name,product_detail 1,noteb ...
- 实验与作业(Python)-05 程序的控制结构
推荐完成顺序: 1->2->3->4.1->4.4->5->4.5->4.7->6 截止日期 下次实验课之前 实验目标 if-elif-else 循环: ...