Android 音频开发(三) 如何播放一帧音频数据上

上一篇只要介绍了如何采集一帧音频,本篇就讲述如何播放一帧音频数据,这一篇我将分倆篇来详细介绍。

Android SDK 提供了3套音频播放的API,分别是:MediaPlayer,SoundPool,AudioTrack,在讲解如何播放一帧音频数据前,我先介绍一下这三个API的区别在哪里。

SoundPool,MediaPlayer,AudioTrack的区别

  1. SoundPool ---- 适合短促且对反应速度比较高的情况(游戏音效或按键声等)
  2. MediaPlayer ---- 适合比较长且时间要求不高的情况
  3. AudioTrack ---- 播放解码后的PCM码流

SoundPool详解

SoundPool的介绍将从如下三个方面详细介绍。

1.SoundPool简介

   SoundPool类是Android用于管理和播放应用程序的音频资源的类。一个SoundPool对象可以看作是一个可以从APK中导入资源或者从文件系统中载入文件的样本集合。它利用MediaPlayer服务为音频解码为一个原始16位PCM流。这个特性使得应用程序可以进行流压缩,而无须忍受在播放音频时解压所带来的CPU负载和时延。
   
    此外对于低延迟播放,SoundPool还可以管理多个音频流。当SoundPool对象构造时,maxStreams参数的设置表示的是在单一的SoundPool中,同一时间所能播放流的最大数量。利用SoundPool可以跟踪活跃的流的数量。如果其数量超过流的最大数目,SoundPool会基于优先级自动停止先前播放的流。限制流的最大数目,有助于减轻CPU的负荷,减少音频混合对视觉和UI性能的影响。

    声音可以通过设置一个非零的循环价值循环。如果值为-1将导致声音永远循环。在这种情况下,应用程序必须明确地调用stop()函数,以停止声音。其他非零值将导致声音按指定数量的时间重复。

    在SoundPool中,播放速率也可以改变。1.0的播放率可以使声音按照其原始频率(如果必要的话,将重新采样硬件输出频率)。而2.0的播放速率,可以使声音按照其原始频率的两倍播放。如果为0.5的播放率,则播放速率是原始频率的一半。播放速率的取值范围是0.5至2.0。

    优先级的运行从低到高排列的。当用户调用play()函数时,如果活跃的流数目大于规定的maxStreams参数,流分配器将会结束优先级最低的流。如果有多条流都处于最低优先级,优先级系统将会选择关闭最老的流。

    一旦声音被成功加载和播放,应用程序可以调用SoundPool.play()来触发的声音。播放过程中的流可又被暂停或继续播放。应用程序还可以通过调整播放率改变音高。

    注意,由于资源的限制,流可以被停止,streamID是一个对特定流实例的引用。如果流被停止并且允许更高优先级的流播放,流就不再有效了。然而,应用程序允许调用没有错误的streamID方法。因为如果应用程序不需要关心流的生命周期,这可能有助于简化程序逻辑。

2.应用场景

       SoundPool在载入声音文件过程中,使用了单独的线程,不会对视觉和UI性能产生影响。但是由于SoundPool对载入声音文件大小有所限制,这就导致了如果SoundPool没有载入完成,而不能安全调用play方法。好在Android SDK提供了一个SoundPool.OnLoadCompleteListener类来帮助我们了解声音文件是否载入完成,用户只须重载 onLoadComplete(SoundPool soundPool, int sampleId, int status) 方法即可实现。

与MediaPlayer相比,MediaPlayer存在着资源占用量较高、延迟时间较长、不支持多个音频同时播放等缺点,但SoundPool本身由于内存资源申请有严格限制,所以在开发过程中,笔者建议尽量用SoundPool来播放一些较短的声音片段或者音效。  
​​​​​​​

3.SoundPool的使用

使用SoundPool我们只需要按照如下几步就可以了。
1. SoundPool的初始化

SoundPool的初始化很简单,他和常用java对象一样,都是通过构造函数实例化而来,查看源码,SoundPool的构造函数如下:

  /**
     * Constructor. Constructs a SoundPool object with the following
     * characteristics:
     *
     * @param maxStreams the maximum number of simultaneous streams for this
     *                   SoundPool object
     * @param streamType the audio stream type as described in AudioManager
     *                   For example, game applications will normally use
     *                   {@link AudioManager#STREAM_MUSIC}.
     * @param srcQuality the sample-rate converter quality. Currently has no
     *                   effect. Use 0 for the default.
     * @return a SoundPool object, or null if creation failed
     * @deprecated use {@link SoundPool.Builder} instead to create and configure a
     *     SoundPool instance
     */
    public SoundPool(int maxStreams, int streamType, int srcQuality) {
    
    
        this(maxStreams,
                new AudioAttributes.Builder().setInternalLegacyStreamType(streamType).build());
        PlayerBase.deprecateStreamTypeForPlayback(streamType, "SoundPool", "SoundPool()");
    }

    private SoundPool(int maxStreams, AudioAttributes attributes) {
    
    
        super(attributes, AudioPlaybackConfiguration.PLAYER_TYPE_JAM_SOUNDPOOL);

        // do native setup
        if (native_setup(new WeakReference<SoundPool>(this), maxStreams, attributes) != 0) {
    
    
            throw new RuntimeException("Native setup failed");
        }
        mLock = new Object();
        mAttributes = attributes;

        baseRegisterPlayer();
    }

下面就详细介绍一下这构造函数的参数的意义:

  • maxStreams:
    指定支持多少个声音,SoundPool对象中允许同时存在的最大流的数量。
  • streamType:
    制定声音类型,流类型可以分为STREAM_VOICE_CALL,STREAM_SYSTEM, STREAM_RING,STREAM_MUSIC和STREAM_ALARM四种类型。他们都在AudioManager中定义。
  • srcQuality:
    指定声音品质(采样率变换质量)。目前没有用到,可以设为0。

