Android MediaPlayer 오류 해결 MediaPlayer start called in state 0

3062 단어 자바Android
이 앱 의 주요 기능 은 재생 과 녹음 이다.
그러나 타이머 타 이 머 를 사용 하여 서브 스 레 드 에서 5 초 씩 총 33 회 녹음 할 수 있 습 니 다.
플레이 어 는 미디어 플레이어 대상 입 니 다.
재생 을 시작 할 때 player.start()를 사용 합 니 다.
재생 종료 시 player.stop()사용
그리고 계속 끝나 기 시 작 했 어 요.
하지만 두 번 째 때 는 미디어 플레이어 start called in state 0 이 잘못 보 고 됩 니 다.
그리고 이 문 제 를 찾 아 보 니 플레이 어 를 처음 사용 하 는 대상 이 라면 플레이 어 를 직접 사용 할 수 있다 는 것 이다.start().
하지만 재 활용 이 라면 두 번 째 시작 은 player.start()앞 이나 player.stop()뒤에 player.prepare()를 추가 해 야 합 니 다.
하지만 이 코드 를 처음 사용 할 때 player.start()를 처음 사용 하면 player.start()앞 에 player.prepare()를 추가 하면 이상 이 생 길 수 있 습 니 다.
따라서 처음 사용 여 부 를 판단 하지 않 기 위해 서 는 player.start()를 여러 번 사용 할 때 player.stop()뒤에 player.prepare()방법 을 추가 하 는 것 을 권장 합 니 다.추가 할 때 이상 이 발생 할 수 있 으 므 로 이상 을 처리 해 야 합 니 다.다음은 제 전체 Timer Task 코드 입 니 다.
class myTaskStart extends TimerTask{
        @Override
    public void run(){
            //    
                System.out.println("    ");

                //count       ,        run     
                if(count<33){
                    count=count+1;
                }
                else {
                    this.cancel();
                    System.exit(0);
                }
                //    count  run     


            //    TextView      
            textView.post(new Runnable() {
                @Override
                public void run() {
                    textView.setText("start"+count);
                }
            });
            //    TextView      

                player.start();
                String fileName = new SimpleDateFormat("yyyyMMddhhmmss").format(new Date());
                audioRecorder.createDefaultAudio(fileName);    // status = Status.STATUS_READY;
                audioRecorder.startRecord(null);

              //    
            try {
                Thread.sleep(5000);
            }
            catch (InterruptedException e)
            {
                Toast.makeText(getApplicationContext(),"  ",Toast.LENGTH_LONG).show();
            }

            //    
            System.out.println("        ");
            audioRecorder.stopRecord();
            player.stop();

            textView.post(new Runnable() {
                @Override
                public void run() {
                    textView.setText("end");
                }
            });

            // player.stop()    player.prepare(),      !
            try {
                player.prepare();
            }
            catch (IOException e)
            {
                System.out.println("player     !");
            }
            //    player.prepare

        }
}

주요 코드 는 다음 과 같 습 니 다.
            player.stop();

            // player.stop()    player.prepare(),      !
            try {
                player.prepare();
            }
            catch (IOException e)
            {
                System.out.println("player     !");
            }
            //    player.prepare

이렇게 하면 정상적으로 운행 할 수 있다.
 

좋은 웹페이지 즐겨찾기