2013-11-05 47 views
0

當按下開始按鈕時,音頻服務開始正常,但按下停止按鈕時音頻服務不會停止。這裏是java的活動。我遵循一個在線示例來創建播放音頻的服務。音頻服務未停止android

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.os.Handler; 
import android.util.Log; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 

public class Ship extends Activity implements View.OnClickListener { 
private static final String TAG = "ShipService"; 
public static final Integer[] TIME_IN_MINUTES = { 30, 45, 60, 180, 360 }; 
public MediaPlayer mediaPlayer; 
public Handler handler = new Handler(); 
public Button button2; 
public Spinner spinner2; 

// Initialize the activity 
@Override 
public void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    // wl.acquire(); 
    setContentView(R.layout.ship); 
    button2 = (Button) findViewById(R.id.btn2); 
    button2.setOnClickListener(this); 
    spinner2 = (Spinner) findViewById(R.id.spinner2); 
    ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, 
      android.R.layout.simple_spinner_item, TIME_IN_MINUTES); 

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner2.setAdapter(adapter); 

} 

// Handle button callback 
@Override 
public void onClick(View v) { 
    switch (v.getId()) { 
    case R.id.btn2: 
     Log.d(TAG, "onClick: starting service"); 
     startService(new Intent(this, Shipservice.class)); 
     break; 
    case R.id.stop: 
     Log.d(TAG, "onClick: stopping srvice"); 
     stopService(new Intent(this, Shipservice.class)); 
     break; 
    } 
} 
} 
+0

是您的按鈕按下得到的方法? –

+0

是的,我認爲這裏的一切都好。只需下載此郵政編碼即可享受 - https://github.com/manishsri01/ServiceDemo –

回答

0

您沒有設置停止按鈕的點擊監聽onCreate()方法。

創建停止按鈕的對象

public Button button2,stopBtn;

更新您的onCreate()像下面

button2 = (Button) findViewById(R.id.btn2); 
button2.setOnClickListener(this); 

//停止按鈕

stopBtn= (Button) findViewById(R.id.stop); 
stopBtn.setOnClickListener(this); 
+0

我沒有宣佈我的停止按鈕,謝謝!此外,如果你在我的上面的代碼中看到,我已經設置了微調器以允許選擇一個時間。我如何設置音頻播放這段時間? – user2869238

0

請參閱此代碼 -

public class ServicesDemo extends Activity implements OnClickListener { 
private static final String TAG = "ServicesDemo"; 
Button buttonStart, buttonStop,buttonNext; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    buttonStart = (Button) findViewById(R.id.buttonStart); 
    buttonStop = (Button) findViewById(R.id.buttonStop); 
    buttonNext = (Button) findViewById(R.id.buttonNext); 

    buttonStart.setOnClickListener(this); 
    buttonStop.setOnClickListener(this); 
    buttonNext.setOnClickListener(this); 
} 

public void onClick(View src) { 
    switch (src.getId()) { 
    case R.id.buttonStart: 
    Log.d(TAG, "onClick: starting srvice"); 
    startService(new Intent(this, MyService.class)); 
    break; 
    case R.id.buttonStop: 
    Log.d(TAG, "onClick: stopping srvice"); 
    stopService(new Intent(this, MyService.class)); 
    break; 
    case R.id.buttonNext: 
    Log.d(TAG, "onClick: next Page"); 
    Intent intent=new Intent(this,NextPage.class); 
    startActivity(intent); 
    break; 
    } 
} 
} 

瞭解更多:http://www.androidhub4you.com/2013/03/how-to-create-services-in-android.html#ixzz2jk9fU7AJ

+0

該代碼是我在發佈之前複製的內容。這是我用作示例的文件。 – user2869238