您好!欢迎来到源码码网

Android音频以及音频播放器开发实例

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-14 19:40
  • 阅读:464

Android 系统支持三种不同来源的音频播放:
1)本地资源

存储在应用程序中的资源,例如存储在 RAW 文件夹下的媒体文件,只能被当前应用程序访问。

2)外部资源

存储在文件系统中的标准媒体文件,例如存储在 SD 卡中的文件,可以被所有应用程序访问。

3)网络资源

通过网络地址取得的数据流(URL),例如“http://www.musiconline.com/classic/007. mp3”,可以被所有应用程序访问。

Android N 支持的音频格式

Android N 支持的音频格式如表 1 所示。

image.png


音频播放器

实例 MediaPlayerAudioDemo 演示了分别播放三种类型的资源的方法。

该实例中 MediaPlayerAudioActivity 向 Intent 对象中传入要载入的资源类型,并通过该 Intent 启动用于播放音乐的 Activity:PlayAudio。PlayAudio 根据传入的参数分别获取对应的音乐资源并且播放。

实例 MediaPlayerAudioDemo 的运行效果如图 1 所示。


MediaPlayerAudioDemo的运行效果

图 1  MediaPlayerAudioDemo的运行效果

实例 MediaPlayerAudioDemo 中的 main.xml 代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">



  6. <Button

  7. android:id="@+id/button01"

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content"

  10. android:text="播放存储在文件系统的音乐" />


  11. <Button

  12. android:id="@+id/button02"

  13. android:layout_width="fill_parent"

  14. android:layout_height="wrap_content"

  15. android:text="播放网络中的音乐" />


  16. <Button

  17. android:id="@+id/button03"

  18. android:layout_width="fill_parent"

  19. android:layout_height="wrap_content"

  20. android:text="播放本地资源的音乐" />

  21. </LinearLayout>


实例 MediaPlayerAudioDemo 中MainActivity.java 文件的代码如下:

  1. package introduction.android.batterydemo;


  2. import android.content.BroadcastReceiver;

  3. import android.content.Context;

  4. import android.content.Intent;

  5. import android.content.IntentFilter;

  6. import android.support.v7.app.AppCompatActivity;

  7. import android.os.Bundle;

  8. import android.view.View;

  9. import android.widget.Button;

  10. import android.widget.CompoundButton;

  11. import android.widget.TextView;

  12. import android.widget.ToggleButton;


  13. import org.w3c.dom.Text;


  14. public class MainActivity extends AppCompatActivity implements View.OnClickListener {

  15. private Button button01, button02, button03;

  16. private String PLAY = "play";

  17. private int Local = 1;

  18. private int Stream = 2;

  19. private int Resources = 3;


  20. @Override

  21. public void onCreate(Bundle saveInstanceState) {

  22. super.onCreate(saveInstanceState);

  23. setContentView(R.layout.activity_main);

  24. button01 = (Button) findViewById(R.id.button01);

  25. button02 = (Button) findViewById(R.id.button02);

  26. button03 = (Button) findViewById(R.id.button03);

  27. button01.setOnClickListener(this);

  28. button02.setOnClickListener(this);

  29. button03.setOnClickListener(this);

  30. }


  31. @Override

  32. public void onClick(View v) {

  33. Intent intent = new Intent(MainActivity.this, PlayAudio.class);

  34. if (v == button01) {

  35. intent.putExtra(PLAY, Local);

  36. }

  37. if (v == button02) {

  38. intent.putExtra(PLAY, Stream);

  39. }

  40. if (v == button03) {

  41. intent.putExtra(PLAY, Resources);

  42. }

  43. MainActivity.this.startActivity(intent);

  44. }

  45. }

实例 MediaPlayerAudioDemo 中 PlayAudio 类实现播放音频的功能,根据 MediaPlayer-AudioActivity 类通过 Intent 传递过来的不同的值,而实现三种不同的播放音频的方式。

