简体   繁体   English

如何从JobScheduler的JobService将MediaPlayer对象传递给AsyncTask

[英]how to pass MediaPlayer object to AsyncTask from JobService of JobScheduler

I am using JobScheduler which uses AsyncTask for its JobService. 我正在使用JobScheduler,其JobService使用AsyncTask。 In the class MJobExecutor which extends AsyncTask uses MediaPlayer.I want to pass the Media Player Object but how. 在扩展AsyncTask的MJobExecutor类中使用MediaPlayer。我想传递媒体播放器对象,但如何传递。 Following is my code for the AsyncTask class called MJobExecutor.java I have made a constructor to pass the MediaPlayer object.But the code seems not working. 以下是我的名为MJobExecutor.java的AsyncTask类的代码,我已经构造了一个传递MediaPlayer对象的构造函数,但是代码似乎无法正常工作。

public class MJobExecutor extends AsyncTask<Void,Void,String> {

ValueExchange value;
private MediaPlayer player;
//Constructor to pass MediaPlayer object.
public MJobExecutor(MediaPlayer player){
    this.player = player;
}
@Override
protected String doInBackground(Void... params) {

    value = new ValueExchange();
    Calendar cal = Calendar.getInstance();
    Date date=cal.getTime();
    DateFormat dateFormat = new SimpleDateFormat("hh:mm a");
    String formattedDate=dateFormat.format(date);
    if(formattedDate.equals(value.getString())){

    }
    play();
    return "Long running task finishes." + value.getString();
}



private void play(){
    if(player == null){
        //Below the getApplicationContext() not working even after the constructor-
        //being declared.
        player = MediaPlayer.create(getApplicatioContext(),R.raw.bensoundfunkyelement);
        player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopPlayer();
            }
        });
    }
    player.start();
}
private void stop(){
    stopPlayer();
}
private void stopPlayer(){
    if(player != null){
        player.release();
        player = null;
    }
}

}

And again in class MJobScheduler.java which extends JobService, MediaPlayer object is passed.But yet it is not working. 在扩展JobService的MJobScheduler.java类中,再次传递了MediaPlayer对象。

public class MJobScheduler extends JobService {

MJobExecutor mJobExecutor;
String alarmTime;
ValueExchange value;
MediaPlayer player;
@Override
public boolean onStartJob(final JobParameters params) {
    alarmTime = params.getExtras().getString("alarmTime");
    value = new ValueExchange();
    value.setString(alarmTime);
    //MediaPlayer object is Passed into the constructor-
    //but the app crashes
    mJobExecutor = new MJobExecutor(player){

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(getApplicationContext(),alarmTime+" "+s,Toast.LENGTH_LONG).show();
            jobFinished(params,false);
        }
    };
    mJobExecutor.execute();
    return true;
}

@Override
public boolean onStopJob(JobParameters params) {
    mJobExecutor.cancel(false);
    return false;
}
}

The Main Activity class is as follows... 主要活动类如下...

public class MainActivity extends AppCompatActivity {

private static final int JOB_ID = 101;
JobScheduler jobScheduler;
JobInfo jobInfo;



TextView textTime;
ImageButton ibLeft,ibRight,ibTop,ibBottom;
TextClock textClock;



String alarmTime = "12:00 AM";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    textTime = (TextView)findViewById(R.id.textView);
    ibLeft = (ImageButton)findViewById(R.id.left);
    ibRight = (ImageButton)findViewById(R.id.right);
    ibTop = (ImageButton)findViewById(R.id.top);
    ibBottom = (ImageButton)findViewById(R.id.bottom);

    textClock.setPadding(0,250,0,0);


    ComponentName componentName = new ComponentName(this,MJobScheduler.class);
    PersistableBundle bundle = new PersistableBundle();
    bundle.putString("alarmTime",alarmTime);

    JobInfo.Builder builder = new JobInfo.Builder(JOB_ID,componentName);

    builder.setExtras(bundle);
    builder.setPeriodic(5000);
    builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
    builder.setPersisted(true);
    jobInfo = builder.build();
    jobScheduler = (JobScheduler) getSystemService(JOB_SCHEDULER_SERVICE);



}


public void Start(View view) {

    jobScheduler.schedule(jobInfo);
    Toast.makeText(this,"Job Started...",Toast.LENGTH_LONG).show();
}

public void Stop(View view) {
    jobScheduler.cancel(JOB_ID);
    Toast.makeText(this,"Job Cancelled...",Toast.LENGTH_LONG).show();
}


}

Instantiate your MediaPlayer object inside class MJobScheduler so that you won't need context inside your AsyncTask . 在类MJobScheduler实例化MediaPlayer对象,以便您在AsyncTask不需要上下文。

player = MediaPlayer.create(getApplicatioContext(),R.raw.bensoundfunkyelement);

Move the above line from your method play in class MJobExecutor to the method onStartJob of class MJobScheduler before the line mJobExecutor = new MJobExecutor(player)... 将上面的行从您在MJobExecutor类中的方法play MJobExecutor onStartJob类的MJobScheduler方法之前,然后在mJobExecutor = new MJobExecutor(player)...

Then in your method play , remove the condition check if(player == null) because it will always be false preventing the code inside it from being executed. 然后在方法play ,删除条件检查if(player == null)因为它始终为false,从而阻止执行其中的代码。

So body of your method onStartJob will be as follows: 因此, onStartJob方法的onStartJob如下所示:

alarmTime = params.getExtras().getString("alarmTime");
value = new ValueExchange();
value.setString(alarmTime);

player = MediaPlayer.create(getApplicatioContext(), R.raw.bensoundfunkyelement);
mJobExecutor = new MJobExecutor(player){

    @Override
    protected void onPostExecute(String s) {
        Toast.makeText(getApplicationContext(),alarmTime+" "+s,Toast.LENGTH_LONG).show();
        jobFinished(params,false);
    }
};
mJobExecutor.execute();
return true;

And your play method will be as follows: 您的play方法如下:

private void play() {
    player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
        @Override
        public void onCompletion(MediaPlayer mp) {
            stopPlayer();
        }
    });
    player.start();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM