- Android
- 2011-12-13
この記事は最終更新日から1年以上経過しています。
MediaPlayer mediaPlayer = new MediaPlayer(); String path = ""; mediaPlayer.setLooping(true); //ループ再生の設定 try { mediaPlayer.setDataSource(path); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { mediaPlayer.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } mediaPlayer.start(); //再生開始 mediaPlayer.pause(); //一時停止 mediaPlayer.stop(); //停止
コントローラーを配置し実装する(未検証)
import android.os.Bundle; import android.app.Activity; import java.io.IOException; import android.media.MediaPlayer; import android.view.MotionEvent; import android.widget.MediaController; public class Mp3Activity extends Activity implements MediaController.MediaPlayerControl { private MediaPlayer mediaPlayer; private MediaController mediaController; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); String path = "再生するファイル"; mediaPlayer = new MediaPlayer(); mediaPlayer.setLooping(true); mediaController = new MediaController(this); mediaController.setAnchorView(findViewById(R.id.root)); mediaController.setMediaPlayer(this); mediaController.setEnabled(true); try { mediaPlayer.setDataSource(path); } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } try { mediaPlayer.prepare(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } //mediaPlayer.start(); //再生 //mediaPlayer.pause(); //一時停止 //mediaPlayer.stop(); //停止 } @Override public void onResume() { super.onResume(); } @Override public void onStop() { super.onStop(); if (mediaPlayer != null) { mediaController.hide(); mediaPlayer.stop(); mediaPlayer.release(); } } @Override public boolean onTouchEvent(MotionEvent event) { mediaController.show(); return super.onTouchEvent(event); } @Override public int getBufferPercentage() { return 0; } @Override public int getCurrentPosition() { return mediaPlayer.getCurrentPosition(); } @Override public int getDuration() { return mediaPlayer.getDuration(); } @Override public boolean isPlaying() { return mediaPlayer.isPlaying(); } @Override public void pause() { mediaPlayer.pause(); } @Override public void seekTo(int pos) { mediaPlayer.seekTo(pos); } @Override public void start() { mediaPlayer.start(); } }
6,267 views