android中常见声音操作方式(Ringtone,SoundPool,MediaPlayer)小结

Android开发中有时候需要用到播放声音操作,在android API 的media包中有三种方式可供我们选择,它们分别是Ringtone,SoundPool,MediaPlayer。因为在我目前的项目中暂时用不到播放很长的音频文件,只需要播放一些简短的提示音,所以在这篇博文中我只是简单的记录下。

  • 1.Ringtone.java
  • 2.SoundPool.java
  • 3.MediaPlayer.java
  • 4.demo
  • 5.bug record

注意:

关于这三个类的介绍我全部用的是android 自身的Javadoc描述;

全部摘自于android-23;

因为我的项目中主要用到Ringtone,所以展示了它全部的描述;

1.Ringtone.java

关于Ringtone的介绍。

package android.media;
/**
 * Ringtone provides a quick method for playing a ringtone, notification, or
 * other similar types of sounds.
 * <p>
 * For ways of retrieving {@link Ringtone} objects or to show a ringtone
 * picker, see {@link RingtoneManager}.
 *
 * @see RingtoneManager
 */
 public class Ringtone {}

2.SoundPool.java

关于SoundPool的介绍。

/**
 * The SoundPool class manages and plays audio resources for applications.
 *
 * <p>A SoundPool is a collection of samples that can be loaded into memory
 * from a resource inside the APK or from a file in the file system. The
 * SoundPool library uses the MediaPlayer service to decode the audio
 * into a raw 16-bit PCM mono or stereo stream. This allows applications
 * to ship with compressed streams without having to suffer the CPU load
 * and latency of decompressing during playback.</p>
 *
 * <p>In addition to low-latency playback, SoundPool can also manage the number
 * of audio streams being rendered at once. When the SoundPool object is
 * constructed, the maxStreams parameter sets the maximum number of streams
 * that can be played at a time from this single SoundPool. SoundPool tracks
 * the number of active streams. If the maximum number of streams is exceeded,
 * SoundPool will automatically stop a previously playing stream based first
 * on priority and then by age within that priority. Limiting the maximum
 * number of streams helps to cap CPU loading and reducing the likelihood that
 * audio mixing will impact visuals or UI performance.</p>
 *
 * <p>Sounds can be looped by setting a non-zero loop value. A value of -1
 * causes the sound to loop forever. In this case, the application must
 * explicitly call the stop() function to stop the sound. Any other non-zero
 * value will cause the sound to repeat the specified number of times, e.g.
 * a value of 3 causes the sound to play a total of 4 times.</p>
 *
 * <p>The playback rate can also be changed. A playback rate of 1.0 causes
 * the sound to play at its original frequency (resampled, if necessary,
 * to the hardware output frequency). A playback rate of 2.0 causes the
 * sound to play at twice its original frequency, and a playback rate of
 * 0.5 causes it to play at half its original frequency. The playback
 * rate range is 0.5 to 2.0.</p>
 *
 * <p>Priority runs low to high, i.e. higher numbers are higher priority.
 * Priority is used when a call to play() would cause the number of active
 * streams to exceed the value established by the maxStreams parameter when
 * the SoundPool was created. In this case, the stream allocator will stop
 * the lowest priority stream. If there are multiple streams with the same
 * low priority, it will choose the oldest stream to stop. In the case
 * where the priority of the new stream is lower than all the active
 * streams, the new sound will not play and the play() function will return
 * a streamID of zero.</p>
 *
 * <p>Let‘s examine a typical use case: A game consists of several levels of
 * play. For each level, there is a set of unique sounds that are used only
 * by that level. In this case, the game logic should create a new SoundPool
 * object when the first level is loaded. The level data itself might contain
 * the list of sounds to be used by this level. The loading logic iterates
 * through the list of sounds calling the appropriate SoundPool.load()
 * function. This should typically be done early in the process to allow time
 * for decompressing the audio to raw PCM format before they are needed for
 * playback.</p>
 *
 * <p>Once the sounds are loaded and play has started, the application can
 * trigger sounds by calling SoundPool.play(). Playing streams can be
 * paused or resumed, and the application can also alter the pitch by
 * adjusting the playback rate in real-time for doppler or synthesis
 * effects.</p>
 *
 * <p>Note that since streams can be stopped due to resource constraints, the
 * streamID is a reference to a particular instance of a stream. If the stream
 * is stopped to allow a higher priority stream to play, the stream is no
 * longer be valid. However, the application is allowed to call methods on
 * the streamID without error. This may help simplify program logic since
 * the application need not concern itself with the stream lifecycle.</p>
 *
 * <p>In our example, when the player has completed the level, the game
 * logic should call SoundPool.release() to release all the native resources
 * in use and then set the SoundPool reference to null. If the player starts
 * another level, a new SoundPool is created, sounds are loaded, and play
 * resumes.</p>
 */
