如何通过ondestroy方法停止播放声音?

时间:2013-06-26 10:46:42

标签: android eclipse android-mediaplayer

我有新的代码可以在移动设备中点击静音按钮时停止播放声音,或点击“从应用程序输出”按钮。我在下面做了我的代码,但声音仍在播放,虽然我没有任何错误。

public class x extends Activity {

private MediaPlayer mp;
private String TAG;
Context mContext;
private IntentListener listener = new IntentListener();
WebAppInterface wb= new WebAppInterface(this);

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_ramadan);

    //Call HTML Files
    WebView myWebView = (WebView) findViewById(R.id.web_engine);

    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("file:///android_asset/index.html");

    // Intiate interface

    myWebView.addJavascriptInterface(new WebAppInterface(this), "Android");
    }

@Override
protected void onStart() {
    super.onStart(); 
}

@Override 
protected void onResume() {

    IntentFilter intentFilter = new IntentFilter();
    intentFilter.addAction(Intent.ACTION_SCREEN_OFF);

    registerReceiver(listener, intentFilter);

    super.onResume();
}
@Override
protected void onStop() {
    if (wb != null) {
        wb.stop();

    }
    super.onStop();
}


@Override
protected void onPause() {

    super.onPause();
    wb.pause();
    unregisterReceiver(listener);
}

@Override

public void onDestroy(){
    IntentFilter intentFilter = new IntentFilter(Intent.ACTION_SCREEN_OFF);
    if (intentFilter !=null){
    Log.v("SERVICE","Service killed");
    wb.stop();
}
    super.onDestroy();  
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{ 
    if ((keyCode == KeyEvent.KEYCODE_BACK)) 
{ 
   mp= MediaPlayer.create(Ramadan.this,R.raw.sound); 

if(mp.isPlaying()) 
{
    mp.stop();
    return true; 
}

} 
else return false;


}
}

WebAppInterface类

public class WebAppInterface    {
     Context mContext;
 private MediaPlayer mp;

WebAppInterface(Context c) {
    mContext = c;
}
public void pause( ) {

    mp= MediaPlayer.create(mContext,R.raw.sound);
    if(mp!=null)
    {
          mp.pause();}
    }

public void stop( ) {
    mp= MediaPlayer.create(mContext,R.raw.sound);
    if(mp!=null)
    {
        mp.stop();
          mp.release();
    }
        }

  @JavascriptInterface
public void playsound(String value ) {
    if (value.equals("on")) {
        mp= MediaPlayer.create(mContext,R.raw.sound);
  mp.start();
    }
    else 
    {  mp.stop();}
}

意图监听器类

public class IntentListener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
      //here code do be executed on when intent comes
    }
}

清单

 <activity
        android:name="com.x.y"
        android:label="@string/app_name">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <action android:name="android.intent.action.ACTION_SCREEN_OFF"/>
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

2 个答案:

答案 0 :(得分:1)

只需添加onDestroy()代码,如下所示:

 @Override
 public void onDestroy() {
    super.onDestroy();
    mp= MediaPlayer.create(x.this,R.raw.sound);
    if(mp.isPlaying())
    {
      mp.stop();
      mp.release();
    }
   }

答案 1 :(得分:0)

我不明白为什么要在stop()pause()方法上创建新的MediaPlayer。正如@WarrenFaith所说,你只停止或暂停一个新创建的MediaPlayer,这样播放器(你的mp属性)仍然在后台播放。

在我看来,您应该尝试删除那些Mediaplayer.create来电