源码教程 2025年06月7日
0 收藏 0 点赞 349 浏览 8505 个字
摘要 :

Android 系统支持三种不同来源的音频播放:1)本地资源存储在应用程序中的资源,例如存储在 RAW 文件夹下的媒体文件,只能被当前应用程序访问。2)外部资源存储在文……

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

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

2)外部资源

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

3)网络资源

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

Android N 支持的音频格式

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

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

音频播放器

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

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

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


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

图 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,播放视频文件。

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

图 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”文件。

微信扫一扫

支付宝扫一扫

版权: 转载请注明出处:https://www.zuozi.net/1024.html

管理员

相关推荐
2025-07-05

对于一些使用WordPress进行外贸建站的商家来说,大部分人会通过在WordPress中添加JS代码和CSS样式表…

701
2025-07-05

当商家遇到WordPress独立站改版或者域名到期等问题时,不免会涉及到WordPress域名的更改,那么这一…

715
2025-07-05

用户在使用WooCommerce进行跨境电商独立站搭建工作时,有可能会借助WooCommerce短代码实现更加醒目…

307
2025-07-05

随着外贸建站行业的不断深入发展,WordPress的多语言功能也显得越来越重要,一个具有多语言的独立站…

1,039
2025-07-05

WooCommerce作为WordPress外贸建站生态的一部分,WooCommerce运费设置是商家在建站过程中不可忽视的…

835
2025-07-05

在外贸建站的过程中,有些商家会选择使用WordPress幻灯片为网站页面增加一定的动感和可观性,进而提…

723
发表评论
暂无评论

还没有评论呢,快来抢沙发~

助力内容变现

将您的收入提升到一个新的水平

点击联系客服

在线时间:08:00-23:00

客服QQ

122325244

客服电话

400-888-8888

客服邮箱

122325244@qq.com

扫描二维码

关注微信客服号