2. 载入声音资源
初始化SoundPool对象后,我们就需要加载音频资源了,SoundPool的加载提供了四个重载的方法,源码如下:

     * Load the sound from the specified path.
     *
     * @param path the path to the audio file
     * @param priority the priority of the sound. Currently has no effect. Use
     *                 a value of 1 for future compatibility.
     * @return a sound ID. This value can be used to play or unload the sound.
     */
    public int load(String path, int priority) {
    
    
        int id = 0;
        try {
    
    
            File f = new File(path);
            ParcelFileDescriptor fd = ParcelFileDescriptor.open(f,
                    ParcelFileDescriptor.MODE_READ_ONLY);
            if (fd != null) {
    
    
                id = _load(fd.getFileDescriptor(), 0, f.length(), priority);
                fd.close();
            }
        } catch (java.io.IOException e) {
    
    
            Log.e(TAG, "error loading " + path);
        }
        return id;
    }

    /**
     * Load the sound from the specified APK resource.
     *
     * Note that the extension is dropped. For example, if you want to load
     * a sound from the raw resource file "explosion.mp3", you would specify
     * "R.raw.explosion" as the resource ID. Note that this means you cannot
     * have both an "explosion.wav" and an "explosion.mp3" in the res/raw
     * directory.
     *
     * @param context the application context
     * @param resId the resource ID
     * @param priority the priority of the sound. Currently has no effect. Use
     *                 a value of 1 for future compatibility.
     * @return a sound ID. This value can be used to play or unload the sound.
     */
    public int load(Context context, int resId, int priority) {
    
    
        AssetFileDescriptor afd = context.getResources().openRawResourceFd(resId);
        int id = 0;
        if (afd != null) {
    
    
            id = _load(afd.getFileDescriptor(), afd.getStartOffset(), afd.getLength(), priority);
            try {
    
    
                afd.close();
            } catch (java.io.IOException ex) {
    
    
                //Log.d(TAG, "close failed:", ex);
            }
        }
        return id;
    }

    /**
     * Load the sound from an asset file descriptor.
     *
     * @param afd an asset file descriptor
     * @param priority the priority of the sound. Currently has no effect. Use
     *                 a value of 1 for future compatibility.
     * @return a sound ID. This value can be used to play or unload the sound.
     */
    public int load(AssetFileDescriptor afd, int priority) {
    
    
        if (afd != null) {
    
    
            long len = afd.getLength();
            if (len < 0) {
    
    
                throw new AndroidRuntimeException("no length for fd");
            }
            return _load(afd.getFileDescriptor(), afd.getStartOffset(), len, priority);
        } else {
    
    
            return 0;
        }
    }

    /**
     * Load the sound from a FileDescriptor.
     *
     * This version is useful if you store multiple sounds in a single
     * binary. The offset specifies the offset from the start of the file
     * and the length specifies the length of the sound within the file.
     *
     * @param fd a FileDescriptor object
     * @param offset offset to the start of the sound
     * @param length length of the sound
     * @param priority the priority of the sound. Currently has no effect. Use
     *                 a value of 1 for future compatibility.
     * @return a sound ID. This value can be used to play or unload the sound.
     */
    public int load(FileDescriptor fd, long offset, long length, int priority) {
    
    
        return _load(fd, offset, length, priority);
    }

下面就简单介绍一下四个load加载方法的使用场景:

  • int load(Context context, int resId, int priority) //从APK资源载入
  • int load(FileDescriptor fd, long offset, long length, int priority) //从FileDescriptor对象载入
  • int load(AssetFileDescriptor afd, int priority) //从Asset对象载入
  • int load(String path, int priority) //从完整文件路径名载入

他们都有一个共同参数priority,priority的作用是什么了,它为优先级设置参数,该参数目前还没有任何作用,Android建议将该参数设置为1,保持和未来的兼容性。

至于如何加载了,其实很简单,一般只需要把多个声音放到HashMap中去:

    soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 100); 
    soundPoolMap = new HashMap<Integer, Integer>();   
    soundPoolMap.put(1, soundPool.load(this, R.raw.testone, 1)); 
    soundPoolMap.put(2, soundPool.load(this, R.raw.testtwo, 1)); 

这样就完成了SoundPool的音频加载。

3. 播放控制
说道播放控制就比较常见了,大家都玩过酷狗音乐,qq音乐…这一类的音频软件,播放控制也很常见,包括了播放,暂停,关闭…当然,SoundPool同样具备这些播放控制方法。下面一一详细介绍这些方法:

  • 播放play方法
    /**
     * Play a sound from a sound ID.
     *
     * Play the sound specified by the soundID. This is the value
     * returned by the load() function. Returns a non-zero streamID
     * if successful, zero if it fails. The streamID can be used to
     * further control playback. Note that calling play() may cause
     * another sound to stop playing if the maximum number of active
     * streams is exceeded. A loop value of -1 means loop forever,
     * a value of 0 means don't loop, other values indicate the
     * number of repeats, e.g. a value of 1 plays the audio twice.
     * The playback rate allows the application to vary the playback
     * rate (pitch) of the sound. A value of 1.0 means play back at
     * the original frequency. A value of 2.0 means play back twice
     * as fast, and a value of 0.5 means playback at half speed.
     *
     * @param soundID a soundID returned by the load() function
     * @param leftVolume left volume value (range = 0.0 to 1.0)
     * @param rightVolume right volume value (range = 0.0 to 1.0)
     * @param priority stream priority (0 = lowest priority)
     * @param loop loop mode (0 = no loop, -1 = loop forever)
     * @param rate playback rate (1.0 = normal playback, range 0.5 to 2.0)
     * @return non-zero streamID if successful, zero if failed
     */
    public final int play(int soundID, float leftVolume, float rightVolume,
            int priority, int loop, float rate) {
    
    
        baseStart();
        return _play(soundID, leftVolume, rightVolume, priority, loop, rate);
    }

参数说明如下:

1:soundID:Load()函数返回的声音ID号。
2:leftVolume:左声道音量设置。
3:rightVolume:右声道音量设置。
4:priority:指定播放声音的优先级,数值越高,优先级越大。
5:loop:指定是否循环。-1表示无限循环,0表示不循环,其他值表示要重复播放的次数。
6:rate:指定播放速率。1.0的播放率可以使声音按照其原始频率。而2.0的播放速率,可以使声音按照其原始频率的两倍播放。如果为0.5的播放率,则播放速率是原始频率的一半。播放速率的取值范围是0.5至2.0。

  • pause方法
    /**
     * Pause a playback stream.
     *
     * Pause the stream specified by the streamID. This is the
     * value returned by the play() function. If the stream is
     * playing, it will be paused. If the stream is not playing
     * (e.g. is stopped or was previously paused), calling this
     * function will have no effect.
     *
     * @param streamID a streamID returned by the play() function
     */
    public native final void pause(int streamID);

