如何在Android中播放服务声音?

时间:2015-11-26 14:55:15

标签: java android service

在一个特殊的Android应用程序中我需要制作一个声音播放几个小时并通过我的服务控制它的服务...但声音播放器的代码在我的服务类的任何方法中都不起作用:((< / p>

我从“服务类”扩展的播放器类:

makeConnection()

}

我的主要活动:

public class QueueManagerServices {

final static int MQOO_INQUIRE_TOTAL = CMQC.MQOO_FAIL_IF_QUIESCING | CMQC.MQOO_INPUT_SHARED | CMQC.MQOO_INQUIRE;

MQQueueManager queueManager = null;
String cluster = null;
MQQueue queue = null;

public String getcluster(String host, int portNo, String qMgr, String channelName){

 try{
  queueManager = makeConnection(host, portNo, qMgr, channelName);
  queue = queueManager.accessQueue(queueName, MQOO_INQUIRE_TOTAL);

  int MQCA_CLUSTER_NAME = 2029;
  int MQ_CLUSTER_NAME_LENGTH = 48;

  int[] selectors = new int[1];
  int[] intAttrs = new int[1];
  byte[] charAttrs = new byte[MQ_CLUSTER_NAME_LENGTH];

  selectors[0] = MQCA_CLUSTER_NAME;

  queue.inquire(selectors, intAttrs, charAttrs);

  cluster = new String (charAttrs);

  } catch (MQException e) {
      System.out.println(e);
  } finally {
      if (queue != null){
        queue.close();
      }
      if (queueManager != null){
        queueManager.disconnect();
      }
  }
  return cluster;
  }
 }

并显示......

package org.tooj;




import org.qtproject.example.Book.R;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.IBinder;




public class Player extends Service {



MediaPlayer Player;
@Override
public void onCreate() {



}

@Override
public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

 //my player 
public int onStartCommand(Intent intent, int flags, int startId) {
    Player = MediaPlayer.create(ToojActivity.getInstance(),R.ra  w.alfa); 
    Player.setLooping(true);
    Player.setVolume(1,1);
    Player.start();
    return 1;
}

public void onStart(Intent intent, int startId) {
    // TO DO
}
public IBinder onUnBind(Intent arg0) {
    // TO DO Auto-generated method
    return null;
}

public void onStop() {

}
public void onPause() {

}
@Override
public void onDestroy() {
    Player.stop();
    Player.release();
}

@Override
public void onLowMemory() {

}

0 个答案:

没有答案