关于MediaPlayer无法停止
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
//设置按钮(Button)监听
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
}
//开始按钮
private OnClickListener start = new OnClickListener()
{
public void onClick(View v)
{
//开启Service
startService(new Intent("com.yarin.Android.MUSIC"));
}
};
//停止按钮
private OnClickListener stop = new OnClickListener()
{
public void onClick(View v)
{
//停止Service
stopService(new Intent("com.yarin.Android.MUSIC"));
}
};
}
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
public class MusicService extends Service{
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
player = MediaPlayer.create(this, R.raw.test);
player.start();
}
public void onDestory(){
Log.d("aaa", "bbb");
super.onDestroy();
player.stop();
}
}
主要代码就是这样,我是用的2.2,就是通过服务来启动一个音频文件的播放/停止,现在是播放可以,但是停止不了,点了“停止”按钮没反应,不知为何啊,我打了Log发现也没进onDestory方法
[最优解释]
其实我2了,又想得太多。
只要把onDestroy()拼对就解决问题了。。。。。。。。。。。。。。。。
[其他解释]
startService(new Intent("com.yarin.Android.MUSIC"));
改为startService(new Intent(this,MusicService.class));
stopService(new Intent("com.yarin.Android.MUSIC"));
改为 stopService(new Intent(this,MusicService.class));
[其他解释]
ComponentName component;
component = new ComponentName(this, MusicService.class);
Intent mIntent;
mIntent = new Intent();
mIntent.setAction("com.yarin.Android.MUSIC_STOP");
mIntent.setComponent(component);
startService(mIntent);
[其他解释]
你的AndroidManifest.xml中有没有注册Service啊!要在里面声明哦,并且你的action要是com.yarin.Android.MUSIC(action=com.yarin.Android.MUSIC)
[其他解释]
我有声明的啊,看AndroidManifest.xml为:
/>
<Button
android:id="@+id/start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="开始播放"/>
<Button
android:id="@+id/stop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="停止播放"
/>
</LinearLayout>
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">使用Service播放音乐例子,谢谢使用!</string>
<string name="app_name">Examples_03_03</string>
</resources>
再就是raw中放一个音频文件,就这些文件了
[其他解释]
来个高手看一下啊,程序都贴出来了
[其他解释]
Note that if a stopped service still has ServiceConnection objects bound to it with the BIND_AUTO_CREATE set, it will not be destroyed until all of these bindings are removed. See the Service documentation for more details on a service's lifecycle.
上面是SDK的内容,如果要用stopService(..)方法,必须通过bindService方法而不是startService!
还有重写系统的方法最好加上@Override,还有你的onDestory拼写错鸟。。。
下面是我改的代码,运行通过
先把<uses-sdk android:minSdkVersion="5" />中的5改成8,与2.2对应。
package com.gejing.Chapter3_2;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity01 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//从main.xml布局中获得Button对象
Button button_start = (Button)findViewById(R.id.start);
Button button_stop = (Button)findViewById(R.id.stop);
//设置按钮(Button)监听
button_start.setOnClickListener(start);
button_stop.setOnClickListener(stop);
}
private ServiceConnection mConnection = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
}
public void onServiceDisconnected(ComponentName className) {
}
};
protected void doBindService() {
bindService(new Intent(this, MusicService.class), mConnection,
Context.BIND_AUTO_CREATE);
}
//开始按钮
private OnClickListener start = new OnClickListener()
{
public void onClick(View v)
{
//开启Service
doBindService();
}
};
//停止按钮
private OnClickListener stop = new OnClickListener()
{
public void onClick(View v)
{
//停止Service
unbindService(mConnection);
}
};
}
package com.gejing.Chapter3_2;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.util.Log;
public class MusicService extends Service{
private MediaPlayer player;
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public void onCreate(){
player = MediaPlayer.create(this, R.raw.test);
player.start();
}
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
}
@Override
public void onDestroy(){
Log.d("aaa", "bbb");
super.onDestroy();
player.stop();
}
}
[其他解释]
我也遇到了同样的service不能停止的问题。
原因是onDestroy写成了onDestory。注意,应该是onDestroy!!
