메뉴 건너뛰기

조회 수 1803 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄 첨부

1. 레이아웃 설정 (activity_main.xml)

 간단하게 재생, 중지, 일시중지의 3가지 버튼을 삽입합니다.

 

2. 레이아웃에 따른 Java 클래스 작성 (MainActivity.java)

 3개의 버튼을 Java 클래스의 Button 형 인스턴스에 연결하고 클릭할 때 곧이어 정의 할 updateActivity 함수가 호출되도록 합니다.

 Java 클래스에는 재생 상태와 관련된 4가지 상수를 정의하고 있습니다.

  • PLAYER_INIT: 앱이 실행되고 재생 관련 함수에서는 아직 아무것도 수행하지 않은 상태입니다.
  • PLAYER_STOP: 소리 재생이 중지된 상태입니다.
  • PLAYER_PLAY: 소리가 재생중인 상태입니다.
  • PLAYER_PAUSE: 소리 재생이 일시중지된 상태입니다.

package com.example.audiotest; 
import java.io.*; 
import android.app.*; 
import android.content.res.*; 
import android.media.*; 
import android.os.*; 
import android.util.*; 
import android.view.*; 
import android.widget.*; 
public class MainActivity extends Activity {private static final int PLAYER_INIT = 0; // 처음 상태 
private static final int PLAYER_STOP = 1; // 중지 상태 
private static final int PLAYER_PLAY = 2; // 재생 상태 
private static final int PLAYER_PAUSE = 3; // 일시정지 상태 
private MediaPlayer mediaPlayer = null; // 미디어 재생 클래스 
private int mediaPlayerStatus = PLAYER_INIT; // 액티비티의 처음 상태 
private Button buttonPlay = null; // 재생 버튼 
private Button buttonStop = null; // 정지 버튼 
private Button buttonPause = null; // 일시정지 버튼 
@Overrideprotected void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
this.buttonPlay = (Button)this.findViewById(com.example.audiotest.R.id.buttonPlay);
this.buttonStop = (Button)this.findViewById(com.example.audiotest.R.id.buttonStop);
this.buttonPause = (Button)this.findViewById(com.example.audiotest.R.id.buttonPause);

this.buttonPlay.setOnClickListener(
new View.OnClickListener(){@Overridepublic void onClick(View v){// TODO Auto-generated method stubMainActivity.this.updateActivity(PLAYER_PLAY);}});

this.buttonStop.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){// TODO Auto-generated method stubMainActivity.this.updateActivity(PLAYER_STOP);}});

this.buttonPause.setOnClickListener(new View.OnClickListener(){@Overridepublic void onClick(View v){// TODO Auto-generated method stubMainActivity.this.updateActivity(PLAYER_PAUSE);}});}// ... 생략 ... }

재생, 중지, 일시정지 함수

 updateActivity 함수는 액티비티의 재생 상태를 변경하는 함수입니다.

 매개변수인 mediaPlayerStatus는 버튼을 누름으로써 새로 설정될 상태를 의미합니다. 매개변수로 받은 mediaPlayerStatus와 액티비티의 현재 재생 상태인 this.mediaPlayerStatus를 비교하여 재생 관련 작동을 제어할 것입니다.



// this.mediaPlayerStatus = 액티비티의 현재 재생 상태 private void updateActivity(int mediaPlayerStatus) {if (this.mediaPlayer == null){AssetManager assetManager = null;AssetFileDescriptor assetFileDescriptor = null;try{// asset에 있는 wav 파일을 재생하도록 설정 this.mediaPlayer = new MediaPlayer();assetManager = this.getAssets();assetFileDescriptor = assetManager.openFd("test.wav");this.mediaPlayer.setDataSource(assetFileDescriptor.getFileDescriptor(), assetFileDescriptor.getStartOffset(), assetFileDescriptor.getLength());assetFileDescriptor.close();this.mediaPlayer.prepare();}catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}}// 새로 설정할 재생 상태 switch (mediaPlayerStatus){case PLAYER_PLAY:// 액티비티의 현재 상태와 새로 설정할 상태를 비교 switch (this.mediaPlayerStatus){case PLAYER_PLAY: // 이미 재생중인데 또 재생을 누른 경우 아무 작업 안 함 break;case PLAYER_STOP: // 정지였다가 재생을 누른 경우 처음부터 재생 (MediaPlayer 생명주기 참조) try{this.mediaPlayer.prepare();this.mediaPlayer.seekTo(0);}catch (IllegalStateException e){// TODO Auto-generated catch blocke.printStackTrace();}catch (IOException e){// TODO Auto-generated catch blocke.printStackTrace();}this.mediaPlayer.start();break;case PLAYER_PAUSE: // 일시정지였는데 재생을 누른 경우 이어서 재생 this.mediaPlayer.start();break;case PLAYER_INIT: // 맨 처음상태에서 재생을 누른 경우 마찬가지로 재생 this.mediaPlayer.start();break;}break;case PLAYER_STOP:switch (this.mediaPlayerStatus){case PLAYER_PLAY: // 재생 중이었는데 정지를 누른 경우 this.mediaPlayer.stop();break;case PLAYER_STOP: // 정지 상태에서 또 정지 누른 경우 break;case PLAYER_PAUSE: // 일시정지 상태에서 정지 누른 경우 this.mediaPlayer.stop();break;case PLAYER_INIT: // 맨 처음 상태에서 정지를 누른 경우 break;}break;case PLAYER_PAUSE:switch (this.mediaPlayerStatus){case PLAYER_PLAY: // 재생 중이었는데 일시정지를 누른 경우 this.mediaPlayer.pause();break;case PLAYER_STOP: // 정지였는데 일시정지를 누른 경우 break;case PLAYER_PAUSE: // 일시정지인데 또 일시정지를 누른 경우 break;case PLAYER_INIT: // 맨 처음상태에서 일시정지를 누른 경우 break;}break;}this.mediaPlayerStatus = mediaPlayerStatus; // 이 액티비티의 재생 상태를 새로 받은 상태로 변경 }


MediaPlayer의 생명주기


List of Articles
번호 제목 날짜 조회 수
97 하이브리드 앱에서의 세션관리(로그인 상태 유지) 2018.12.27 5001
96 WebView를 사용할때 HttpClient를 이용한 Session 유지 2018.12.27 4380
95 안드로이드 - 에디트텍스트(EditText) 사용법 정리 file 2021.03.29 2755
94 WebView에서 카메라 및 이미지 업로드 (선택적용가능) file 2020.12.14 2692
93 구글맵으로 GPS 현재위치 실시간 연동하기 file 2020.12.14 2445
92 위젯 업데이트 주기 빠르게 하기 2018.10.02 2142
91 안드로이드 - 네비게이션 드로어(Navigation Drawer)를 활용하여 슬라이드 메뉴 구현하기 file 2021.04.01 1858
» MediaPlayer 클래스 사용법 file 2018.10.02 1803
89 Android Studio에서 SQLCipher 라이브러리 추가 방법 file 2018.10.02 1776
88 안드로이드 - 날짜 및 시간 정보 입력받기 (DatePickerDialog / TimePickerDialog) file 2021.04.01 1764
87 안드로이드 WebView 에서 tel: 이 되지않는 경우. 2018.10.02 1633
86 Apk manager 이용해 Decompile (디컴파일) 하기 file 2021.03.16 1620
85 안드로이드 - BottomNavigationView 사용하여 하단 메뉴 만들기 file 2021.04.02 1439
84 Volley 로 웹요청하고 응답받기2 - Post방식 , 로그인-회원가입 (php,mysql 연동) file 2020.12.14 1431
83 안드로이드 - 타이머(Timer) 구현하기 2021.04.01 1368
82 [하이브리드앱] userAgent를 이용해서 웹 / 앱 접속 구분하기 2021.09.30 1282
81 안드로이드 - switch를 사용법 및 구현 file 2021.04.02 1280
80 안드로이드 - 텍스트뷰(TextView) 사용법 정리 file 2021.03.31 1247
79 안드로이드 - KeyEvent(키 이벤트) 처리 file 2021.04.02 1211
78 WebView 작업할때 Net::ERR_UNKNOWN_URL_SCHEME 에러 발생할때 (전화걸기,문자보내기 안된다) 2020.12.14 1067
Board Pagination Prev 1 ... 4 5 6 7 8 9 10 11 12 13 Next
/ 13

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved