当前位置:编程学习 > wap >>

关于android播放器小问题

自己没事写了一个android音乐播放器 后台播放那种蛋疼的是自己感觉代码没错啊,就是出不来。。大家帮我看看 我搞一天了 做不出来我会睡不着的。。
xml文件就是三个按钮  start pause stop
src
MAIN

package 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;
}

}
Service

package 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 类 --------------------编程问答-------------------- 错误日志 --------------------编程问答-------------------- 创建mp对象就不对:
MediaPlayer.create(this, R.raw.zqc);
改成mp=MediaPlayer.create(this, R.raw.zqc);//读取音频

试试!
另外你的程序有好多问题,具体你再看吧!关于这方面的资料网上挺多的, --------------------编程问答--------------------
引用 2 楼 z03081118 的回复:
创建mp对象就不对:
MediaPlayer.create(this, R.raw.zqc);
改成mp=MediaPlayer.create(this, R.raw.zqc);//读取音频

试试!
另外你的程序有好多问题,具体你再看吧!关于这方面的资料网上挺多的,

我也知道代码写的很乱可是我自己找真心找不到哪里有错误,我感觉格式什么的都对啊。。。。 --------------------编程问答--------------------
引用 3 楼 wstxdxyx 的回复:
Quote: 引用 2 楼 z03081118 的回复:

创建mp对象就不对:
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);//读取音频 也不对么?
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,