public class SoundPool {}

3.MediaPlayer.java

package android.media;
/**
 * MediaPlayer class can be used to control playback
 * of audio/video files and streams. An example on how to use the methods in
 * this class can be found in {@link android.widget.VideoView}.
 * For ways of retrieving {@link Ringtone} objects or to show a ringtone
 * picker, see {@link RingtoneManager}.
 *
 * @see RingtoneManager
 */
public class MediaPlayer implements SubtitleController.Listener{}

4.demo

因为这个demo很简单,就一个Activity和xml,所以我就不提供源码下载了。

请直接看码:

package android.media;
package org.tuzhao.demo.activity;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.Ringtone;
import android.media.RingtoneManager;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import org.tuzhao.sqlite.R;

import java.io.IOException;

/**
 * three types to play sound
 * @author tuzhao
 */
public class SoundActivity extends AppCompatActivity implements View.OnClickListener {

    private Ringtone ringtone;

    private SoundPool soundPool;
    private int loadId;

    private MediaPlayer mediaPlayer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sound);
        //初始化这三个方法
        ringtone = initRingtone(this);

        soundPool = initSoundPool();
        loadId = soundPool.load(this, R.raw.beep, 1);

        mediaPlayer = initMediaPlayer(this, 0);

        findViewById(R.id.bt_ringtone).setOnClickListener(this);
        findViewById(R.id.bt_sound_pool).setOnClickListener(this);
        findViewById(R.id.bt_media).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_ringtone:
                ringtone.play();
                break;
            case R.id.bt_sound_pool:
                soundPool.play(loadId, 1.0f, 1.0f, 0, 0, 1.0f);
                break;
            case R.id.bt_media:
                mediaPlayer.start();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //release resource of this two types.
        soundPool.release();
        mediaPlayer.release();
    }

    /**
     * init type of Ringtone
     * @param context Activity
     * @return Ringtone
     */
    private Ringtone initRingtone(Activity context) {
        Uri notification = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        return RingtoneManager.getRingtone(context, notification);
    }

    /**
     * init type of  SoundPool
     * @return SoundPool
     */
    @SuppressWarnings("deprecation")
    private SoundPool initSoundPool() {

//      The content of the comments need API-23
//      SoundPool.Builder builder=new SoundPool.Builder();
//      builder.setMaxStreams(10);
//
//      AudioAttributes.Builder audioBuilder=new AudioAttributes.Builder();
//      audioBuilder.setContentType(AudioAttributes.CONTENT_TYPE_UNKNOWN);
//      audioBuilder.setFlags(AudioAttributes.FLAG_AUDIBILITY_ENFORCED);
//      audioBuilder.setLegacyStreamType(AudioManager.STREAM_SYSTEM);
//      audioBuilder.setUsage(AudioAttributes.USAGE_NOTIFICATION);
//      AudioAttributes attributes = audioBuilder.build();
//
//      builder.setAudioAttributes(attributes);
//      SoundPool soundPool = builder.build();
        return new SoundPool(10, AudioManager.STREAM_NOTIFICATION, 5);
    }

    /**
     * init type of  MediaPlayer
     * @param context Activity
     * @param rawId   the id of raw
     * @return MediaPlayer
     */
    public MediaPlayer initMediaPlayer(Activity context, int rawId) {
        context.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        final MediaPlayer mediaPlayer = new MediaPlayer();
        mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        /**
         * When the beep has finished playing, rewind to queue up another one.
         */
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mediaPlayer.seekTo(0);
            }
        });
        mediaPlayer.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                mediaPlayer.release();
                return true;
            }
        });
        AssetFileDescriptor file;
        if (rawId == 0) {
            file = context.getResources().openRawResourceFd(R.raw.beep);
        } else {
            file = context.getResources().openRawResourceFd(rawId);
        }
        try {
            mediaPlayer.setDataSource(file.getFileDescriptor(),
                file.getStartOffset(), file.getLength());
            file.close();
            float BEEP_VOLUME = 1.0f;
            mediaPlayer.setVolume(BEEP_VOLUME, BEEP_VOLUME);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            Log.i("initSpecifiedSound", "what happened to init sound? you need to deal it .");
            return null;
        }
        return mediaPlayer;
    }

}

