每天进步一点----- MediaPlayer
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了每天进步一点----- MediaPlayer相关的知识,希望对你有一定的参考价值。
package com.example.playaudiotest; import java.io.File; import android.app.Activity; import android.media.MediaPlayer; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { private Button play; private Button pause; private Button stop; private MediaPlayer mediaPlayer = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); play = (Button) findViewById(R.id.play); pause = (Button) findViewById(R.id.pause); stop = (Button) findViewById(R.id.stop); play.setOnClickListener(this); pause.setOnClickListener(this); stop.setOnClickListener(this); initMediaPlayer(); } private void initMediaPlayer() { try { File file = new File(Environment.getExternalStorageDirectory(), "music.mp3"); mediaPlayer.setDataSource(file.getPath()); mediaPlayer.prepare(); } catch (Exception e) { e.printStackTrace(); } } @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 protected void onDestroy() { super.onDestroy(); if (mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } }
以上是关于每天进步一点----- MediaPlayer的主要内容,如果未能解决你的问题,请参考以下文章
[每天进步一点点~] uni-app css 制作雷达扫描、波浪移动动画效果
每天进步一点点:生产环境上的代码迁移:重写Uber 无范式数据仓库的分片层