2012-08-31 51 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); 

}

代碼右邊有可能的問題。您正試圖播放一個空的MediaPlayer。請編輯此行

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

看起來像這樣

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

你擁有了它,你正在創建範圍較小的第二媒體播放器soundplay的方式,當函數結束即去世,留給私營媒體播放器您之前設置爲空。

也是我想你忘記了在新的MediaPlayer對象

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

這是基本的模式,我跟着投。

+0

謝謝你的回答,但它仍然無法正常工作。 –

+0

你可以給我們調試日誌嗎? – WIllJBD

+0

也看到更新的答案 – WIllJBD