一、MediaPlay状态机详解(MediaPlay的生命周期)
MediaPlayer状态机如下图所示
3、在AndroidManifest.xml中添加权限
android:requestLegacyExternalStorage="true"
public class PermissionsManagement {
private static final String TAG = "PermissionsManagement";
public static void requestMyPermissions(Activity mActivity) {
if (ContextCompat.checkSelfPermission(mActivity,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//没有授权,编写申请权限代码
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 100);
} else {
Log.d(TAG, "requestMyPermissions: 有写SD权限");
}
if (ContextCompat.checkSelfPermission(mActivity,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
//没有授权,编写申请权限代码
ActivityCompat.requestPermissions(mActivity, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 100);
} else {
Log.d(TAG, "requestMyPermissions: 有读SD权限");
}
}
}
4、然后在MainActivity 实现功能。
public class MainActivity extends AppCompatActivity implements MyMusicPlayer.OnMyPreparedListener {
private static final String TAG = "wq892373445";
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
private MyMusicPlayer mMyMusicPlayer;
//显示流媒体的总播放时长
private TextView tv_end;
private SeekBar seekbar;
private TextView tv_start;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PermissionsManagement.requestMyPermissions(this);
seekbar = (SeekBar)findViewById(R.id.seekbar);
tv_end = (TextView)findViewById(R.id.tv_end);
tv_start = (TextView)findViewById(R.id.tv_start);
//绑定监听器,监听拖动到指定位置
seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
int duration2 = mMyMusicPlayer.getDuration() / 1000;//获取音乐总时长
int position = mMyMusicPlayer.getCurrentPosition();//获取当前播放的位置
tv_start.setText(mMyMusicPlayer.calculateTime(position / 1000));//开始时间
tv_end.setText(mMyMusicPlayer.calculateTime(duration2));//总时长
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
mMyMusicPlayer.setSeekbarChaning(true);
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
mMyMusicPlayer.setSeekbarChaning(false);
mMyMusicPlayer.setSeekto(seekBar.getProgress());//在当前位置播放
tv_start.setText(mMyMusicPlayer.formattime(mMyMusicPlayer.getCurrentPosition()));
}
});
mMyMusicPlayer = new MyMusicPlayer();
mMyMusicPlayer.setOnMyPreparedListener(this);
int duration2 = mMyMusicPlayer.getDuration() / 1000;//获取音乐总时长
int position = mMyMusicPlayer.getCurrentPosition();//获取当前播放的位置
tv_start.setText(mMyMusicPlayer.calculateTime(position / 1000));//开始时间
tv_end.setText(mMyMusicPlayer.calculateTime(duration2));//总时长
}
public native String stringFromJNI();
@Override
protected void onDestroy() {
mMyMusicPlayer.release();
super.onDestroy();
}
public void onPlay(View view) {
mMyMusicPlayer.Play(seekbar);
}
public void onPause(View view) {
mMyMusicPlayer.Pause();
}
public void onReplay(View view) {
mMyMusicPlayer.Replay();
}
public void onStop(View view) {
mMyMusicPlayer.Stop();
}
public void onLooping(View view) {
mMyMusicPlayer.setLooping(true);
}
@Override
public void onMyPrepared() {
Log.d(TAG, "onPrepared()");
// 装载完毕回调
//获取流媒体的总播放时长,单位是毫秒。
tv_end.setText(mMyMusicPlayer.calculateTime((mMyMusicPlayer.getDuration()/ 1000)));
Log.d(TAG, "总的播放时长:"+mMyMusicPlayer.getDuration());
//获取当前流媒体的播放的位置,单位是毫秒
tv_start.setText(mMyMusicPlayer.calculateTime((mMyMusicPlayer.getCurrentPosition()/ 1000)));
}
}
路过独木桥!!
原创文章 137获赞 5访问量 2万+
关注
私信
展开阅读全文
作者:路过独木桥!!