参数说明如下:

暂停指定播放流的音效(streamID 应通过play()返回)。

  • resume 方法
    /**
     * Resume a playback stream.
     *
     * Resume the stream specified by the streamID. This
     * is the value returned by the play() function. If the stream
     * is paused, this will resume playback. If the stream was not
     * previously paused, calling this function will have no effect.
     *
     * @param streamID a streamID returned by the play() function
     */
    public native final void resume(int streamID);

参数说明如下:

继续播放指定播放流的音效(streamID 应通过play()返回)。

  • stop方法
    /**
     * Stop a playback stream.
     *
     * Stop the stream specified by the streamID. This
     * is the value returned by the play() function. If the stream
     * is playing, it will be stopped. It also releases any native
     * resources associated with this stream. If the stream is not
     * playing, it will have no effect.
     *
     * @param streamID a streamID returned by the play() function
     */
    public native final void stop(int streamID);

参数说明如下:

终止指定播放流的音效(streamID 应通过play()返回)。

注意,在使用play,pause,resume,stop方法时需要注意以下几点:

1.play()函数传递的是一个load()返回的soundID——指向一个被记载的音频资源 ,如果播放成功则返回一个非0的streamID——指向一个成功播放的流 ;同一个soundID 可以通过多次调用play()而获得多个不同的streamID (只要不超出同时播放的最大数量);、

2.pause()、resume()和stop()是针对播放流操作的,传递的是play()返回的streamID ;

3.play()中的priority参数,只在同时播放的流的数量超过了预先设定的最大数量是起作用,管理器将自动终止优先级低的播放流。如果存在多个同样优先级的流,再进一步根据其创建事件来处理,新创建的流的年龄是最小的,将被终止;

4.无论如何,程序退出时,手动终止播放并释放资源是必要的。就像io流操作一样。

5.paly()中的一些参数需要独立的设置:
final void setLoop(int streamID, int loop)//设置指定播放流的循环.
final void setVolume(int streamID, float leftVolume, float rightVolume)//设置指定播放流的音量.
final void setPriority(int streamID, int priority)// 设置指定播放流的音量.
final void setRate(int streamID, float rate)//设置指定播放流的优先级,上面已说明priority的作用;rate设置指定播放流的速率,一般取值在0.5-2.0之间

4. 释放资源

播放结束,我们可以调用释放方法释放所有SoundPool对象占据的内存和资源。而释放资源需要用到如下俩个方法:

-unload方法

  /**
     * Unload a sound from a sound ID.
     *
     * Unloads the sound specified by the soundID. This is the value
     * returned by the load() function. Returns true if the sound is
     * successfully unloaded, false if the sound was already unloaded.
     *
     * @param soundID a soundID returned by the load() function
     * @return true if just unloaded, false if previously unloaded
     */
    public native final boolean unload(int soundID);

很明显unload方法是load方法的对称方法,既然有load加载方法,那必然有unload取消加载方法。

参数说明:

卸载一个指定的音频资源

- release方法

    /**
     * Release the SoundPool resources.
     *
     * Release all memory and native resources used by the SoundPool
     * object. The SoundPool can no longer be used and the reference
     * should be set to null.
     */
    public final void release() {
    
    
        baseRelease();
        native_release();
    }

参数说明:

释放SoundPool中的所有音频资源

5. 简单的封装
了解了基本的使用方法,下面就简单封装一下,代码如下:

package com.bnd.myaudioandvideo.utils

import android.content.Context
import android.media.AudioManager
import android.media.RingtoneManager
import android.media.SoundPool
import android.net.Uri
import androidx.annotation.IntDef
import androidx.annotation.RawRes
import com.bnd.myaudioandvideo.R
import java.lang.annotation.Retention
import java.lang.annotation.RetentionPolicy
import java.util.*

/**
 * <pre>
 * desc  :  封装了SoundPool
 * thanks To:   http://flycatdeng.iteye.com/blog/2120043
 * http://www.2cto.com/kf/201408/325318.html
 * https://developer.android.com/reference/android/media/SoundPool.html
</pre> *
 */
class SoundPoolHelper @JvmOverloads constructor(maxStream: Int = 1, @TYPE streamType: Int = TYPE_MUSIC) {
    
    
    @IntDef(TYPE_MUSIC, TYPE_ALARM, TYPE_RING)
    @Retention(RetentionPolicy.SOURCE)
    annotation class TYPE

    @IntDef(RING_TYPE_MUSIC, RING_TYPE_ALARM, RING_TYPE_RING)
    @Retention(RetentionPolicy.SOURCE)
    annotation class RING_TYPE

    /*变量*/
    private val soundPool: SoundPool?
    private var NOW_RINGTONE_TYPE = RingtoneManager.TYPE_NOTIFICATION
    private var maxStream: Int
    private val ringtoneIds: MutableMap<String, Int?>

    /**
     * 设置RingtoneType,这只是关系到加载哪一个默认音频
     * 需要在load之前调用
     * @param ringtoneType  ringtoneType
     * @return  this
     */
    fun setRingtoneType(@RING_TYPE ringtoneType: Int): SoundPoolHelper {
    
    
        NOW_RINGTONE_TYPE = ringtoneType
        return this
    }

    /**
     * 加载音频资源
     * @param context   上下文
     * @param resId     资源ID
     * @return  this
     */
    fun load(context: Context?, ringtoneName: String, @RawRes resId: Int): SoundPoolHelper {
    
    
        if (maxStream == 0) return this
        maxStream--
        ringtoneIds[ringtoneName] = soundPool!!.load(context, resId, 1)
        return this
    }

    /**
     * 加载默认的铃声
     * @param context 上下文
     * @return  this
     */
    fun loadDefault(context: Context): SoundPoolHelper {
    
    
        val uri = getSystemDefaultRingtoneUri(context)
        if (uri == null) load(context, "default", R.raw.water_wave) else load(context, "default", ConvertUtils.uri2Path(context, uri)!!)
        return this
    }

    /**
     * 加载铃声
     * @param context   上下文
     * @param ringtoneName 自定义铃声名称
     * @param ringtonePath 铃声路径
     * @return  this
     */
    fun load(context: Context?, ringtoneName: String, ringtonePath: String): SoundPoolHelper {
    
    
        if (maxStream == 0) return this
        maxStream--
        ringtoneIds[ringtoneName] = soundPool!!.load(ringtonePath, 1)
        return this
    }

    /**
     * int play(int soundID, float leftVolume, float rightVolume, int priority, int loop, float rate) :
     * 1)该方法的第一个参数指定播放哪个声音;
     * 2) leftVolume 、
     * 3) rightVolume 指定左、右的音量:
     * 4) priority 指定播放声音的优先级,数值越大,优先级越高;
     * 5) loop 指定是否循环, 0 为不循环, -1 为循环;
     * 6) rate 指定播放的比率,数值可从 0.5 到 2 , 1 为正常比率。
     */
    fun play(ringtoneName: String, isLoop: Boolean) {
    
    
        if (ringtoneIds.containsKey(ringtoneName)) {
    
    
            soundPool!!.play(ringtoneIds[ringtoneName]!!, 1f, 1f, 1, if (isLoop) -1 else 0, 1f)
        }
    }

    fun playDefault() {
    
    
        play("default", false)
    }

    /**
     * 释放资源
     */
    fun release() {
    
    
        soundPool?.release()
    }

    /**
     * 获取系统默认铃声的Uri
     * @param context  上下文
     * @return  uri
     */
    private fun getSystemDefaultRingtoneUri(context: Context): Uri? {
    
    
        return try {
    
    
            RingtoneManager.getActualDefaultRingtoneUri(context, NOW_RINGTONE_TYPE)
        } catch (e: Exception) {
    
    
            null
        }
    }

    companion object {
    
    
        /*常量*/
        const val TYPE_MUSIC = AudioManager.STREAM_MUSIC
        const val TYPE_ALARM = AudioManager.STREAM_ALARM
        const val TYPE_RING = AudioManager.STREAM_RING
        const val RING_TYPE_MUSIC = RingtoneManager.TYPE_ALARM
        const val RING_TYPE_ALARM = RingtoneManager.TYPE_NOTIFICATION
        const val RING_TYPE_RING = RingtoneManager.TYPE_RINGTONE
    }

    /*方法*/
    init {
    
    
        soundPool = SoundPool(maxStream, streamType, 1)
        this.maxStream = maxStream
        ringtoneIds = HashMap()
    }
}

ConvertUtils工具类代码如下:

package com.bnd.myaudioandvideo.utils

import android.content.ContentResolver
import android.content.Context
import android.net.Uri
import android.provider.MediaStore

object ConvertUtils {
    
    
    /**
     * 把 Uri 转变 为 真实的 String 路径
     * @param context 上下文
     * @param uri  URI
     * @return 转换结果
     */
    fun uri2Path(context: Context, uri: Uri?): String? {
    
    
        if (null == uri) return null
        val scheme = uri.scheme
        var data: String? = null
        if (scheme == null) data = uri.path else if (ContentResolver.SCHEME_FILE == scheme) {
    
    
            data = uri.path
        } else if (ContentResolver.SCHEME_CONTENT == scheme) {
    
    
            val cursor = context.contentResolver.query(uri, arrayOf(MediaStore.Images.ImageColumns.DATA), null, null, null)
            if (null != cursor) {
    
    
                if (cursor.moveToFirst()) {
    
    
                    val index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
                    if (index > -1) {
    
    
                        data = cursor.getString(index)
                    }
                }
                cursor.close()
            }
        }
        return data
    }
}

MediaPlayer详解

在介绍MediaPlayer前,我觉得可以先看一下MediaPlayer的生命周期图,搞清楚MediaPlayer的生命周期图可以帮助我们在使用Android MediaPlayer时考虑情况更周全,写出代码也更条理清晰。MediaPlayer生命周期图如下:
在这里插入图片描述
这张状态转换图清晰的描述了MediaPlayer的各个状态,也列举了主要的方法的调用时序,每种方法只能在一些特定的状态下使用,如果使用时MediaPlayer的状态不正确则会引发IllegalStateException异常。

下面详细介绍这几个状态:

1)当一个MediaPlayer对象被刚刚用new操作符创建或是调用了reset()方法后,它就处于Idle状态。当调用了release()方法后,它就处于End状态。这两种状态之间是MediaPlayer对象的生命

  • 在一个新构建的MediaPlayer对象和一个调用了reset()方法的MediaPlayer对象之间有一个微小的但是十分重要的差别,在处于Idle状态时,调用getCurrentPosition(),getDuration(), getVideoHeight(), prepare() getVideoWidth(),setAudioStreamType(int), setLooping(boolean), setVolume(float,float), pause(), start(), stop(),seekTo(int), 或者 prepareAsync(),方法都是编程错误。当一个MediaPlayer对象刚被构建的时候,内部的播放引擎和对象的状态都没有改变,在这个时候调用以上的那些方法,框架将无法回调客户端程序注册的OnErrorListener.onError()方法;但若这个MediaPlayer对象调用了reset()方法之后,再调用以上的那些方法,内部的播放引擎就会回调客户端程序注册的OnErrorListener.onError()方法了,并将错误的状态传入。

  • 我们建议,一旦一个MediaPlayer对象不再被使用,应立即调用release()方法来释放在内部的播放引擎中与这个MediaPlayer对象关联的资源。资源可能包括如硬件加速组件的单态组件,若没有调用release()方法可能会导致之后的MediaPlayer对象实例无法使用这种单态硬件资源,从而退回到软件实现或运行失败。一旦MediaPlayer对象进入了End状态,它不能再被使用,也没有办法再迁移到其它状态。

  • 此外,使用new操作符创建的MediaPlayer对象处于Idle状态,而那些通过重载的create()便利方法创建的MediaPlayer对象却不是处于Idle状态。事实上,如果成功调用了重载的create()方法,那么这些对象已经是Prepare状态了。

2),在一般情况下,由于种种原因一些播放控制操作可能会失败,如不支持的音频/视频格式,缺少隔行扫描的音频/视频,分辨率太高,流超时等原因,等等。因此,错误报告和恢复在这种情况下是非常重要的。有时,由于编程错误,在处于无效状态的情况下调用了一个播放控制操作可能发生。在所有这些错误条件下,内部的播放引擎会调用一个由客户端程序员提供的OnErrorListener.onError()方法。客户端程序员可以通过调用MediaPlayer.setOnErrorListener(android.media.MediaPlayer.OnErrorListener)方法来注册OnErrorListener.

  • 一旦发生错误,MediaPlayer对象会进入到Error状态。
  • 为了重用一个处于Error状态的MediaPlayer对象,可以调用reset()方法来把这个对象恢复成Idle状态。
  • 注册一个OnErrorListener来获知内部播放引擎发生的错误。
  • 在不合法的状态下调用一些方法,如prepare(),prepareAsync()和setDataSource()方法会抛出IllegalStateException异常

3),调用setDataSource(FileDescriptor)方法,或setDataSource(String)方法,或setDataSource(Context,Uri)方法,或setDataSource(FileDescriptor,long,long)方法会使处于Idle状态的对象迁移到Initialized状态。

  • 若当此MediaPlayer处于其它的状态下,调用setDataSource()方法,会抛出IllegalStateException异常。
  • 好的编程习惯是不要疏忽了调用setDataSource()方法的时候可能会抛出的IllegalArgumentException异常和IOExcept

4),在开始播放之前,MediaPlayer对象必须要进入Prepared状态。

  • 有两种方法(同步和异步)可以使MediaPlayer对象进入Prepared状态:要么调用prepare()方法(同步),此方法返回就表示该MediaPlayer对象已经进入了Prepared状态;要么调用prepareAsync()方法(异步),此方法会使此MediaPlayer对象进入Preparing状态并返回,而内部的播放引擎会继续未完成的准备工作。当同步版本返回时或异步版本的准备工作完全完成时就会调用客户端程序员提供的OnPreparedListener.onPrepared()监听方法。可以调用MediaPlayer.setOnPreparedListener(android.media.MediaPlayer.OnPreparedListener)方法来注册OnPreparedListener.

  • Preparing是一个中间状态,在此状态下调用任何具备边影响的方法的结果都是未知的!

  • 在不合适的状态下调用prepare()和prepareAsync()方法会抛出IllegalStateException异常。当MediaPlayer对象处于Prepared状态的时候,可以调整音频/视频的属性,如音量,播放时是否一直亮屏,循环播放等。

5),要开始播放,必须调用start()方法。当此方法成功返回时,MediaPlayer的对象处于Started状态。isPlaying()方法可以被调用来测试某个MediaPlayer对象是否在Started状态。

  • 当处于Started状态时,内部播放引擎会调用客户端程序员提供的OnBufferingUpdateListener.onBufferingUpdate()回调方法,此回调方法允许应用程序追踪流播放的缓冲的状态。

  • 对一个已经处于Started 状态的MediaPlayer对象调用start()方法没有影响。

6), 播放可以被暂停,停止,以及调整当前播放位置。当调用pause()方法并返回时,会使MediaPlayer对象进入Paused状态。注意Started与Paused状态的相互转换在内部的播放引擎中是异步的。所以可能需要一点时间在isPlaying()方法中更新状态,若在播放流内容,这段时间可能会有几秒钟。

  • 调用start()方法会让一个处于Paused状态的MediaPlayer对象从之前暂停的地方恢复播放。当调用start()方法返回的时候,MediaPlayer对象的状态会又变成Started状态。

  • 对一个已经处于Paused状态的MediaPlayer对象pause()方法没有影响。

7),调用stop()方法会停止播放,并且还会让一个处于Started,Paused,Prepared或PlaybackCompleted状态的MediaPlayer进入Stopped状态。

  • 对一个已经处于Stopped状态的MediaPlayer对象stop()方法没有影响。

8),调用seekTo()方法可以调整播放的位置。

  • seekTo(int)方法是异步执行的,所以它可以马上返回,但是实际的定位播放操作可能需要一段时间才能完成,尤其是播放流形式的音频/视频。当实际的定位播放操作完成之后,内部的播放引擎会调用客户端程序员提供的OnSeekComplete.onSeekComplete()回调方法。可以通过setOnSeekCompleteListener(OnSeekCompleteListener)方法注册。

  • 注意,seekTo(int)方法也可以在其它状态下调用,比如Prepared,Paused和PlaybackCompleted状态。此外,目前的播放位置,实际可以调用getCurrentPosition()方法得到,它可以帮助如音乐播放器的应用程序不断更新播放进度

9),当播放到流的末尾,播放就完成了。

  • 如果调用了setLooping(boolean)方法开启了循环模式,那么这个MediaPlayer对象会重新进入Started状态。

  • 若没有开启循环模式,那么内部的播放引擎会调用客户端程序员提供的OnCompletion.onCompletion()回调方法。可以通过调用MediaPlayer.setOnCompletionListener(OnCompletionListener)方法来设置。内部的播放引擎一旦调用了OnCompletion.onCompletion()回调方法,说明这个MediaPlayer对象进入了PlaybackCompleted状态。

  • 当处于PlaybackCompleted状态的时候,可以再调用start()方法来让这个MediaPlayer对象再进入Started状态。

这张状态转换图清晰的描述了MediaPlayer的各个状态,也列举了主要的方法的调用时序,每种方法只能在一些特定的状态下使用,如果使用时MediaPlayer的状态不正确则会引发IllegalStateException异常。

AudioTrack详解

AudioTrack只能播放PCM数据流。AudioTrack是管理和播放单一音频资源的类。AudioTrack仅仅能播放已经解码的PCM流,用于PCM音频流的回放。

AudioTrack实现PCM音频播放

AudioTrack实现PCM音频播放只需要按照5步就可以实现pcm的音频播放。步骤如下:

  • 配置基本参数
  • 获取最小缓冲区大小
  • 创建AudioTrack对象
  • 获取PCM文件,转成DataInputStream
  • 开启/停止播放

1.配置基本参数

  • StreamType音频流类型