下面是xml布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    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"
    tools:context="org.tuzhao.sqlite.activity.SoundActivity">

    <Button
        android:id="@+id/bt_ringtone"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="88dp"
        android:text="Ringtone"
        android:textAllCaps="false"
        />

    <Button
        android:id="@+id/bt_sound_pool"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/bt_ringtone"
        android:layout_alignStart="@+id/bt_ringtone"
        android:layout_below="@+id/bt_ringtone"
        android:layout_marginTop="15dp"
        android:text="SoundPool"
        android:textAllCaps="false"/>

    <Button
        android:id="@+id/bt_media"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/bt_sound_pool"
        android:layout_alignStart="@+id/bt_sound_pool"
        android:layout_below="@+id/bt_sound_pool"
        android:layout_marginTop="15dp"
        android:text="MediaPlayer"
        android:textAllCaps="false"/>
    </RelativeLayout>

5.bug record

bugs:

AudioTrack: AudioFlinger could not create track, status: -12

AudioFlinger: no more track names available

AudioSink: Unable to create audio track

MediaPlayer: Should have subtitle controller already set

resolve:我出现这种情况是因为我最刚开始采用的不是上面Java文件初始化的方法,而是每播放一次声音就new 一个对象来播放声音。这样能播放,但是播放了十几次就报上面的错误。正确做法是只初始化一次。

每次调用下面这三个分别对应方法:

soundPool.play(sourceId, 1.0f, 1.0f, 0, 0, 1.0f);

ringtone.play();

mediaPlayer.start();



本人才疏学浅,出错在所难免。如文中有错误的地方望指正,谢谢!

欢迎关注我的Github账号:https://github.com/tuzhao

时间: 2024-10-13 02:33:40

android中常见声音操作方式(Ringtone,SoundPool,MediaPlayer)小结的相关文章

Android中常见的内存泄漏

为什么会产生内存泄漏? 当一个对象已经不需要再使用了,本该被回收时,而有另外一个正在使用的对象持有它的引用从而导致它不能被回收,这导致本该被回收的对象不能被回收而停留在堆内存中,这就产生了内存泄漏. 内存泄漏对程序的影响? 内存泄漏是造成应用程序OOM的主要原因之一!我们知道Android系统为每个应用程序分配的内存有限,而当一个应用中产生的内存泄漏比较多时,这就难免会导致应用所需要的内存超过这个系统分配的内存限额,这就造成了内存溢出而导致应用Crash. Android中常见的内存泄漏汇总 1

Android 中常见控件的介绍和使用

1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.lang.Object   ? android.view.View   ? android.widget.TextView 直接子类: Button, CheckedTextView, Chronometer, DigitalClock, EditText 间接子类: AutoCompleteTextV

Android中常见Intent习惯用法-上篇(附源码下载)

