Git Product home page Git Product logo

audioplay's Introduction

使用MediaPlayer进行音频播放

播放本地文件,raw,assets,网络资源效果。

播放效果图

How to

To get a Git project into your build:
Step 1. Add the JitPack repository to your build file
Gradle
allprojects {
   repositories {
   	...
   	maven { url 'https://jitpack.io' }
   }
}
Step 2. Add the dependency
dependencies {
      implementation 'com.github.Chen-Xi-g:AudioPlay:v1.1.0'
}

如何使用(How to use)

1.初始化(Initialization)

/**
 * 在需要使用的类中进行初始化
 */
MediaPlayerUtils mMediaPlayerUtils = new MediaPlayerUtils();

2.使用(Use)

 /**
  *  设置播放类型。
  */
 //播放sd卡资源
 MediaPlayerUtils.setFilePlay(File file);
 //播放raw下文件
 MediaPlayerUtils.setRawPlay(Context context,int rawId);
 //播放assets下文件
 MediaPlayerUtils.setAssetsName(Context context,String assetsName);
 //播放网络资源
 MediaPlayerUtils.setNetPath(String netPath);

 //开始播放
 MediaPlayerUtils.start();
 //停止播放
 MediaPlayerUtils.stop();
 //暂停播放
 MediaPlayerUtils.pause();
 //继续播放
 MediaPlayerUtils.resume();
 //或
 MediaPlayerUtils.getMediaPlayer().start();
 //是否正在运行
 MediaPlayerUtils.isRunning();
//是否在播放中
 MediaPlayerUtils.isPlaying();
//播放文件的时长
 MediaPlayerUtils.getDuration();
//获取当前播放位置
 MediaPlayerUtils.getCurrentPosition();
//左右声道大小 0-1
 MediaPlayerUtils.setVolume(float leftVolume, float rightVolume);
//设置唤醒方式 需要在清单文件AndroidManifest.xml中添加权限 
//<uses-permission android:name="android.permission.WAKE_LOCK" />
 MediaPlayerUtils.setWakeMode(Context context, int mode);
//播放时不熄屏
 MediaPlayerUtils.setScreenOnWhilePlaying(boolean screenOn);
//指定播放位置 毫秒
 MediaPlayerUtils.seekTo(int msec);
//是否循环播放
 MediaPlayerUtils.setLooping(boolean looping);
//获取当前播放资源类型
 MediaPlayerUtils.getMusicType();
//多久获取一次进度 毫秒
 MediaPlayerUtils.setSleep(int sleep);
//获取MediaPlayer对象
 MediaPlayerUtils.getMediaPlayer();

3.MediaPlayer监听回调

/**
 * MediaPlayer状态回调
 */
MediaPlayerUtils.setMediaPlayFunctionListener();

public interface MediaPlayFunctionListener {
  // 准备完成
  void prepared();
  // 开始播放
  void start();
  // 暂停
  void pause();
  // 停止播放
  void stop();
  //重置
  void reset();
}

 /**
 * 获取当前信息的回调
 */
MediaPlayerUtils.setMediaPlayInfoListener();

public interface MediaPlayInfoListener {
  //播放错误监听
  void onError(MediaPlayer mp, int what, int extra);
  //播放完成监听
  void onCompletion(MediaPlayer mediaPlayer);
  //网络缓冲监听
  void onBufferingUpdate(MediaPlayer mediaPlayer, int i);
  //进度调整监听
  void onSeekComplete(MediaPlayer mediaPlayer);
  //时实播放进度
  void onSeekBarProgress(int progress);
}

具体代码可以看Demo中示例

如果你感觉对你有用的话请点一下Star吧,而且你还可以打赏一波(If you feel useful to you, please click Star, or you can reward it.)

如何联系我(How to contact me)

QQ: 1217056667

邮箱(Email): [email protected]

小站: www.minlukj.com

简书: https://www.jianshu.com/u/41def8c88ec3

audioplay's People

Contributors

chen-xi-g avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.