最主要的几种STREAM:
1:AudioManager.STREAM_MUSIC:用于音乐播放的音频流。
2:AudioManager.STREAM_SYSTEM:用于系统声音的音频流。
3:AudioManager.STREAM_RING:用于电话铃声的音频流。
4:AudioManager.STREAM_VOICE_CALL:用于电话通话的音频流。
5:AudioManager.STREAM_ALARM:用于警报的音频流。
6:AudioManager.STREAM_NOTIFICATION:用于通知的音频流。
7:AudioManager.STREAM_BLUETOOTH_SCO:用于连接到蓝牙电话时的手机音频流。
8:AudioManager.STREAM_SYSTEM_ENFORCED:在某些国家实施的系统声音的音频流。
9:AudioManager.STREAM_DTMF:DTMF音调的音频流。
10:AudioManager.STREAM_TTS:文本到语音转换(TTS)的音频流。

为什么分那么多种类型,其实原因很简单,比如你在听music的时候接到电话,这个时候music播放肯定会停止,此时你只能听到电话,如果你调节音量的话,这个调节肯定只对电话起作用。当电话打完了,再回到music,你肯定不用再调节音量了。

其实系统将这几种声音的数据分开管理,STREAM参数对AudioTrack来说,它的含义就是告诉系统,我现在想使用的是哪种类型的声音,这样系统就可以对应管理他们了。
  • MODE模式(static和stream两种)

    1. AudioTrack.MODE_STREAM

STREAM的意思是由用户在应用程序通过write方式把数据一次一次得写到AudioTrack中。这个和我们在socket中发送数据一样,应用层从某个地方获取数据,例如通过编解码得到PCM数据,然后write到AudioTrack。这种方式的坏处就是总是在JAVA层和Native层交互,效率损失较大。

  1. AudioTrack.MODE_STATIC

STATIC就是数据一次性交付给接收方。好处是简单高效,只需要进行一次操作就完成了数据的传递;缺点当然也很明显,对于数据量较大的音频回放,显然它是无法胜任的,因而通常只用于播放铃声、系统提醒等对内存小的操作

  • 采样率:mSampleRateInHz

