播放音頻:
在 Android 中播放音頻文件一般都是使用 MediaPlayer 類來實現的,它對多種格式的音頻文件提供了非常全面的控制方法,從而使得播放音樂的工作變得十分簡單。下表列出了MediaPlayer 類中一些較爲常用的控制方法。
播放流程:
1)首先需要創建出一個 MediaPlayer 對象
2)然後調用 setDataSource()方法來設置音頻文件的路徑,
3)再調用 prepare()方法使 MediaPlayer進入到準備狀態,
4)接下來調用 start()方法就可以開始播放音頻, 調用 pause()方法就會暫停播放,調用 reset()方法就會停止播放。
public class MainActivity extends AppCompatActivity implements View.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(); // 初始化MediaPlayer } private void initMediaPlayer() { try { // File file = new File(Environment.getExternalStorageDirectory(), // "music.mp3"); // mediaPlayer.setDataSource(file.getPath()); // 指定音頻文件的路徑 mediaPlayer.setDataSource(MainActivity.this,Uri.parse("http://sc.111ttt.com/up/mp3/73262/46444AE0C73A8C8B4DCE6B2A1CE33302.mp3")); mediaPlayer.prepare(); // 讓MediaPlayer進入到準備狀態 } 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(); } } }
記得添加網絡權限,否則提示no content provider
播放視頻:
播放視頻文件其實並不比播放音頻文件複雜,主要是使用 VideoView 類來實現的。
public class video extends AppCompatActivity implements View.OnClickListener{ private VideoView videoView; private Button play; private Button pause; private Button replay; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.video); play = (Button) findViewById(R.id.play); pause = (Button) findViewById(R.id.pause); replay = (Button) findViewById(R.id.replay); videoView = (VideoView) findViewById(R.id.video_view); play.setOnClickListener(this); pause.setOnClickListener(this); replay.setOnClickListener(this); initVideoPath(); } private void initVideoPath() { File file = new File(Environment.getExternalStorageDirectory(), "xxx.mp4"); videoView.setVideoPath(file.getPath()); // 指定視頻文件的路徑 } @Override public void onClick(View v) { switch (v.getId()) { case R.id.play: if (!videoView.isPlaying()) { videoView.start(); // 開始播放 } break; case R.id.pause: if (videoView.isPlaying()) { videoView.pause(); // 暫時播放 } break; case R.id.replay: if (videoView.isPlaying()) { videoView.resume(); // 重新播放 } break; } } @Override protected void onDestroy() { super.onDestroy(); if (videoView != null) { videoView.suspend(); } } }
藉助於官方的VideoView進行佈局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <VideoView android:id="@+id/video_view" android:layout_width="match_parent" android:layout_height="wrap_content" /> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/play" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Play" /> <Button android:id="@+id/pause" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Pause" /> <Button android:id="@+id/replay" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="Replay" /> </LinearLayout> </LinearLayout>