放在service里面如何实现控制当前播放。

解决方案 »


  1. //PlayerActivity.java(播放界面)
    //Service.java//在播放界面单击播放后
    Intent intent = new Intent();
    intent.putExtra("mp3Info",mp3Info);  
    intent.putExtra("MSG",MSG_PLAY);     //MSG_PLAY为播放常量
    intent.setClass(MainActivity.this,PlayerActivity.class);
    startService(intent);//然后在service里接收后处理播放请求
      

  2. 刚才写错了
    intent.setClass(PlayerActivity.thisService.class);
      

  3. activity1:
         listview.setOnItemClickListener(new OnItemClickListener() { @Override
    public void onItemClick(AdapterView<?> arg0, View arg1,
    int position, long arg3) {  
     Intent intent=new Intent(ItemActivity.this,playservice.class);
           String pathString= lists.get(position).getUrl();
           String title=lists.get(position).getTitle();
           intent.putExtra("uri", pathString);
           intent.putExtra("title", title);
     startActivity(intent);
    }
    });activity2:
    public class Playing extends Activity{
    MediaPlayer mediaPlayer;
    Button back;
    TextView textView;
    ImageButton start;
    Button next; @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.musicplay);       Intent intent=getIntent();
           String pathString= intent.getStringExtra("uri");
           String titleString=intent.getStringExtra("title");
           Uri uri=Uri.parse(pathString);
    Log.i("uri",pathString);
    back=(Button) findViewById(R.id.button_back);
    start=(ImageButton) findViewById(R.id.imageButton1);
    start.setBackgroundResource(R.drawable.play);
    textView=(TextView) findViewById(R.id.textView_music);
    next=(Button) findViewById(R.id.button_next);
    mediaPlayer=MediaPlayer.create(this,uri);
    textView.setText(titleString);
    mediaPlayer.start();
    start.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {

                 if(mediaPlayer.isPlaying()){//暂停
                  mediaPlayer.pause();
                  start.setBackgroundResource(R.drawable.play);
                  Log.i("play", "play");
                 }else {//继续播放
                  mediaPlayer.start();
                  start.setBackgroundResource(R.drawable.stop);
                  Log.i("stop", ""+mediaPlayer);
    }
    }
    });}
    }
      

  4. 刚才写错了
    intent.setClass(PlayerActivity.thisService.class);看一下该怎么修改呢?
      


  5. start.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    Intent intent = new Intent();
    intent.setClass(PlayerActivity.this, PlayerService.class);
    intent.putExtra("MSG", AppConstant.PlayerMsg.MSG_PLAY);
                           intent.putExtra("uri", pathString);
                            intent.putExtra("title", title);
    startService(intent);
    }
    }
    });
    Service.javapublic class PlayerService extends Service{

    private MediaPlayer mediaPlayer = null;     @Override
    public IBinder onBind(Intent intent) {
    return null;
    } @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
                     String pathString= intent.getStringExtra("uri");
                     String titleString=intent.getStringExtra("title");
                     Uri uri=Uri.parse(pathString);
    int MSG = intent.getIntExtra("MSG", 0); if(MSG == AppConstant.PlayerMsg.PLAY_MSG){      //AppConstant.PlayerMsg.PLAY_MSG为自定义的常量,我定义为1
    play();
    }
    return super.onStartCommand(intent, flags, startId);
    }
          public void play(){
            //在这写播放的代码

      

  6. Quote: 引用 8 楼 oqqneet12 的回复:

    [code=java]
    但是我前面是先播放的onclick是一个imagebutton的响应事件。。
      

类似问题 »