播放应用内的音频文件
- 首先在res文件下新建一个文件夹叫raw,然后把音频文件拷进去,来让我们播放。
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main"
android:layout_width="match_parent" android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="horizontal"
tools:context="com.example.zy_musicplay.MainActivity">
<SeekBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/seekBar"
android:layout_weight="1"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_media_play"
android:onClick="isPlayOrPause"
/>
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
private MediaPlayer mediaPlayer;
private SeekBar seekBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
seekBar = (SeekBar) findViewById(R.id.seekBar);
//给进度条设置拖到事件
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
//获取拖到后的进度
int progress = seekBar.getProgress();
mediaPlayer.seekTo(progress);
}
});
}
public void isPlayOrPause(View view){
ImageButton imageButton = (ImageButton) view;
if(mediaPlayer==null){
mediaPlayer = MediaPlayer.create(this, R.raw.love);
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取播放时长
int duration = mediaPlayer.getDuration();
seekBar.setMax(duration);
new MyThread().start();
}
else if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
imageButton.setImageResource(android.R.drawable.ic_media_play);
}
else{
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
}
}
class MyThread extends Thread{
@Override
public void run() {
super.run();
while (seekBar.getProgress()<=seekBar.getMax()){
//获取音乐的当前位置
int currentPosition = mediaPlayer.getCurrentPosition();
seekBar.setProgress(currentPosition);
}
}
}
}
API
1. 创建:MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.文件);
2. 判断音频是否正在播放:mediaPlayer.isPlaying()
3. 播放:mediaPlayer.start();
4. 暂停:mediaPlayer.pause();
5. 音乐时间:mediaPlayer.getDuration()
6. 音乐当前播放位置:mediaPlayer.getCurrentPosition();
7. 在某个位置播放:mediaPlayer.seekTo(int);
8. 播放完成事件 :mediaPlayer.setOnCompletionListenner()
9. 停止:mediaPlayer.stop()
10. 释放内存 :mediaPlayer.replease()
播放SD卡上的音频文件
首先在清单文件中加入读取内存卡的权限
<!--读取内存卡的权限-->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
其他不变,只需要改变一下获取音源的方式
MainActivity
if(mediaPlayer==null){
//播放应用内的音频文件
//mediaPlayer = MediaPlayer.create(this, R.raw.love);
//播放内存卡中的音频文件
mediaPlayer = new MediaPlayer();
//设置音频流的类型
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
//设置音源
try {
mediaPlayer.setDataSource(this, Uri.parse("file://mnt/sdcard/Music/love.mp3"));
//准备一下
mediaPlayer.prepare();
} catch (IOException e) {
e.printStackTrace();
}
mediaPlayer.start();
imageButton.setImageResource(android.R.drawable.ic_media_pause);
//获取播放时长
int duration = mediaPlayer.getDuration();
seekBar.setMax(duration);
new MyThread().start();
}
API
- 文件存放位置:内存卡
- 创建:MediaPalyer mediaPlayer = new MediaPlayer();
- 设置播放的类型:mediaPlayer.serAudioStreamType(AudioManager.STREAM_MUSIC);
- 设置音源:mediaPlayer.setDataSource(this, Uri.parse(“file://mnt/sdcard/音频路径”));
- 准备: mediaPlayer.prepare();
- 开始:mediaPlayer.start();