Android中的Intent是一个非常重要的类,如果对Intent不是特别了解,可以参见博文<Android中Intent概述及使用>.如果对Intent Filter不是特别了解,可以参见博文<Android中Intent对象与Intent Filter过滤匹配过程详解>. 本文着重讲一下Android中一些常见的Intent的习惯用法,比如如何通过Intent发送短信.发送邮件.启动摄像机拍照录视频.设置闹铃.打开WIFI设置界面等等. 限于篇幅,本博文分为上下两篇,这是上篇

Android中常见的设计模式

自己理解的设计模式遵循的原则: 1)功能单一明确,设计一个类的意图要明确,不能大包大揽什么功能都继承进去 2)对于扩展要开放,修改要关闭.软件通常都有需求变化,变化过程中通过扩展的方式来实现需求变化,而不是通过修改原有的方法,因为修改原有的方法会导致原来方法的调用方会出问题,这样层层调用出问题. 3)变化的进行抽象,不变的进行具体.设计代码过程中会面对很对可变的东西,比如在实现一个功能的时候,能够运用不同的方式进行实现,这个时候可以将每个具体的实现方法进行抽象,真正不变的是这个方法要实现的目的

Android中常见IPC方法总结

欢迎转载,转载请注明出处http://blog.csdn.net/l664675249/article/details/50654926 IPC (Interprocess communication)跨进程通信,是指在两个进程之间交换数据的过程.多进程通信一般分为两种情况.第一种,一个应用因为自身的需要采用多进程实现,比如某些模块由于特殊原因需要运行在单独的进程中.第二种情况,当前应用需要获得其它应用的数据,由于是两个应用,所以必须采用跨进程的方式.下面就对常用的IPC方法做一个总结. 使用B

Android中常见功能包描述

在Android中,各种包写成android.*的方式,重要包的描述如下所示:android.app :提供高层的程序模型.提供基本的运行环境android.content:包含各种的对设备上的数据进行访问和发布的类android.database :通过内容提供者浏览和操作数据库android.graphics:底层的图形库,包含画布,颜色过滤,点,矩形,可以将他们直接绘制到屏幕上.android.location :定位和相关服务的类android.media :提供一些类管理多种音频.视频

Android中常见功能包描述(转)

在Android中,各种包写成android.*的方式,重要包的描述如下所示:android.app :提供高层的程序模型.提供基本的运行环境android.content:包含各种的对设备上的数据进行访问和发布的类android.database :通过内容提供者浏览和操作数据库android.graphics:底层的图形库,包含画布,颜色过滤,点,矩形,可以将他们直接绘制到屏幕上.android.location :定位和相关服务的类android.media :提供一些类管理多种音频.视频

Android中常见的几种布局的总结

众所周知,一个应用程序的良好与否,很大程度上取决于它的用户界面.这就像是一个人给人的第一感觉也是从脸开始的一样.一个应用程序首先展示给客户的就是它的界面,通途的说,也就是软件的脸面.只有良好的用户交互界面,才能在第一时间抓住客户心理,取得优势.那么今天我就来给大家说一下在Android程序的开发过程中,对于Android应用程序的一些常见的布局以及个人的一些看法,希望对大家有所帮助. 在Android4.0之前一共有5种关于Android的布局,分别是:LinearLayout(线性布局),Re

Android中常见的图片加载框架

图片加载涉及到图片的缓存.图片的处理.图片的显示等.而随着市面上手机设备的硬件水平飞速发展,对图片的显示要求越来越高,稍微处理不好就会造成内存溢出等问题.很多软件厂家的通用做法就是借用第三方的框架进行图片加载. 开源框架的源码还是挺复杂的,但使用较为简单.大部分框架其实都差不多,配置稍微麻烦点,但是使用时一般只需要一行,显示方法一般会提供多个重载方法,支持不同需要.这样会减少很不必要的麻烦.同时,第三方框架的使用较为方便,这大大的减少了工作量.提高了开发效率.本文主要介绍四种常用的图片加载框架,