마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

main activity에서 sound 부분만 빼서 따로 클래스를 정의 했는데 오류가 생깁니다.

0 추천
public class SoundControl extends Activity{

 public static final int BASE = 1;
 public static final int ACTION = 2;
 public static final int GEAR = 3;

 private static final int BASE_SOUND = 10;
 SoundPool base_sound = null;
 SoundPool action_sound = null;
 SoundPool gear_sound = null;

 private AudioManager audio;

 int baseId;
 int actionId;
 int gearId;

 public void soundCreate( int id) {
  
  Context context = new SoundRacerActivity();

  switch (id) {
  case BASE:
   base_sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
   baseId = base_sound.load(context, R.raw.porsche_911_gt3_rs_basics2,
     1);
   audio.setStreamVolume(AudioManager.STREAM_MUSIC, BASE_SOUND, 0);
   break;
  case ACTION:
   action_sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
   actionId = action_sound.load(context,
     R.raw.porsche_911_gt3_rs_exhaust1, 1);
   break;

  case GEAR:
   gear_sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
   gearId = gear_sound.load(context,
     R.raw.porsche_911_gt3_rs_shift_gears, 1);
   break;
  }

 }

 public void play(int id) {
  switch (id) {
  case BASE:
   base_sound.play(baseId, 1.0f, 1.0f, 0, -1, 1.0f);
   break;
   
  case ACTION:
   action_sound.play(actionId, 1.0f, 1.0f, 0, -1, 1.0f);
   break;

  case GEAR:
   gear_sound.play(gearId, 1.0f, 1.0f, 0, -1, 1.0f);
   break;
  }
 }

 public void pause(int id) {
  switch (id) {
  case BASE:
   base_sound.pause(baseId);
   break;
  case ACTION:
   action_sound.pause(actionId);
   break;

  case GEAR:
   gear_sound.pause(gearId);
   break;
  }
 }

 public void release(int id) {

  switch (id) {
  case BASE:
   base_sound.stop(baseId);
   base_sound.release();
   base_sound = null;
   break;
  case ACTION:
   action_sound.stop(actionId);
   action_sound.release();
   action_sound = null;
   break;
  case GEAR:
   gear_sound.stop(gearId);
   gear_sound.release();
   gear_sound = null;
  }
 }

 public void audioInitial() {
  audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  audio.setStreamVolume(AudioManager.STREAM_MUSIC, BASE_SOUND, 0);
 }


 public void audioVolumeControl(int vol) {
  
  audio.setStreamVolume(AudioManager.STREAM_MUSIC, vol, 0);

 }

 public void stop() {
  if (action_sound != null) {
   action_sound.stop(actionId);
   action_sound.release();
   action_sound = null;
  } else if (base_sound != null) {
   base_sound.stop(baseId);
   base_sound.release();
   base_sound = null;
  } else {
   gear_sound.stop(gearId);
   gear_sound.release();
   gear_sound = null;
  }

 }

소스가 워낙 지저분 해서 sound 부분만 따로 빼서 main activity에서 사용하려고 합니다. 그런데 계속 오류가 나는데...이렇게 소스를 짜도 괜찮은가요??

안왕초보 (1,390 포인트) 님이 2014년 3월 5일 질문
저~ 위에 댓글소스 한번 테스트해봤는데
이늠 빼니까 실행 오류는 안나네요.
audio.setStreamVolume(AudioManager.STREAM_MUSIC, BASE_SOUND, 0);

컨텍스트는 클래스 내부에 전역으로 선언하셨으면 참조가 되는게 맞는뎅.. 왜 안되시는거징.. 흠
실행 오류는 오디오 매니저 때문이네요 ㅎㅎ

public class SoundControl {

    public static final int BASE = 1;
    public static final int ACTION = 2;
    public static final int GEAR = 3;

    private static final int BASE_SOUND = 10;
    SoundPool base_sound = null;
    SoundPool action_sound = null;
    SoundPool gear_sound = null;

    private AudioManager audio;

    int baseId;
    int actionId;
    int gearId;
    
    Context c;
    
    public SoundControl (Context context)
     {
        Log.i("생성자 호출", "실행됨" +context);
         c = context;
         audio = (AudioManager) c.getSystemService(Context.AUDIO_SERVICE);
     }

    public void soundCreate(int id) {
        
        switch (id) {
        case BASE:
            Log.i("Base", "실행됨" +id);

            base_sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
            baseId = base_sound.load(c, R.raw.bird, 1);
            
            audio.setStreamVolume(AudioManager.STREAM_MUSIC, BASE_SOUND, 0);
            Log.i("Base", "44444444" +id);
            break;
        case ACTION:
            Log.i("ACTION", "실행됨" +id);
            action_sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
            actionId = action_sound.load(c, R.raw.bird, 1);
            Log.i("ACTION", "종료됨" +id);
            break;

        case GEAR:
            Log.i("GEAR", "실행됨" +id);
            gear_sound = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
            gearId = gear_sound.load(c, R.raw.bird, 1);
            Log.i("GEAR", "종료됨" +id);
            break;
        }

    }
}
오!!! 매번 감사합니다 ㅜㅜ
초보개발자님께서 가르쳐 주신 방법으로 계속 하는데 실행 오류가 생겨서 왜 그러는지 계속 소스 변화 시켰는데
SoundControl soundControl = new SoundControl(getBaseContext());
이부분을 전역 변수로 하면 안되고 main에 onCreate 부분에 넣으지 잘 되네요 ㅎㅎ 감사합니다
축하드려요~!! 매번 고생이 많으시네요 ㅋㅋ

1개의 답변

+1 추천
SoundRacerActivity 가 Activity를 상속받은 클래스라면 잘못쓰고 계신 겁니다.

Activity 클래스 객체는 직접 생성하면 안됩니다.
익명사용자 님이 2014년 3월 5일 답변
activity 상속을 안하려고 하는데 getSystemService 쪽에 계속 오류가 생겨서 어쩔 수 없이 상속을 해버린거에요 ㅜㅜ
getSystemService 오류 때문에 audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
  audio.setStreamVolume(AudioManager.STREAM_MUSIC, BASE_SOUND, 0);
는 main activity 쪽으로 넣어버렸어요
...