PlayAudio.java 文件的代码如下:


  1. package introduction.android.batterydemo;


  2. import android.app.Activity;

  3. import android.media.MediaPlayer;

  4. import android.os.Bundle;

  5. import android.widget.TextView;

  6. import android.widget.Toast;


  7. public class PlayAudio extends Activity {

  8. private TextView textview;

  9. private String PLAY = "paly";

  10. private MediaPlayer mediaplayer;

  11. private String path;


  12. @Override

  13. public void onCreate(Bundle savedInstanceState) {

  14. super.onCreate(savedInstanceState);

  15. setContentView(R.layout.activity_main);

  16. textview = (TextView) findViewById(R.id.textview);

  17. Bundle extras = getIntent().getExtras();

  18. playAudio(extras.getInt(PLAY));

  19. }


  20. private void playAudio(int play) {

  21. // TODO Auto-generated method stub

  22. try {

  23. switch (play) {

  24. case 1:

  25. path = "sdcard/music/white.mp3";

  26. if (path == "") {

  27. Toast.makeText(PlayAudio.this, "在SD未找到音频文件",

  28. Toast.LENGTH_LONG);

  29. }

  30. mediaplayer = new MediaPlayer();

  31. mediaplayer.setDataSource(path);

  32. mediaplayer.prepare();

  33. mediaplayer.start();

  34. textview.setText("正在播放文件中的音乐");

  35. break;

  36. case 2:

  37. path = "http://www.musiconline.com/classic/007.mp3";

  38. if (path == "") {

  39. Toast.makeText(PlayAudio.this, "未找到您要播放的音乐",

  40. Toast.LENGTH_LONG).show();

  41. }

  42. mediaplayer = new MediaPlayer();

  43. mediaplayer.setDataSource(path);

  44. mediaplayer.prepare();

  45. mediaplayer.start();

  46. textview.setText("正在播放网络中的音乐");

  47. break;

  48. case 3:

  49. mediaplayer = MediaPlayer.create(this, null);

  50. mediaplayer.start();

  51. textview.setText("正在播放本地资源中的音乐");

  52. break;

  53. }

  54. } catch (Exception e) {

  55. System.out.println("出现异常");

  56. }

  57. }


  58. @Override

  59. protected void onDestroy() {

  60. // TODO Auto-generated method stub

  61. super.onDestroy();

  62. if (mediaplayer != null) {

  63. mediaplayer.release();

  64. mediaplayer = null;

  65. }

  66. }

  67. }

其中,path 指向要播放的音频文件的位置。

本实例中,外部文件系统中的资源是放置在 SD 卡中的 music 目录下的 white.mp3;网络资源使用的是 http://www.musiconline.com/classic/007.mp3;本地资源使用的是 raw 目录下的 black.mp3 文件。

实例 MediaPlayerAudioDemo 中 AndroidManifest.xml 文件的代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>

  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="introduction.android.batterydemo"

  4. android:versionCode="1"

  5. android:versionName="1.0">


  6. <uses-sdk android:minSdkVersion="10" />

  7. <application

  8. android:allowBackup="true"

  9. android:icon="@mipmap/ic_launcher"

  10. android:label="@string/app_name"

  11. android:roundIcon="@mipmap/ic_launcher_round"

  12. android:supportsRtl="true"

  13. android:theme="@style/AppTheme">

  14. <activity android:name=".MainActivity">

  15. <intent-filter>

  16. <action android:name="android.intent.action.MAIN" />

  17. <category android:name="android.intent.category.LAUNCHER" />

  18. </intent-filter>

  19. </activity>

  20. <activity android:name=".PlayAudio" />

  21. </application>


  22. </manifest>

在该实例中,每次播放音频文件时都会从 MediaPlayerAudioActivity 跳转到一个新的 Activity,即 PlayAudio。

当返回 MediaPlayerAudioActivity 时,由于 PlayAudio 对象被释放掉,因此播放的音乐也随之停止,不再播放。若想在返回 MediaPlayerAudioActivity 时音乐不停止,则需要使用 Service 在后台播放音频文件。
 

后台播放音频

实例 AudioServiceDemo 演示了如何在后台播放音频。该实例的运行效果如图 2 所示。当用户单击“启动 Service”按钮时,当前 Activity 结束,应用程序界面消失,返回 Android 应用程序列表,同时后台启动 Service,播放视频文件。

AudioServiceDemo的运行效果

图 2  AudioServiceDemo的运行效果

该实例界面简单,仅一个按钮。布局文件 main.xml 的代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>

  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="fill_parent"

  4. android:layout_height="fill_parent"

  5. android:orientation="vertical">


  6. <Button

  7. android:id="@+id/button1"

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content"

  10. android:text="启动Service" />

  11. </LinearLayout>

实例 AudioServiceDemo 中 Activity 文件 AudioServiceDemoActivity.java 的代码如下:


  1. package introduction.android.audioservicedemo;


  2. import android.content.Intent;

  3. import android.support.v7.app.AppCompatActivity;

  4. import android.os.Bundle;

  5. import android.view.View;

  6. import android.widget.Button;


  7. public class MainActivity extends AppCompatActivity {

  8. private Button btn;


  9. @Override

  10. protected void onCreate(Bundle savedInstanceState) {

  11. super.onCreate(savedInstanceState);

  12. setContentView(R.layout.activity_main);

  13. btn = (Button) findViewById(R.id.button1);

  14. btn.setOnClickListener(new View.OnClickListener() {

  15. @Override

  16. public void onClick(View view) {

  17. startService(new Intent("introduction.android.AudioServiceDemo.MY_AUDIO_SERVICE"));

  18. finish();

  19. }

  20. });

  21. }

  22. }

AudioServiceDemoActivity 在按钮被单击后使用 startService() 方法启动了自定义的服务 MY_AUDIO_SERVICE,然后调用 finish() 方法关闭当前 Activity。该服务需要在 AndroidManifest. xml 文件中进行声明。

