Android游戏之音频类设计

简介: Android游戏之音频类设计 1、基础知识:A. setVolumeControlStream(AudioManager.STREAM_MUSIC);http://developer.

Android游戏之音频类设计

 

1、基础知识:


A. setVolumeControlStream(AudioManager.STREAM_MUSIC);
http://developer.android.com/training/managing-audio/volume-playback.html
http://developer.android.com/reference/android/app/Activity.html#setVolumeControlStream(int)

public final void  setVolumeControlStream (int streamType)
Added in API level 1

Suggests an audio stream whose volume should be changed by the hardware volume controls.
The suggested audio stream will be tied to the window of this Activity. Volume requests which are received while the Activity is in the

foreground will affect this stream.
It is not guaranteed that the hardware volume controls will always change this stream's volume (for example, if a call is in progress, its

stream's volume may be changed instead). To reset back to the default, use USE_DEFAULT_STREAM_TYPE.

Parameters

streamType
The type of the audio stream whose volume should be changed by the hardware volume controls. 

AudioManager的值:http://developer.android.com/reference/android/media/AudioManager.html
int STREAM_MUSIC The audio stream for music playback 

B.getAssets ()
http://developer.android.com/reference/android/content/res/Resources.html#getAssets()
public final AssetManager  getAssets ()
Added in API level 1

Retrieve underlying AssetManager storage for these resources.
C.AssetManager
http://developer.android.com/reference/android/content/res/AssetManager.html
D.AssetFileDescriptor
http://developer.android.com/reference/android/content/res/AssetFileDescriptor.html
E.SoundPool
http://developer.android.com/reference/android/media/SoundPool.html
The SoundPool class manages and plays audio resources for applications.
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.
http://developer.android.com/reference/android/media/SoundPool.html#SoundPool(int, int, int)
Parameters

maxStreams
the maximum number of simultaneous streams for this SoundPool object
streamType
the audio stream type as described in AudioManager For example, game applications will normally use STREAM_MUSIC.
srcQuality
the sample-rate converter quality. Currently has no effect. Use 0 for the default.

Returns
a SoundPool object, or null if creation failed
F.MediaPlayer
http://developer.android.com/reference/android/media/MediaPlayer.html
G.MediaPlayer.OnCompletionListener
http://developer.android.com/reference/android/media/MediaPlayer.OnCompletionListener.html


2、设计3个接口Sound,Music,Audio,音频Audio由声音Sound和音乐Music构成

package com.badlogic.androidgames.framework;

public interface Sound {
    public void play(float volume);

    public void dispose();
}

package com.badlogic.androidgames.framework;

public interface Music {
    public void play();

    public void stop();

    public void pause();

    public void setLooping(boolean looping);

    public void setVolume(float volume);

    public boolean isPlaying();

    public boolean isStopped();

    public boolean isLooping();

    public void dispose();
}

package com.badlogic.androidgames.framework;

public interface Audio {
    public Music newMusic(String filename);

    public Sound newSound(String filename);
}


3、针对3个接口实现每一个对应的类

package com.badlogic.androidgames.framework.impl;

import java.io.IOException;

import android.content.res.AssetFileDescriptor;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;

import com.badlogic.androidgames.framework.Music;

public class AndroidMusic implements Music, OnCompletionListener {
    MediaPlayer mediaPlayer;
    boolean isPrepared = false;

    public AndroidMusic(AssetFileDescriptor assetDescriptor) {
        mediaPlayer = new MediaPlayer();
        try {
            mediaPlayer.setDataSource(assetDescriptor.getFileDescriptor(),
                    assetDescriptor.getStartOffset(),
                    assetDescriptor.getLength());
            mediaPlayer.prepare();
            isPrepared = true;
            mediaPlayer.setOnCompletionListener(this);
        } catch (Exception e) {
            throw new RuntimeException("Couldn't load music");
        }
    }

    @Override
    public void dispose() {
        if (mediaPlayer.isPlaying())
            mediaPlayer.stop();
        mediaPlayer.release();
    }

    @Override
    public boolean isLooping() {
        return mediaPlayer.isLooping();
    }

    @Override
    public boolean isPlaying() {
        return mediaPlayer.isPlaying();
    }

    @Override
    public boolean isStopped() {
        return !isPrepared;
    }

    @Override
    public void pause() {
        if (mediaPlayer.isPlaying())
            mediaPlayer.pause();
    }

