zoukankan      html  css  js  c++  java
  • 10_播放音效

    copy 音效文件 explosion.ogg 和 coin.ogg 到  assets 目录下


    import java.io.IOException;

    import android.media.AudioManager;
    import android.media.SoundPool;
    import android.os.Bundle;
    import android.app.Activity;
    import android.content.res.AssetFileDescriptor;
    import android.content.res.AssetManager;
    import android.view.Menu;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnTouchListener;
    import android.widget.TextView;

    public class MainActivity extends Activity implements OnTouchListener {
     SoundPool soundPool;
        int explosionId = -1;
        int explosionId1 = -1;
       
        int playFlag = 0;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            TextView textView = new TextView(this);
            textView.setOnTouchListener(this);
            setContentView(textView);

           
            setVolumeControlStream(AudioManager.STREAM_MUSIC);
            soundPool = new SoundPool(20, AudioManager.STREAM_MUSIC, 0);

            try {
                AssetManager assetManager = getAssets();
                AssetFileDescriptor descriptor = assetManager
                        .openFd("explosion.ogg");
                explosionId = soundPool.load(descriptor, 1);
               
                AssetFileDescriptor descriptor1 = assetManager
                        .openFd("coin.ogg");
                explosionId1 = soundPool.load(descriptor1, 1);
               
            } catch (IOException e) {
                textView.setText("Couldn't load sound effect from asset, "
                        + e.getMessage());
            }
        }

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if (explosionId != -1) {
                 playFlag++;
                 if(playFlag %2 ==0)
                 {
                    soundPool.play(explosionId, 1, 1, 0, 0, 1);
                 }
                 else
                 {
                   soundPool.play(explosionId1, 1, 1, 0, 0, 1);
                 }
                 
                }
            }
            return true;
        }
    }

  • 相关阅读:
    c#的逆向工程-IL指令集
    利用nginx concat模块合并js css
    DotNetOpenAuth实践之Webform资源服务器配置
    STL使用迭代器逆向删除
    驱动安装时的错误
    How to detect the presence of the Visual C++ 2010 redistributable package
    Shell脚本使用汇总整理——文件夹及子文件备份脚本
    Shell脚本使用汇总整理
    Shell脚本使用汇总整理——mysql数据库5.7.8以后备份脚本
    Shell脚本使用汇总整理——mysql数据库5.7.8以前备份脚本
  • 原文地址:https://www.cnblogs.com/xl711436/p/3060477.html
Copyright © 2011-2022 走看看