zoukankan      html  css  js  c++  java
  • 多媒体播放API 生命周期束&简介

    多媒体播放API 生命周期束&简介

    public class MainActivity extends Activity implements OnClickListener { 

     private EditText et_path;

     private Button bt_play, bt_replay, bt_stop;

     private static MediaPlayer mediaPlayer;

     public static MediaPlayer getMediaPlayer() {

      if (mediaPlayer == null) {

       synchronized (MainActivity.class) {

        if (mediaPlayer == null) {

         mediaPlayer = new MediaPlayer();

        }

       }

      }

      return mediaPlayer;

     }

     @Override

     protected void onCreate(Bundle savedInstanceState) {

      super.onCreate(savedInstanceState);

      setContentView(R.layout.activity_main);

      this.et_path = (EditText) this.findViewById(R.id.et_path);

      this.bt_play = (Button) this.findViewById(R.id.bt_play);

      this.bt_replay = (Button) this.findViewById(R.id.bt_replay);

      this.bt_stop = (Button) this.findViewById(R.id.bt_stop);

      this.bt_play.setOnClickListener(this);

      this.bt_replay.setOnClickListener(this);

      this.bt_stop.setOnClickListener(this);

      if(mediaPlayer != null && mediaPlayer.isPlaying()) {

       this.bt_play.setText("暂停");

      } else if(mediaPlayer != null ) {

       this.bt_play.setText("播放");

       

      }

     }

     @Override

     public void onClick(View v) {

      switch (v.getId()) {

      case R.id.bt_play:

       play();

       break;

      case R.id.bt_replay:

       replay();

       break;

      case R.id.bt_stop:

       stop();

       break;

      }

     }

     /**

      * 播放或暂停音乐

      */

     private void play() {

      // 播放状态

      if (mediaPlayer != null && mediaPlayer.isPlaying()) {

       mediaPlayer.pause();

       this.bt_play.setText("播放");

      } else if (mediaPlayer != null) {

       mediaPlayer.start();

       this.bt_play.setText("暂停");

      } else {

       String path = this.et_path.getText().toString().trim();

       File file = new File(path);

       if (file.exists()) {

        try {

         mediaPlayer = getMediaPlayer();

         mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);

         mediaPlayer.setDataSource(path);

         mediaPlayer.prepare();

         mediaPlayer.start();

         mediaPlayer.setOnCompletionListener(new OnCompletionListener() {

         

          @Override

          public void onCompletion(MediaPlayer arg0) {

           Toast.makeText(getApplication(), "播放完成", 0).show();

           stop();

          }

         });

         this.bt_play.setText("暂停");

        } catch (Exception e) {

         e.printStackTrace();

         Toast.makeText(this, "播放错误", 0).show();

        }

       } else {

        Toast.makeText(this, "音频文件不存在", 0).show();

       }

      }

     }

     private void replay() {

      if(mediaPlayer != null) {

       mediaPlayer.seekTo(0);

       this.bt_play.setText("暂停");

       return;

      }

      play();

     }

     private void stop() {

      if(mediaPlayer != null) {

       mediaPlayer.stop();

       mediaPlayer.release();

       mediaPlayer = null;

       this.bt_play.setText("播放");

      }

     }

    }

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

        xmlns:tools="http://schemas.android.com/tools"

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="vertical"

        tools:context=".MainActivity" >

        <EditText

            android:id="@+id/et_path"

            android:text="/storage/sdcard0/MIUI/music/mp3/大地_Beyond.mp3"

            android:layout_width="match_parent"

            android:layout_height="wrap_content" />

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal" >

            <Button

                android:id="@+id/bt_play"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_weight="1"

                android:text="播放" />

            <Button

                android:id="@+id/bt_replay"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_weight="1"

                android:text="重播" />

            <Button

                android:id="@+id/bt_stop"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:layout_weight="1"

                android:text="停止" />

        </LinearLayout>

    </LinearLayout>

  • 相关阅读:
    饿了么P7级前端工程师进入大厂的面试经验
    前端程序员面试的坑,简历写上这一条信息会被虐死!
    这次来分享前端的九条bug吧
    移动端开发必会出现的问题和解决方案
    创建一个dynamics 365 CRM online plugin (八)
    创建一个dynamics 365 CRM online plugin (七)
    创建一个dynamics 365 CRM online plugin (六)
    创建一个dynamics 365 CRM online plugin (五)
    使用User Primary Email作为GUID的问题
    怎样Debug Dynamics 365 CRM Plugin
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469815.html
Copyright © 2011-2022 走看看