自己没事写了一个android音乐播放器 后台播放那种蛋疼的是自己感觉代码没错啊,就是出不来。。大家帮我看看 我搞一天了 做不出来我会睡不着的。。
xml文件就是三个按钮 start pause stop
src
MAINpackage com.example.begin;import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {
TextView textview;
Button button1,button2,button3;
String st;
final Intent serviceIntent=new Intent();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.tv);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==R.id.button1){
st="start";
}else
if(v.getId()==R.id.button2){
st="pause";
}
else
if(v.getId()==R.id.button3){
st="stop";
}
Bundle bundle=new Bundle();
bundle.putString("name", st);
serviceIntent.putExtras(bundle);
serviceIntent.setAction("com.example.begin");
startService(serviceIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
Servicepackage com.example.begin;import java.io.IOException;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;public class service extends Service{ private MediaPlayer mp = new MediaPlayer(); @Override
public void onCreate() {
// TODO Auto-generated method stub
MediaPlayer.create(this, R.raw.zqc);
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
mp.release();
super.onDestroy();
} @Override
public void onStart(Intent intent, int startId) {
Bundle bundle = this.getIntent().getExtras();
String name=bundle.getString("name");
if(name.equals("start")){
mp.start();
}
else if(name.equals("pause")){
mp.pause();
}
else if(name.equals("stop")){
mp.stop();
}
}
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
设置的Intent的 action属性
<service android:name=".begin">
<intent-filter >
<action
android:name="com.example.begin.service"/>
</intent-filter>
</service>
求大家给的意见 我快烦死了。Android类
xml文件就是三个按钮 start pause stop
src
MAINpackage com.example.begin;import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends Activity implements OnClickListener {
TextView textview;
Button button1,button2,button3;
String st;
final Intent serviceIntent=new Intent();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.tv);
button1=(Button)findViewById(R.id.button1);
button2=(Button)findViewById(R.id.button2);
button3=(Button)findViewById(R.id.button3);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
}
public void onClick(View v){
if(v.getId()==R.id.button1){
st="start";
}else
if(v.getId()==R.id.button2){
st="pause";
}
else
if(v.getId()==R.id.button3){
st="stop";
}
Bundle bundle=new Bundle();
bundle.putString("name", st);
serviceIntent.putExtras(bundle);
serviceIntent.setAction("com.example.begin");
startService(serviceIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}}
Servicepackage com.example.begin;import java.io.IOException;import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.IBinder;public class service extends Service{ private MediaPlayer mp = new MediaPlayer(); @Override
public void onCreate() {
// TODO Auto-generated method stub
MediaPlayer.create(this, R.raw.zqc);
super.onCreate();
} @Override
public void onDestroy() {
// TODO Auto-generated method stub
mp.release();
super.onDestroy();
} @Override
public void onStart(Intent intent, int startId) {
Bundle bundle = this.getIntent().getExtras();
String name=bundle.getString("name");
if(name.equals("start")){
mp.start();
}
else if(name.equals("pause")){
mp.pause();
}
else if(name.equals("stop")){
mp.stop();
}
}
private Intent getIntent() {
// TODO Auto-generated method stub
return null;
} @Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
设置的Intent的 action属性
<service android:name=".begin">
<intent-filter >
<action
android:name="com.example.begin.service"/>
</intent-filter>
</service>
求大家给的意见 我快烦死了。Android类
MediaPlayer.create(this, R.raw.zqc);
改成mp=MediaPlayer.create(this, R.raw.zqc);//读取音频试试!
另外你的程序有好多问题,具体你再看吧!关于这方面的资料网上挺多的,
MediaPlayer.create(this, R.raw.zqc);
改成mp=MediaPlayer.create(this, R.raw.zqc);//读取音频 也不对么?