2012-08-31 2 views
1

mediaplayer 및 viewpager를 사용하여 사운드 파일을 재생하려고하지만 작동하지 않습니다. 오류 메시지가 표시됩니다.사용자 이벤트의 사운드 파일 재생

"Unfortunately, the process horizontalviewpaging has stopped" 

(페이지 스 와이 핑 및 클릭 이벤트) 어떻게이 문제를 해결할 수 있습니까?

package ro.ovidiuconeac.horizontalviewswiping; 

import ro.ovidiuconeac.horizontalviewswiping.R; 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class CustomPageChangeListener extends Activity implements OnPageChangeListener, OnClickListener { 

    private MediaPlayer soundplay = null; 

    @SuppressWarnings("unused") 
    public void onCreate(Bundle savedInstanceState) { 

    // load layout 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page_1); 
    MediaPlayer soundplay=MediaPlayer.create(this, R.raw.catsound); 
    } 

    private Button buttonBlue = null; 
    private Button buttonYellow = null; 
    private Button buttonRed = null; 
    private Activity context = null; 

    /** 
    * Constructor 
    */ 
    public CustomPageChangeListener(Activity context) { 
    this.context = context; 

    } 

    public void onPageScrollStateChanged(int arg0) { 
    // TODO Auto-generated method stub 

    } 

    public void onPageScrolled(int arg0, float arg1, int arg2) { 
    // TODO Auto-generated method stub 

    } 

    public void onPageSelected(int position) { 

    switch (position) { 
     case 0 : { 
     ButtonBlue = (Button) context.findViewById(R.id.buttonBlue); 
     buttonBlue.setOnClickListener(this); 
     soundplay.start(); 
     break; 
     } 
     case 1 : { 
     buttonYellow = (Button) context.findViewById(R.id.buttonYellow); 
     buttonYellow.setOnClickListener(this); 
     soundplay.start(); 
     break; 
     } 
     case 2 : { 
     buttonRed = (Button) context.findViewById(R.id.buttonRed); 
     buttonRed.setOnClickListener(this); 
     soundplay.start(); 
     break; 
     } 
    } 
    } 

    public void onClick(View v) { 

    // Click button blue 
    if (buttonBlue != null && v.getId() == buttonBlue.getId()) { 
     Toast.makeText(context, "Blue screen", Toast.LENGTH_SHORT).show(); 
     soundplay.start();  
    } 

    // Click button yellow 
    if (buttonYellow != null && v.getId() == buttonYellow.getId()) { 
     Toast.makeText(context, "Yellow screen", Toast.LENGTH_SHORT).show(); 
     soundplay.start();   
    } 

    // Click button red 
    if (buttonRed != null && v.getId() == buttonRed.getId()) { 
     Toast.makeText(context, "Red screen", Toast.LENGTH_SHORT).show(); 
     soundplay.start();  
    } 
    } 
} 

이 질문에 대한 추가 정보 인 '답변'에서 편집되었습니다.

> 09-01 08:38:04.964: E/AndroidRuntime(631): FATAL EXCEPTION: main 
> 09-01 08:38:04.964: E/AndroidRuntime(631): java.lang.NullPointerException 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at ro.ovidiuconeac.horizontalviewswiping.CustomPageChangeListener.onClick(CustomPageChangeListener.java:80) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.view.View.performClick(View.java:4084) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.view.View$PerformClick.run(View.java:16966) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.os.Handler.handleCallback(Handler.java:615) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.os.Handler.dispatchMessage(Handler.java:92) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.os.Looper.loop(Looper.java:137) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at android.app.ActivityThread.main(ActivityThread.java:4745) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at java.lang.reflect.Method.invokeNative(Native Method) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at java.lang.reflect.Method.invoke(Method.java:511) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
> 09-01 08:38:04.964: E/AndroidRuntime(631): at dalvik.system.NativeStart.main(Native Method) 

코드가 변경되었지만 작동하지 않습니다.

package ro.ovidiuconeac.horizontalviewswiping; 
import ro.ovidiuconeac.horizontalviewswiping.R; 
import android.app.Activity; 
import android.media.MediaPlayer; 
import android.support.v4.view.ViewPager.OnPageChangeListener; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.Toast; 

