• android中MediaPlayer类的用法


    用法直接看sample

    package com.turtle920.androidaudioprocess;
    
    import android.media.MediaPlayer;
    import android.os.Environment;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    import java.io.IOException;
    
    public class MainActivity extends AppCompatActivity {
    
        MediaPlayer mp = new MediaPlayer();
        float leftVol=0.5f, rightVol=0.5f;
        String song = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Music/tf.mp3";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            try {
                mp.setDataSource(song);
                mp.prepare();
                mp.setVolume(leftVol,rightVol);
            } catch (Exception e) {
                Log.e("DEBUG", "" + e.toString());
                e.printStackTrace();
            }
    
            Button button1 = (Button) findViewById(R.id.button_start);
            button1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mp.start();
                }
            });
    
            Button button2 = (Button) findViewById(R.id.button_pause);
            button2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mp.pause();
                }
            });
    
            Button button3 = (Button) findViewById(R.id.button_replay);
            button3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mp.stop();
                    try {
                        mp.prepare();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            });
    
            Button button4 = (Button)findViewById(R.id.button_l);
            button4.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (leftVol+0.05f<=1.0f) leftVol+=0.05f;
                    if (rightVol-0.05f>=0f) rightVol-=0.05f;
                    Log.e("DEBUG",""+leftVol+" "+rightVol);
                    mp.setVolume(leftVol, rightVol);
                }
            });
    
            Button button5 = (Button)findViewById(R.id.button_r);
            button5.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (leftVol-0.05f>=0f) leftVol-=0.05f;
                    if (rightVol+0.05f<=1.0f) rightVol+=0.05f;
                    Log.e("DEBUG",""+leftVol+" "+rightVol);
                    mp.setVolume(leftVol, rightVol);
                }
            });
    
        }
    }

    注意要在manifest里申明权限

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    另外如果真机环境API 23+ 还需要在机子上给app读取SD卡得权限

  • 相关阅读:
    spring事务管理中,使用Synchronized修饰事务方法,同步为什么会失效
    java枚举类是怎么初始化的,为什么说枚举类是线程安全的
    spring事务管理中,同一个Service类中非事务方法调用事务方法,事务失效解决方法(3种)
    spring boot借助freemarker实现word文档模板导出
    mybatis plus使用报错:Caused by: java.sql.SQLException: sql injection violation, syntax error: syntax err
    js中遍历枚举类型
    IDEA 设置SVN提交忽略文件 target .iml
    python中的open( )函数
    使用crf++
    安装crf++
  • 原文地址:https://www.cnblogs.com/turtle920/p/4949843.html
Copyright © 2020-2023  润新知