• 多媒体播放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>

  • 相关阅读:
    普通链表的各种排序及常用操作
    数据结构、算法与应用(C++描述)(第二版)第六章习题解答
    数据结构、算法与应用(C++描述)(第二版)第三章习题解答
    数据结构、算法与应用(C++描述)(第二版)第二章习题解答
    数据结构、算法与应用(C++描述)(第二版)第一章习题解答
    数据结构、算法与应用(C++描述)(第二版)第五章习题解答
    C++排序算法
    Code-C++-Cut CString to get keyValue by ","||"}"
    C++-Struct string初始化&&map初始化
    JSON的简单介绍以及C语言的JSON库使用
  • 原文地址:https://www.cnblogs.com/freenovo/p/4469815.html
Copyright © 2020-2023  润新知