采样率 (MediaRecoder 的采样率通常是8000Hz AAC的通常是44100Hz。 设置采样率为44100,目前为常用的采样率,官方文档表示这个值可以兼容所有的设置)

  • 通道数目:mChannelConfig
    首先得出声道数,目前最多只支持双声道。为什么最多只支持双声道?看下面的源码:
  static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) {
    
    
      int channelCount = 0;
      switch(channelConfig) {
    
    
      case AudioFormat.CHANNEL_OUT_MONO:
      case AudioFormat.CHANNEL_CONFIGURATION_MONO:
          channelCount = 1;
          break;
      case AudioFormat.CHANNEL_OUT_STEREO:
      case AudioFormat.CHANNEL_CONFIGURATION_STEREO:
          channelCount = 2;
          break;
      default:
          if (!isMultichannelConfigSupported(channelConfig)) {
    
    
              loge("getMinBufferSize(): Invalid channel configuration.");
              return ERROR_BAD_VALUE;
          } else {
    
    
              channelCount = AudioFormat.channelCountFromOutChannelMask(channelConfig);
          }
      }

  .......
  • 音频量化位数:mAudioFormat(只支持8bit和16bit两种。)
  if ((audioFormat !=AudioFormat.ENCODING_PCM_16BIT)

  && (audioFormat !=AudioFormat.ENCODING_PCM_8BIT)) {
    
    

  returnAudioTrack.ERROR_BAD_VALUE;

  }

2.最新缓冲区配置

mMinBufferSize取决于采样率、声道数和采样深度三个属性,那么具体是如何计算的呢?我们看一下源码

static public int getMinBufferSize(int sampleRateInHz, int channelConfig, int audioFormat) {
    
    
    
    ....

    int size = native_get_min_buff_size(sampleRateInHz, channelCount, audioFormat);
    if (size <= 0) {
    
    
        loge("getMinBufferSize(): error querying hardware");
        return ERROR;
    }
    else {
    
    
        return size;
    }
}

看到源码缓冲区的大小的实现在nativen层中,接着看下native层代码实现:

rameworks/base/core/jni/android_media_AudioTrack.cpp

static jint android_media_AudioTrack_get_min_buff_size(JNIEnv*env,  jobject thiz,

jint sampleRateInHertz,jint nbChannels, jint audioFormat) {
    
    

int frameCount = 0;

if(AudioTrack::getMinFrameCount(&frameCount, AUDIO_STREAM_DEFAULT,sampleRateInHertz) != NO_ERROR) {
    
    

    return -1;

 }

 return  frameCount * nbChannels * (audioFormat ==javaAudioTrackFields.PCM16 ? 2 : 1);

}

这里又调用了getMinFrameCount,这个函数用于确定至少需要多少Frame才能保证音频正常播放。那么Frame代表了什么意思呢?可以想象一下视频中帧的概念,它代表了某个时间点的一幅图像。这里的Frame也是类似的,它应该是指某个特定时间点时的音频数据量,所以android_media_AudioTrack_get_min_buff_size中最后采用的计算公式就是:

至少需要多少帧每帧数据量 = frameCount * nbChannels * (audioFormat ==javaAudioTrackFields.PCM16 ? 2 : 1);
公式中frameCount就是需要的帧数,每一帧的数据量又等于:
Channel数每个Channel数据量= nbChannels * (audioFormat ==javaAudioTrackFields.PCM16 ? 2 : 1)层层返回getMinBufferSize就得到了保障AudioTrack正常工作的最小缓冲区大小了。

3.创建AudioTrack对象

取到mMinBufferSize后,我们就可以创建一个AudioTrack对象了。它的构造函数原型是:

public AudioTrack(int streamType, int sampleRateInHz, int channelConfig, int audioFormat,
        int bufferSizeInBytes, int mode)
throws IllegalArgumentException {
    
    
    this(streamType, sampleRateInHz, channelConfig, audioFormat,
            bufferSizeInBytes, mode, AudioManager.AUDIO_SESSION_ID_GENERATE);
}

在源码中一层层往下看

public AudioTrack(AudioAttributes attributes, AudioFormat format, int bufferSizeInBytes,
        int mode, int sessionId)
                throws IllegalArgumentException {
    
    
    super(attributes, AudioPlaybackConfiguration.PLAYER_TYPE_JAM_AUDIOTRACK);
    
    .....

    // native initialization
    int initResult = native_setup(new WeakReference<AudioTrack>(this), mAttributes,
            sampleRate, mChannelMask, mChannelIndexMask, mAudioFormat,
            mNativeBufferSizeInBytes, mDataLoadMode, session, 0 /*nativeTrackInJavaObj*/);
    if (initResult != SUCCESS) {
    
    
        loge("Error code "+initResult+" when initializing AudioTrack.");
        return; // with mState == STATE_UNINITIALIZED
    }

    mSampleRate = sampleRate[0];
    mSessionId = session[0];

    if (mDataLoadMode == MODE_STATIC) {
    
    
        mState = STATE_NO_STATIC_DATA;
    } else {
    
    
        mState = STATE_INITIALIZED;
    }

    baseRegisterPlayer();
}

最终看到了又在native_setup方法中,在native中initialization,看看实现些什么了

/*frameworks/base/core/jni/android_media_AudioTrack.cpp*/

static int  android_media_AudioTrack_native_setup(JNIEnv*env, jobject thiz, jobject weak_this,

        jint streamType, jintsampleRateInHertz, jint javaChannelMask,

        jint audioFormat, jintbuffSizeInBytes, jint memoryMode, jintArray jSession)

{
    
       

    .....

    sp<AudioTrack>lpTrack = new AudioTrack();

    .....

AudioTrackJniStorage* lpJniStorage =new AudioTrackJniStorage();

这里调用了native_setup来创建一个本地AudioTrack对象,创建一个Storage对象,从这个Storage猜测这可能是存储音频数据的地方,我们再进入了解这个Storage对象。

if (memoryMode== javaAudioTrackFields.MODE_STREAM) {
    
    

    lpTrack->set(
    ...

    audioCallback, //回调函数

    &(lpJniStorage->mCallbackData),//回调数据

        0,

        0,//shared mem

        true,// thread cancall Java

        sessionId);//audio session ID

    } else if (memoryMode ==javaAudioTrackFields.MODE_STATIC) {
    
    

    ...

    lpTrack->set(
        ... 

        audioCallback, &(lpJniStorage->mCallbackData),0,      

        lpJniStorage->mMemBase,// shared mem

        true,// thread cancall Java

        sessionId);//audio session ID

    }

....// native_setup结束

调用set函数为AudioTrack设置这些属性——我们只保留两种内存模式(STATIC和STREAM)有差异的地方,入参中的倒数第三个是lpJniStorage->mMemBase,而STREAM类型时为null(0)。太深了,对于基础的知识先研究到这里吧

4.获取PCM文件,转成DataInputStream

根据存放PCM的路径获取到PCM文件

/**
 * 播放文件
 * @param path
 * @throws Exception
 */
private void setPath(String path) throws Exception {
    
    
    File file = new File(path);
    mDis = new DataInputStream(new FileInputStream(file));
}

5.开启/停止播放

  • 开始播放
    停止播放音频数据,如果是STREAM模式,会等播放完最后写入buffer的数据才会停止。如果立即停止,要调用pause()方法,然后调用flush方法,会舍弃还没有播放的数据。
  public void play()throws IllegalStateException {
    
    
      if (mState != STATE_INITIALIZED) {
    
    
          throw new IllegalStateException("play() called on uninitialized AudioTrack.");
      }
      //FIXME use lambda to pass startImpl to superclass
      final int delay = getStartDelayMs();
      if (delay == 0) {
    
    
          startImpl();
      } else {
    
    
          new Thread() {
    
    
              public void run() {
    
    
                  try {
    
    
                      Thread.sleep(delay);
                  } catch (InterruptedException e) {
    
    
                      e.printStackTrace();
                  }
                  baseSetStartDelayMs(0);
                  try {
    
    
                      startImpl();
                  } catch (IllegalStateException e) {
    
    
                      // fail silently for a state exception when it is happening after
                      // a delayed start, as the player state could have changed between the
                      // call to start() and the execution of startImpl()
                  }
              }
          }.start();
      }
  }
  • 停止播放
    停止播放音频数据,如果是STREAM模式,会等播放完最后写入buffer的数据才会停止。如果立即停止,要调用pause()方法,然后调用flush方法,会舍弃还没有播放的数据
public void stop()throws IllegalStateException {
    
    
      if (mState != STATE_INITIALIZED) {
    
    
          throw new IllegalStateException("stop() called on uninitialized AudioTrack.");
      }
      // stop playing
      synchronized(mPlayStateLock) {
    
    
          native_stop();
          baseStop();
          mPlayState = PLAYSTATE_STOPPED;
          mAvSyncHeader = null;
          mAvSyncBytesRemaining = 0;
      }
}
  • 暂停播放
    暂停播放,调用play()重新开始播放。
  • 释放本地AudioTrack资源
    AudioTrack.release()
  • 返回当前的播放状态
    AudioTrack.getPlayState()

注意: **
1:flush()只在模式为STREAM下可用。将音频数据刷进等待播放的队列,任何写入的数据如果没有提交的话,都会被舍弃,但是并不能保证所有用于数据的缓冲空间都可用于后续的写入。:
2:播放一个PCM文件,按照上面的五步走。
3:注意参数有配置,如量化位数是8BIT还是16BIT等。
4:想更加了解AudioTrack里的方法就动手写一个demo深入了解那些方法的用途。
5:能不能续播(还没有验证)

6.AudioTrack的简单封装

package com.bnd.myaudioandvideo.utils

import android.media.AudioFormat
import android.media.AudioManager
import android.media.AudioRecord
import android.media.AudioTrack
import android.os.Process
import java.io.DataInputStream
import java.io.File
import java.io.FileInputStream

class AudioTrackManager {
    
    
    private var mAudioTrack: AudioTrack? = null
    private var mDis //播放文件的数据流
            : DataInputStream? = null
    private var mRecordThread: Thread? = null
    private var isStart = false

    //指定缓冲区大小。调用AudioRecord类的getMinBufferSize方法可以获得。
    private var mMinBufferSize = 0
    private fun initData() {
    
    
        //根据采样率,采样精度,单双声道来得到frame的大小。
        mMinBufferSize = AudioTrack.getMinBufferSize(mSampleRateInHz, mChannelConfig, mAudioFormat) //计算最小缓冲区
        //注意,按照数字音频的知识,这个算出来的是一秒钟buffer的大小。
        //创建AudioTrack
        mAudioTrack = AudioTrack(mStreamType, mSampleRateInHz, mChannelConfig,
                mAudioFormat, mMinBufferSize, mMode)
    }

    /**
     * 销毁线程方法
     */
    private fun destroyThread() {
    
    
        try {
    
    
            isStart = false
            if (null != mRecordThread && Thread.State.RUNNABLE == mRecordThread!!.state) {
    
    
                try {
    
    
                    Thread.sleep(500)
                    mRecordThread!!.interrupt()
                } catch (e: Exception) {
    
    
                    mRecordThread = null
                }
            }
            mRecordThread = null
        } catch (e: Exception) {
    
    
            e.printStackTrace()
        } finally {
    
    
            mRecordThread = null
        }
    }

    /**
     * 启动播放线程
     */
    private fun startThread() {
    
    
        destroyThread()
        isStart = true
        if (mRecordThread == null) {
    
    
            mRecordThread = Thread(recordRunnable)
            mRecordThread!!.start()
        }
    }

    /**
     * 播放线程
     */
    var recordRunnable = Runnable {
    
    
        try {
    
    
            //设置线程的优先级
            Process.setThreadPriority(Process.THREAD_PRIORITY_URGENT_AUDIO)
            val tempBuffer = ByteArray(mMinBufferSize)
            var readCount = 0
            while (mDis!!.available() > 0) {
    
    
                readCount = mDis!!.read(tempBuffer)
                if (readCount == AudioTrack.ERROR_INVALID_OPERATION || readCount == AudioTrack.ERROR_BAD_VALUE) {
    
    
                    continue
                }
                if (readCount != 0 && readCount != -1) {
    
     //一边播放一边写入语音数据
                    //判断AudioTrack未初始化,停止播放的时候释放了,状态就为STATE_UNINITIALIZED
                    if (mAudioTrack!!.state == AudioTrack.STATE_UNINITIALIZED) {
    
    
                        initData()
                    }
                    mAudioTrack!!.play()
                    mAudioTrack!!.write(tempBuffer, 0, readCount)
                }
            }
            stopPlay() //播放完就停止播放
        } catch (e: Exception) {
    
    
            e.printStackTrace()
        }
    }

    /**
     * 播放文件
     * @param path
     * @throws Exception
     */
    @Throws(Exception::class)
    private fun setPath(path: String) {
    
    
        val file = File(path)
        mDis = DataInputStream(FileInputStream(file))
    }

    /**
     * 启动播放
     *
     * @param path
     */
    fun startPlay(path: String) {
    
    
        try {
    
    
//            //AudioTrack未初始化
//            if(mAudioTrack.getState() == AudioTrack.STATE_UNINITIALIZED){
    
    
//                throw new RuntimeException("The AudioTrack is not uninitialized");
//            }//AudioRecord.getMinBufferSize的参数是否支持当前的硬件设备
//            else if (AudioTrack.ERROR_BAD_VALUE == mMinBufferSize || AudioTrack.ERROR == mMinBufferSize) {
    
    
//                throw new RuntimeException("AudioTrack Unable to getMinBufferSize");
//            }else{
    
    
            setPath(path)
            startThread()
            //            }
        } catch (e: Exception) {
    
    
            e.printStackTrace()
        }
    }

    /**
     * 停止播放
     */
    fun stopPlay() {
    
    
        try {
    
    
            destroyThread() //销毁线程
            if (mAudioTrack != null) {
    
    
                if (mAudioTrack!!.state == AudioRecord.STATE_INITIALIZED) {
    
     //初始化成功
                    mAudioTrack!!.stop() //停止播放
                }
                if (mAudioTrack != null) {
    
    
                    mAudioTrack!!.release() //释放audioTrack资源
                }
            }
            if (mDis != null) {
    
    
                mDis!!.close() //关闭数据输入流
            }
        } catch (e: Exception) {
    
    
            e.printStackTrace()
        }
    }

    companion object {
    
    
        @Volatile
        private var mInstance: AudioTrackManager? = null

        //音频流类型
        private const val mStreamType = AudioManager.STREAM_MUSIC

        //指定采样率 (MediaRecoder 的采样率通常是8000Hz AAC的通常是44100Hz。 设置采样率为44100,目前为常用的采样率,官方文档表示这个值可以兼容所有的设置)
        private const val mSampleRateInHz = 44100

        //指定捕获音频的声道数目。在AudioFormat类中指定用于此的常量
        private const val mChannelConfig = AudioFormat.CHANNEL_CONFIGURATION_MONO //单声道

        //指定音频量化位数 ,在AudioFormaat类中指定了以下各种可能的常量。通常我们选择ENCODING_PCM_16BIT和ENCODING_PCM_8BIT PCM代表的是脉冲编码调制,它实际上是原始音频样本。
        //因此可以设置每个样本的分辨率为16位或者8位,16位将占用更多的空间和处理能力,表示的音频也更加接近真实。
        private const val mAudioFormat = AudioFormat.ENCODING_PCM_16BIT

        //STREAM的意思是由用户在应用程序通过write方式把数据一次一次得写到audiotrack中。这个和我们在socket中发送数据一样,
        // 应用层从某个地方获取数据,例如通过编解码得到PCM数据,然后write到audiotrack。
        private const val mMode = AudioTrack.MODE_STREAM

        /**
         * 获取单例引用
         *
         * @return
         */
        val instance: AudioTrackManager?
            get() {
    
    
                if (mInstance == null) {
    
    
                    synchronized(AudioTrackManager::class.java) {
    
    
                        if (mInstance == null) {
    
    
                            mInstance = AudioTrackManager()
                        }
                    }
                }
                return mInstance
            }
    }

    init {
    
    
        initData()
    }
}

总结

音频开发的知识点还是很多的,学习音频开发需要大家有足够的耐心,一步一个脚印的积累,只有这样才能把音频开发学好。下面推荐几个比较好的博主,希望对大家有所帮助。
csdn博主:《雷神雷霄骅》
51CTO博客:《Jhuster的专栏》

猜你喜欢

转载自blog.csdn.net/ljx1400052550/article/details/114270325
今日推荐