标签:放音 exception path lease err str start bsp isp
MediaPlayer常见用法
关键代码:
public class MainActivity extends Activity { private Button play,pause,stop; private MediaPlayer mediaPlayer = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initMusic(); } private void initMusic(){ try{ File file = new File(Environment.getExternalStorageDirectory()+"/Guo","a.mp3"); mediaPlayer.setDataSource(file.getPath()); mediaPlayer.prepare(); }catch (Exception e){ e.printStackTrace(); } } protected void myClick(View v){ if( v.getId() == R.id.play ){ if( !mediaPlayer.isPlaying() ){ mediaPlayer.start(); //开始播放 } } if( v.getId() == R.id.pause ){ if( mediaPlayer.isPlaying() ){ mediaPlayer.pause(); // 暂停 } } if( v.getId() == R.id.stop ){ if( mediaPlayer.isPlaying() ){ mediaPlayer.reset(); // 停止播放 initMusic(); } } } @Override protected void onDestroy() { super.onDestroy(); if( mediaPlayer != null ){ mediaPlayer.stop(); mediaPlayer.release(); } } }
所需权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
标签:放音 exception path lease err str start bsp isp
原文地址:http://www.cnblogs.com/itfenqing/p/6747263.html