public class CustomPageChangeListener extends Activity implements OnPageChangeListener,  OnClickListener { 

    MediaPlayer soundplay; 
private Button buttonBlue = null; 
private Button buttonYellow = null; 
private Button buttonRed = null; 
private Activity context = null; 

/** 
* Constructor 
*/ 

public CustomPageChangeListener(Activity context) { 
    this.context = context; 

} 

public void onPageScrollStateChanged(int arg0) { 
    // TODO Auto-generated method stub 

} 

public void onPageScrolled(int arg0, float arg1, int arg2) { 
    // TODO Auto-generated method stub 

} 

public void onPageSelected(int position) { 

    switch (position) { 
     case 0 : { 
      buttonBlue = (Button) context.findViewById(R.id.buttonBlue); 
      buttonBlue.setOnClickListener(this); 

      break; 
     } 
     case 1 : { 
      buttonYellow = (Button) context.findViewById(R.id.buttonYellow); 
      buttonYellow.setOnClickListener(this); 
      break; 
     } 
     case 2 : { 
      buttonRed = (Button) context.findViewById(R.id.buttonRed); 
      buttonRed.setOnClickListener(this); 

      break; 
     } 
    } 
} 


public void onClick(View v) { 

    // Click button blue 
    if (buttonBlue != null && v.getId() == buttonBlue.getId()) { 
     Toast.makeText(context, "Blue screen", Toast.LENGTH_SHORT).show(); 

    } 

    // Click button yellow 
    if (buttonYellow != null && v.getId() == buttonYellow.getId()) { 

     // Toast.makeText(context, "Yellow screen", Toast.LENGTH_SHORT).show(); 
     playMusic(); 
    } 

    // Click button red 
    if (buttonRed != null && v.getId() == buttonRed.getId()) { 
     Toast.makeText(context, "Red screen", Toast.LENGTH_SHORT).show(); 

    } 
} 


public void playMusic() { 
    soundplay = new MediaPlayer(); 
     try { 
     soundplay = MediaPlayer.create(getApplicationContext(), R.raw.bangyeni); 
     soundplay.setVolume(1, 1); 
     soundplay.start(); 

} 
catch (Exception e){ 
    Toast.makeText(context, "Error", Toast.LENGTH_SHORT).show(); 
} 
} 

}

+0

게시물 관련 LogCat 출력 –

+0

LogCat 오류 로그를 공유하면 더 잘 도와 드릴 수 있습니다. – JRaymond

+0

잊어 버렸을 때 대답을 수락 해주십시오. – WIllJBD

답변

1
private MediaPlayer soundplay = null; 
@SuppressWarnings("unused") 
public void onCreate(Bundle savedInstanceState) { 
// load layout 

super.onCreate(savedInstanceState); 
setContentView(R.layout.page_1); 
MediaPlayer soundplay=MediaPlayer.create(this, R.raw.catsound); 

}

그 코드는 바로 가능성 문제가있다. null MediaPlayer를 재생하려고합니다. 민간 MediaPlayer를 떠나,

 MediaPlayer soundplay=MediaPlayer.create(this, R.raw.catsound); 

는 함수가 종료 될 때 죽으면 당신이 당신이 작은 범위와 두 번째 MediaPlayer를의 soundplay를 만드는이이

soundplay=MediaPlayer.create(this, R.raw.catsound); 

방법, 모양이 행을 편집하십시오 당신은 더 일찍 null로 설정합니다.

도 난 당신이 내가 따라 기본 모델 인 새로운 MediaPlayer를 객체

 mp = new MediaPlayer(); 
    mp = MediaPlayer.create(getApplicationContext(), R.raw.sound); 
    mp.setVolume(1, 1); 
    mp.start(); 

에 캐스팅 잊고 생각합니다.

+0

답변 해 주셔서 감사하지만 여전히 작동하지 않습니다. –

+0

디버그 로그를 보내 주시겠습니까? – WIllJBD

+0

업데이트 된 답변보기 – WIllJBD