    @Override
    public void play() {
        if (mediaPlayer.isPlaying())
            return;

        try {
            synchronized (this) {
                if (!isPrepared)
                    mediaPlayer.prepare();
                mediaPlayer.start();
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void setLooping(boolean isLooping) {
        mediaPlayer.setLooping(isLooping);
    }

    @Override
    public void setVolume(float volume) {
        mediaPlayer.setVolume(volume, volume);
    }

    @Override
    public void stop() {
        mediaPlayer.stop();
        synchronized (this) {
            isPrepared = false;
        }
    }

    @Override
    public void onCompletion(MediaPlayer arg0) {
        synchronized (this) {
            isPrepared = false;
        }
    }
}


 

package com.badlogic.androidgames.framework.impl;

import android.media.SoundPool;

import com.badlogic.androidgames.framework.Sound;

public class AndroidSound implements Sound {
    int soundId;
    SoundPool soundPool;
    
    public AndroidSound(SoundPool soundPool,int soundId) {
        this.soundId = soundId;
        this.soundPool = soundPool;
    }

    @Override
    public void play(float volume) {
        soundPool.play(soundId, volume, volume, 0, 0, 1);
    }

    @Override
    public void dispose() {
        soundPool.unload(soundId);
    }

}


 

package com.badlogic.androidgames.framework.impl;

import java.io.IOException;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.media.AudioManager;
import android.media.SoundPool;

import com.badlogic.androidgames.framework.Audio;
import com.badlogic.androidgames.framework.Music;
import com.badlogic.androidgames.framework.Sound;

public class AndroidAudio implements Audio {
    AssetManager assets;
    SoundPool soundPool;

    public AndroidAudio(Activity activity) {
        activity.setVolumeControlStream(AudioManager.STREAM_MUSIC);
        this.assets = activity.getAssets();
        this.soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);
    }

    @Override
    public Music newMusic(String filename) {
        try {
            AssetFileDescriptor assetDescriptor = assets.openFd(filename);
            return new AndroidMusic(assetDescriptor);
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load music '" + filename + "'");
        }
    }

    @Override
    public Sound newSound(String filename) {
        try {
            AssetFileDescriptor assetDescriptor = assets.openFd(filename);
            int soundId = soundPool.load(assetDescriptor, 0);
            return new AndroidSound(soundPool, soundId);
        } catch (IOException e) {
            throw new RuntimeException("Couldn't load sound '" + filename + "'");
        }
    }   
}


 

本文浅述了“beginning-android-games”书本的相关基础知识和相关类的设计分析,
书本和源码:http://download.csdn.net/detail/yangzhenping/8420261
本篇中的类实现来自“beginning-android-games\ch07-gl-basics”

目录
相关文章
|
4月前
|
Android开发
【Android App】蓝牙的设备配对、音频传输、点对点通信的讲解及实战(附源码和演示 超详细)
【Android App】蓝牙的设备配对、音频传输、点对点通信的讲解及实战(附源码和演示 超详细)
92 0
|
6月前
|
数据库连接 Android开发
安卓android期末项目之纸牌游戏之三
安卓android期末项目之纸牌游戏之三
64 0
|
6月前
|
存储 Android开发 数据库管理
Android安卓项目猜纸牌游戏之二 实体类
Android安卓项目猜纸牌游戏之二 实体类
29 0
|
4月前
|
XML Java Android开发
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
Android Studio App开发中使用录音机、MediaRecorder录制音频和MediaPlayer播放音频讲解及实战(附源码)
72 0
|
3月前
|
算法 Java 定位技术
分享104个益智休闲安卓游戏源码,总有一款适合你
分享104个益智休闲安卓游戏源码,总有一款适合你
150 1
|
4月前
|
XML 编解码 算法
Android开发音效中录制WAV音频和录制MP3音频的讲解及实战(超详细 附源码)
Android开发音效中录制WAV音频和录制MP3音频的讲解及实战(超详细 附源码)
65 0
|
3月前
|
编解码 移动开发 人工智能
android游戏源码
android游戏源码
77 0
|
4月前
|
XML Java Android开发
Android 开发中原始音频的录播和和自定义音频控制条的讲解及实战(超详细 附源码)
Android 开发中原始音频的录播和和自定义音频控制条的讲解及实战(超详细 附源码)
27 0
|
4月前
|
XML 存储 Java
Android 开发音频录播中媒体录制器MediaRecorder和媒体播放器MediaPlayer的讲解及实战(超详细 附源码)
Android 开发音频录播中媒体录制器MediaRecorder和媒体播放器MediaPlayer的讲解及实战(超详细 附源码)
42 0
|
4月前
|
XML Java Android开发
Android App开发音量调节中实现拖动条和滑动条和音频管理器AudioManager讲解及实战(超详细 附源码和演示视频)
Android App开发音量调节中实现拖动条和滑动条和音频管理器AudioManager讲解及实战(超详细 附源码和演示视频)
71 0