安卓播放音频

简介:

播放音频

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卡,一种是播放应用中的音频,一种是播放在线的。

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

422101-20171021144558006-444645305.png


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/7704772.html,如需转载请自行联系原作者

相关文章
iOS- 关于AVAudioSession的使用——后台播放音乐
iOS- 关于AVAudioSession的使用——后台播放音乐
445 0
iOS- 关于AVAudioSession的使用——后台播放音乐
|
9月前
|
Android开发 iOS开发
iOS 一个类似安卓的日期选择器
在iOS的app上一般的日期选择器都会选择用 UIDatePicker 来做,但是我之前的产品经理并不喜欢那种选择的模式。说事要统一安卓和iOS的设计风格。这就是我写这个选择器的初衷。
|
iOS开发
ios 音乐后台播放
ios 音乐后台播放
54 0
|
iOS开发
IOS播放音乐
IOS播放音乐
41 0
|
iOS开发
iOS后台播放背景音乐文件
iOS后台播放背景音乐文件
113 0
|
前端开发 Android开发
关于安卓自定义进度条(二)
关于安卓自定义进度条(二)
192 0
|
编解码 API 开发工具
iOS 屏幕共享
前言:由于最近项目中需要使用到屏幕共享,所以对iOS屏幕共享进行了一番调研,在这里也分享下踩坑之路。
|
开发工具 iOS开发
IOS 使用 ZbarSDK 二维码扫描
IOS 使用 ZbarSDK 二维码扫描
193 0
IOS 使用 ZbarSDK 二维码扫描
|
iOS开发 开发者
iOS录屏框架ReplayKit的应用总结
iOS录屏框架ReplayKit的应用总结
996 0
|
iOS开发 Windows