Skip to content
This repository has been archived by the owner on Jul 29, 2022. It is now read-only.

Commit

Permalink
Android MediaPlayer进行音频播放的工具类,回调各种状态方便开发使用。
Browse files Browse the repository at this point in the history
  • Loading branch information
Chen-Xi-g committed Nov 15, 2019
0 parents commit c2bbb7c
Show file tree
Hide file tree
Showing 52 changed files with 1,446 additions and 0 deletions.
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
*.iml
.gradle
/local.properties
/.idea/libraries
/.idea/modules.xml
/.idea/workspace.xml
.DS_Store
/build
/captures
.externalNativeBuild
Binary file added .idea/caches/build_file_checksums.ser
Binary file not shown.
29 changes: 29 additions & 0 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 19 additions & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 34 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
29 changes: 29 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apply plugin: 'com.android.application'

android {
compileSdkVersion 28
defaultConfig {
applicationId "com.minlukj.audioplay"
minSdkVersion 26
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':mediaplaylib')
}
21 changes: 21 additions & 0 deletions app/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.minlukj.audioplay;

import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import static org.junit.Assert.*;

/**
* Instrumented test, which will execute on an Android device.
*
* @see <a href="http://d.android.com/tools/testing">Testing documentation</a>
*/
@RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest {
@Test public void useAppContext() {
// Context of the app under test.
Context appContext = InstrumentationRegistry.getTargetContext();

assertEquals("com.minlukj.audioplay", appContext.getPackageName());
}
}
23 changes: 23 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.minlukj.audioplay">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<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>

</manifest>
Binary file added app/src/main/assets/music.mp3
Binary file not shown.
Binary file added app/src/main/assets/music2.mp3
Binary file not shown.
173 changes: 173 additions & 0 deletions app/src/main/java/com/minlukj/audioplay/MainActivity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
package com.minlukj.audioplay;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import com.minlukj.mediaplaylib.MediaPlayFunctionListener;
import com.minlukj.mediaplaylib.MediaPlayInfoListener;
import com.minlukj.mediaplaylib.MediaPlayerUtils;
import java.io.File;

import static com.minlukj.mediaplaylib.MediaPlayerUtils.PLAY_STATE0;
import static com.minlukj.mediaplaylib.MediaPlayerUtils.PLAY_STATE1;
import static com.minlukj.mediaplaylib.MediaPlayerUtils.PLAY_STATE2;
import static com.minlukj.mediaplaylib.MediaPlayerUtils.PLAY_STATE3;

public class MainActivity extends AppCompatActivity {

private String TAG = MainActivity.class.getCanonicalName();

private ProgressBar mMusicProgressBar,mMusicNetProgressBar;
private Button mFile,mRaw,mAssets,mNet,mStart,mPause,mStop;
private TextView mMusicType;

private MediaPlayerUtils mMediaPlayerUtils;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMusicProgressBar = findViewById(R.id.music_progressBar);
mMusicNetProgressBar = findViewById(R.id.music_net_progressBar);
mFile = findViewById(R.id.music_file);
mRaw = findViewById(R.id.music_raw);
mAssets = findViewById(R.id.music_assets);
mNet = findViewById(R.id.music_net);
mStart = findViewById(R.id.music_start);
mPause = findViewById(R.id.music_pause);
mStop = findViewById(R.id.music_stop);
mMusicType = findViewById(R.id.music_type);
mMusicNetProgressBar.setMax(100);

mMediaPlayerUtils = new MediaPlayerUtils();
initOnClick();
mMediaPlayerUtils.setMediaPlayFunctionListener(new MediaPlayFunctionListener() {
@Override public void prepared() {
//准备完毕
Log.i(TAG,"准备完毕自动开始播放");
}

@Override public void start() {
//开始播放
mMusicProgressBar.setMax(mMediaPlayerUtils.getDuration());
}

@Override public void pause() {
//暂停播放
Log.i(TAG,"暂停了播放");
}

@Override public void stop() {
//停止播放
Log.i(TAG,"停止了播放");
}

@Override public void reset() {
//重置
Log.i(TAG,"重置了播放");
}
});

mMediaPlayerUtils.setMediaPlayInfoListener(new MediaPlayInfoListener() {
@Override public void onError(MediaPlayer mp, int what, int extra) {
//错误监听
Log.e(TAG,"播放错误");
}

@Override public void onCompletion(MediaPlayer mediaPlayer) {
//播放完成监听
Log.i(TAG,"播放完成");
}

@Override public void onBufferingUpdate(MediaPlayer mediaPlayer, int i) {
//网络缓冲进度监听
Log.i(TAG,String.valueOf(i));
mMusicNetProgressBar.setProgress(i);
}

@Override public void onSeekComplete(MediaPlayer mediaPlayer) {
//进度调整监听
Log.i(TAG,"调整了进度");
}

@Override public void onSeekBarProgress(int progress) {
//播放进度监听
mMusicProgressBar.setProgress(progress);
}
});
}

private void initOnClick() {
mFile.setOnClickListener(mOnClickListener);
mRaw.setOnClickListener(mOnClickListener);
mAssets.setOnClickListener(mOnClickListener);
mNet.setOnClickListener(mOnClickListener);
mStart.setOnClickListener(mOnClickListener);
mPause.setOnClickListener(mOnClickListener);
mStop.setOnClickListener(mOnClickListener);
}

private View.OnClickListener mOnClickListener = new View.OnClickListener() {
@Override public void onClick(View v) {
if (v == mFile){
//播放本地文件
mMediaPlayerUtils.setFilePlay(new File(Environment.getExternalStorageDirectory(),"music.mp3"));
}else if (v == mRaw){
//播放Raw
mMediaPlayerUtils.setRawPlay(MainActivity.this,R.raw.music3);
}else if (v == mAssets){
//播放Assets
mMediaPlayerUtils.setAssetsName(MainActivity.this,"music2.mp3");
}else if (v == mNet){
//播放网络资源
mMediaPlayerUtils.setNetPath("http://www.minlukj.com/music.mp3");
}else if (v == mStart){
//开始播放
mMediaPlayerUtils.start();
switch (mMediaPlayerUtils.getMusicType()){
case PLAY_STATE0:
mMusicType.setText("正在播放文件类型");
break;
case PLAY_STATE1:
mMusicType.setText("正在播放RAW下文件资源");
break;
case PLAY_STATE2:
mMusicType.setText("正在播放ASSETS文件下资源");
break;
case PLAY_STATE3:
mMusicType.setText("正在播放网络资源");
break;

}
}else if (v == mPause){
//暂停播放
mMediaPlayerUtils.pause();
}else if (v == mStop){
//停止播放
mMediaPlayerUtils.stop();
}
}
};

@Override protected void onDestroy() {
super.onDestroy();
if (mMediaPlayerUtils != null)
mMediaPlayerUtils.destory();
}

@Override protected void onResume() {
super.onResume();
if (mMediaPlayerUtils != null)
mMediaPlayerUtils.resume();
}

@Override protected void onPause() {
super.onPause();
if (mMediaPlayerUtils != null)
mMediaPlayerUtils.pause();
}
}
Loading

0 comments on commit c2bbb7c

Please sign in to comment.