AndroidManifest.xml 的代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>


  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"

  3. package="introduction.android.AudioServiceDemo"

  4. android:versionCode="1"

  5. android:versionName="1.0">


  6. <uses-sdk android:minSdkVersion="14" />

  7. <application

  8. android:icon="@drawable/ic_launcher"

  9. android:label="@string/app_name">

  10. <activity

  11. android:name=".AudioServiceDemoActivity"

  12. android:label="@string/app_name">

  13. <intent-filter>

  14. <action android:name="android.intent.action.MAIN" />

  15. <category android:name="android.intent.category.LAUNCHER" />

  16. </intent-filter>

  17. </activity>

  18. <service android:name="MyAudioService">

  19. <intent-filter>

  20. <action android:name="introduction.android.AudioServiceDemo.MY_AUDIO_SERVICE" />

  21. <category android:name="android.intent.category.DEFAULT" />

  22. </intent-filter>

  23. </service>

  24. </application>

  25. </manifest>


其中:

  1. <service android:name="MyAudioService">

  2. <intent-filter>

  3. <action android:name="introduction.android.AudioServiceDemo.MY_AUDIO_SERVICE" />

  4. <category android:name="android.intent.category.DEFAULT" />

  5. </intent-filter>

  6. </service>

定义了名为 MyAudioService 的 Service,该 Service 对名为“introduction.android.AudioServiceDemo. MY_AUDIO_SERVICE”的动作进行处理。

实例 AudioServiceDemo 中 MyAudioService.java 的代码如下:


  1. package introduction.android.audioservicedemo;


  2. import android.app.Service;

  3. import android.content.Intent;

  4. import android.media.MediaPlayer;

  5. import android.os.IBinder;


  6. import java.io.IOException;


  7. public class MyAudioService extends Service {

  8. private MediaPlayer mediaplayer;


  9. @Override

  10. public IBinder onBind(Intent argO) {

  11. // TODO Auto-generated method stub

  12. return null;

  13. }


  14. @Override

  15. public void onDestroy() {

  16. // TODO Auto-generated method stub

  17. super.onDestroy();

  18. if (mediaplayer != null) {

  19. mediaplayer.release();

  20. mediaplayer = null;

  21. }

  22. }


  23. @Override

  24. public void onStartCommand(Intent intent, int flags, int startId) {

  25. // TODO Auto-generated method stub

  26. super.onStartCommand(intent, flags, startId);

  27. String path = "sdcard/music/white.mp3";

  28. mediaplayer = new MediaPlayer();

  29. try {

  30. mediaplayer.setDataSource(path);

  31. mediaplayer.prepare();

  32. mediaplayer.start();

  33. } catch (IOException e) {

  34. // TODO Auto-generated catch block

  35. e.printStackTrace();

  36. }

  37. }

  38. }

该服务启动 Mediaplayer,并播放存放于 SD 卡中的“sdcard/music/white.mp3”文件。

特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • css中rel的属性值都有哪些,分别代表什么意思
  • css中rel的属性值都有哪些,分别代表什么意思
  • 在HTML中,元素的rel属性用于定义当前文档与被链接文档之间的关系。这个属性在CSS的上下文中经常与样式表关联,但rel属性的用途远不止于此。以下是一些常见的rel属性值及其意义:1、stylesheet:表示被链接的文档是一个样式表。这通常用于链接CSS文件。
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-03-28 12:28
  • 阅读:202
  • css中的z-index是什么意思,如何使用?
  • css中的z-index是什么意思,如何使用?
  • z-index是CSS属性,用于控制元素在页面中的层叠顺序。z-index的值决定了元素在垂直层面上的显示顺序,具有较高z-index值的元素会覆盖具有较低z-index值的元素。默认情况下,元素的z-index值是auto,这意味着元素的层叠顺序由其在文档流中的位置决定。在没有使用定位属性的情况下,后面出现的元素会覆盖前面出现的元素。
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-03-28 11:34
  • 阅读:20
  • PHP开发五种数据打印方式举例说明
  • PHP开发五种数据打印方式举例说明
  • 在PHP中,有几种常用的打印方式,包括:1、echo:用于输出一个或多个字符串。它是PHP语句,不是函数,因此没有返回值。例如:echo ”Hello, World!”; // 输出 ”Hello, World!”2、print:用于输出一个字符串。它需要一个参数,并需要使用圆括号。print函数在输出后有返回值,如果执行失败则返回f
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-01-18 23:09
  • 阅读:318
  • php开发判断字符串是否相等的方法
  • php开发判断字符串是否相等的方法
  • 本文介绍php开发中常用的字符串比较的方法,以PHP7.4为例,可以使用双等号,strcmp()方法,strcasemp()方法、strncasecmp()方法以及ctrncmp()方法,等进行判断,下边以”==”和strcmp()方法为例进行举例:// 定义插入的数据$data = [    ”name” &
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-01-18 21:04
  • 阅读:238
  • thinkphp6 No input file specified解决办法
  • thinkphp6 No input file specified解决办法
  • thinkphp6出现Noinputfilespecified错误基本上都是因为访问路径出错引起的,解决办法也很简单,打开public目录下的的.htaccess文件,对伪静态规则进行编辑,将:把:RewriteRule ^(.*)$ index.php/$1 [QSA,PT,L]改为:RewriteRule ^(.*)$ index.php [L,E=PATH_I
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-01-17 20:24
  • 阅读:152
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部