• 安卓播放音频


    播放音频

    1.设置界面

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/play"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Play"
            />
    
        <Button
            android:id="@+id/pause"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Pause"
            />
    
        <Button
            android:id="@+id/stop"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Stop"
            />
    
    </LinearLayout>
    
    

    2.配置权限

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="demo.jq.com.playaudiotest">
    
        <application
            android:allowBackup="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/AppTheme">
            <activity android:name=".MainActivity">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
    
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    </manifest>
    

    3.实现功能

    package demo.jq.com.playaudiotest;
    
    import android.content.pm.PackageManager;
    import android.media.MediaPlayer;
    import android.net.Uri;
    import android.os.Environment;
    import android.support.annotation.NonNull;
    import android.support.v4.app.ActivityCompat;
    import android.support.v4.content.ContextCompat;
    import android.support.v4.os.EnvironmentCompat;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    import java.io.File;
    import java.util.jar.Manifest;
    
    /**
     * @author jim
     * implements View.OnClickListener 继承接口
     */
    public class MainActivity extends AppCompatActivity implements View.OnClickListener{
        private MediaPlayer mediaPlayer = new MediaPlayer();
        private static final String TAG = "MainActivity";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            Button play = (Button) findViewById(R.id.play);
            Button pause = (Button) findViewById(R.id.pause);
            Button stop = (Button) findViewById(R.id.stop);
    
            play.setOnClickListener(this);
            pause.setOnClickListener(this);
            stop.setOnClickListener(this);
    
            if (ContextCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {
                ActivityCompat.requestPermissions(MainActivity.this,new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
            } else {
                initMediaPlayer();
            }
    
    
        }
    
        private void initMediaPlayer() {
            try {
                Uri setDataSourceuri = Uri.parse("android.resource://demo.jq.com.playaudiotest/"+R.raw.music);
                mediaPlayer.setDataSource(this,setDataSourceuri);
                mediaPlayer.prepare();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public void onRequestPermissionsResult(int requestCode,  String[] permissions,  int[] grantResults) {
            switch (requestCode) {
                case 1:
                    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                        initMediaPlayer();
                    } else {
                        Toast.makeText(this,"拒绝权限将无法使用程序",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                    break;
                default:
            }
        }
    
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.play:
                    if (!mediaPlayer.isPlaying()) {
                        mediaPlayer.start();
                    }
                    break;
                case R.id.pause:
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.pause();
                    }
                    break;
                case R.id.stop:
                    if (mediaPlayer.isPlaying()) {
                        mediaPlayer.reset();
                        initMediaPlayer();
                    }
                    break;
                default:
                    break;
            }
        }
    
        @Override
        public void onDestroy() {
            super.onDestroy();
            if (mediaPlayer != null) {
                mediaPlayer.stop();
                mediaPlayer.release();
            }
        }
    }
    
    

    这里有个关键就是获取音频文件的地址。

    有几种情况,一种是播放sd卡,一种是播放应用中的音频,一种是播放在线的。

    上面的案例是播放应用中的。

    有意思的!确实能播放!

  • 相关阅读:
    K-means Algorithm
    Naive Bayes Algorithm
    Generalized Linear Models
    Support Vector Machine
    wordpress迁移
    Gaussian Discriminant Analysis
    Locally Weighted Regression
    Matlab绘图高级部分
    Supervised Learning-Regression
    html 字符串互转DOM
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/7704772.html
Copyright © 2020-2023  润新知