android音频播放方式有两种:一种是MediaPlayer,另一种是SoundPool,下面这个例子是用的第一种
1.首先看布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="音乐文件名称"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="neyanbhbin.mp3"
android:id="@+id/fileName"
/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="播放"
android:id="@+id/startButton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="暂停"
android:layout_toRightOf="@id/startButton"
android:layout_alignTop="@id/startButton"
android:id="@+id/pauseButton"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="停止"
android:layout_toRightOf="@+id/pauseButton"
android:layout_alignTop="@+id/pauseButton"
android:id="@+id/endButton"
/>
</RelativeLayout>
</LinearLayout>
2.响应的Activity代码如下
package com.lamp.audio;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Audio extends Activity {
private EditText fileNameText = null;
private MediaPlayer mediaPlayer = null;
private boolean ispause = false;
private static final String TAG = "Audio";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fileNameText = (EditText)this.findViewById(R.id.fileName);
Button startButton = (Button)this.findViewById(R.id.startButton);
Button pauseButton = (Button)this.findViewById(R.id.pauseButton);
Button endButton = (Button)this.findViewById(R.id.endButton);
startButton.setOnClickListener(listener);
pauseButton.setOnClickListener(listener);
endButton.setOnClickListener(listener);
mediaPlayer = new MediaPlayer();
mediaPlayer.setOnCompletionListener(new OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Toast.makeText(Audio.this, "播放结束", Toast.LENGTH_LONG).show();
}
});
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button button = (Button)v;
switch (button.getId()) {
case R.id.startButton:
String fileName = fileNameText.getText().toString();
mediaPlayer.reset();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
try {
mediaPlayer.setDataSource("/mnt/sdcard/" + fileName);
//缓冲
mediaPlayer.prepare();
//开始播放
mediaPlayer.start();
} catch (Exception e) {
Log.e(TAG, e.toString());
}
break;
case R.id.pauseButton:
if(mediaPlayer.isPlaying()){
//暂停播放
mediaPlayer.pause();
}else{
mediaPlayer.start();
}
break;
case R.id.endButton:
if(mediaPlayer.isPlaying()){
//停止播放
mediaPlayer.stop();
}
break;
}
}
};
//当手机收到外来响应,比如接到电话,音频播放暂停
protected void onPause() {
if(mediaPlayer.isPlaying()){
mediaPlayer.pause();
ispause = true;
}
super.onPause();
}
//当挂断电话后恢复播放
protected void onResume() {
if(ispause){
mediaPlayer.start();
ispause = false;
}
super.onResume();
}
@Override
protected void onDestroy() {
mediaPlayer.release();
mediaPlayer = null;
super.onDestroy();
}
}
播放的map3文件可以放在资源文件夹,sdk卡,此例子是放在sdk根目录下
分享到:
相关推荐
Android 音乐播放源码.zip
基于android平台的音频播放自定义控件,使用简单易操作
简单的音乐播放,音乐的控制与音乐列表的加载
AndroidStudio音乐播放前台服务,可播放音乐
Android音乐播放软件是一种可以在Android设备上运行的应用程序,旨在给用户提供方便、直观地管理和播放音乐文件的工具。这类软件通常具有以下功能和特点: 1. 音乐播放功能:允许用户浏览设备中的音乐文件并播放...
android 音乐播放程序,源码,可以运行的,里面有注解。
基于android音乐播放程序的开发.docx基于android音乐播放程序的开发.docx基于android音乐播放程序的开发.docx基于android音乐播放程序的开发.docx基于android音乐播放程序的开发.docx基于android音乐播放程序的开发....
Android音乐播放代码,一个android音乐播放器源码,了解android音乐播放器的编程要点,本源码包括编译生成的APK文件,有Android手机的用户可把这个文件拷贝至手机运行一下看效果。
android 音乐播放源码.rar
Android程序研发源码Android 音乐播放源码.zip
Android音乐播放源码.zip
Android音乐播放的实现
android播放在线音乐demo,获取在线音乐并播放控制等,可直接运行实现
android 音频播放器 源码 源代码 导入可用
音频播放,适用于短小且可能会重复播放的音频文件
2021-影音播放-android 音乐播放源码.zip
主要描述了来电时,中断了后台播放音乐,并在挂电话后恢复了后台音乐
该资源包含了一个用android实现的简单的音乐播放的小实例,简单易懂,适合初学者,喜欢的下载!
Android 音乐播放歌词测试demo,已经测试成功!