第一行安卓代码——播放多媒体文件8.4.1

    xiaoxiao2021-03-26  7

    8.4.1播放音频 在安卓中播放较长的音频文件,如背景音乐等都是通过MediaPlayer类来实现的,它对多种格式的音频文件提供了非常全面的控制方法,从而使播放音乐的工作变得非常简单。下面是MediaPlayer类中的常用的控制方法

    setDataSource() //设置音频文件的位置 prepare() //在播放音频文件之前调用这个方法完成准备工作 start() //开始或继续播放 stop() //停止播放,之后也无法再继续播放 pause() //暂停,可以继续 release() //释放掉与MediaPlayer有关的资源 isPlayering() //是否正在播放 getDuration() //获取载入音乐的时间长度

    原理很简单,直接上代码

    AndroidManifest.xml中声明用到的权限

    ... <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> ...

    布局文件:

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.hms.playaudiotest.MainActivity"> <Button android:id="@+id/play" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Play" /> <Button android:id="@+id/stop" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Stop" /> <Button android:id="@+id/Pause" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Pause"/> </LinearLayout>

    MainActivity

    public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private MediaPlayer mediaPlayer = new MediaPlayer(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button play = (Button) findViewById(R.id.play); Button stop = (Button) findViewById(R.id.stop); Button pause = (Button) findViewById(R.id.Pause); play.setOnClickListener(this); stop.setOnClickListener(this); pause.setOnClickListener(this); if(ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED){ ActivityCompat.requestPermissions(MainActivity.this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE }, 1); } else { initMediaPlayer(); } } @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; } } 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 onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { switch (requestCode) { case 1: if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { initMediaPlayer(); } else { Toast.makeText(this, "拒绝使用", Toast.LENGTH_SHORT).show(); finish(); } break; default: } } @Override protected void onDestroy() { super.onDestroy(); if(mediaPlayer != null) { mediaPlayer.stop(); mediaPlayer.release(); } } }
    转载请注明原文地址: https://ju.6miu.com/read-650232.html

    最新回复(0)