2017-01-31 6 views
0

私は自分のプロジェクトでメディアプレーヤーを実装しましたが、アプリケーションを終了しても音楽を再生しますが、アプリケーションを終了すると、音楽。 私が直面している私の問題は、私のアプリケーションが未熟なアプリケーションを再び開くたびに、以前のものを再生して、音楽をもう一度再生することです。私は間違っていますか?バックグラウンドサービスでメディアプレーヤーを正しく機能させることができません

私の必要条件は、音楽を開いたらすぐに再生を開始する必要があります。私のアプリを終了しても、バックグラウンドで再生する必要があります。

Main_activity.java

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    String msg = "Android : "; 

    private Button play,pause,restart; 
    LocalService mService; 
    boolean mBound = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     play=(Button) findViewById(R.id.play); 
     pause=(Button) findViewById(R.id.pause); 
     restart=(Button) findViewById(R.id.restart); 
     play.setOnClickListener(this); 
     pause.setOnClickListener(this); 
     restart.setOnClickListener(this); 
     Intent intent = new Intent(this, LocalService.class); 
     startService(intent); 
    } 
    /** Called when the activity is about to become visible. */ 
    @Override 
    protected void onStart() { 
     super.onStart(); 
     Log.d(msg, "The onStart() event"); 
     Intent intent = new Intent(this, LocalService.class); 
     bindService(intent, mConnection, Context.BIND_AUTO_CREATE); 
    } 

    /** Called when the activity has become visible. */ 
    @Override 
    protected void onResume() { 
     super.onResume(); 
     Log.d(msg, "The onResume() event"); 

    } 

    /** Called when another activity is taking focus. */ 
    @Override 
    protected void onPause() { 
     super.onPause(); 
     Log.d(msg, "The onPause() event"); 
    } 

    /** Called when the activity is no longer visible. */ 
    @Override 
    protected void onStop() { 
     super.onStop(); 
     Log.d(msg, "The onStop() event"); 

    } 

    /** Called just before the activity is destroyed. */ 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
     Log.d(msg, "The onDestroy() event"); 
     Log.d("mbound value",mBound+""); 
     if (mBound) { 
      Intent intent=new Intent(this,LocalService.class); 
      stopService(intent); 
      unbindService(mConnection); 
      mBound = false; 
     } 
    } 

    @Override 
    public void onClick(View v) { 
      switch (v.getId()){ 
       case(R.id.play): 
        if(mBound) 
         mService.start_mp(); 
        break; 
       case(R.id.pause): 
        if(mBound) 
         mService.pause_mp(); 
        break; 
       case (R.id.restart): 
        if(mBound) 
         mService.restart_mp(); 
        break; 

       default: 
        break; 
      } 
    } 
    /** Defines callbacks for service binding, passed to bindService() */ 
    public ServiceConnection mConnection = new ServiceConnection() { 

     @Override 
     public void onServiceConnected(ComponentName className, 
             IBinder service) { 
      // We've bound to LocalService, cast the IBinder and get LocalService instance 
      LocalService.LocalBinder binder = (LocalService.LocalBinder) service; 
      mService = binder.getService(); 
      mBound = true; 
     } 

     @Override 
     public void onServiceDisconnected(ComponentName arg0) { 
      mBound = false; 
     } 
    }; 
} 

LocalSe - 音楽がここに

の再生を停止すべきであることは私のコードですrvice.java

public class LocalService extends Service { 

    private MediaPlayer mediaPlayer; 

    // Binder given to clients 
    private final IBinder mBinder = new LocalBinder(); 

    @Nullable 
    @Override 
    public IBinder onBind(Intent intent) { 
     Log.i("Onbind called",""); 
     return mBinder; 
    } 

    /** Called when the service is being created. */ 
    @Override 
    public void onCreate() { 
     super.onCreate(); 
     mediaPlayer=MediaPlayer.create(this,R.raw.jingle); 

    } 

    /** The service is starting, due to a call to startService() */ 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     mediaPlayer.start(); 
     Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show(); 
     return START_STICKY; 
    } 

    /** Called when all clients have unbound with unbindService() */ 
    @Override 
    public boolean onUnbind(Intent intent) { 
     return true; 
    } 


    /** Called when a client is binding to the service with bindService()*/ 
    @Override 
    public void onRebind(Intent intent) { 
     super.onRebind(intent); 
    } 

    /** Called when The service is no longer used and is being destroyed */ 
    @Override 
    public void onDestroy() { 
     super.onDestroy(); 
//  Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show(); 
    } 

    /** 
    * Class used for the client Binder. Because we know this service always 
    * runs in the same process as its clients, we don't need to deal with IPC. 
    */ 
    public class LocalBinder extends Binder { 
     LocalService getService() { 
      // Return this instance of LocalService so clients can call public methods 
      return LocalService.this; 
     } 
    } 
    public void start_mp(){ 
     if(!mediaPlayer.isPlaying()) 
     mediaPlayer.start(); 
    } 
    public void pause_mp(){ 
     mediaPlayer.pause(); 
    } 
    public void restart_mp(){ 
     if(mediaPlayer.isPlaying()) mediaPlayer.release(); 
     mediaPlayer=MediaPlayer.create(this,R.raw.jingle); 
     mediaPlayer.start(); 
    } 


} 

P.S.-私は私のアプリ内のプレーヤーの機能のための私のアプリケーションでボタンを適用しています。

答えて

0

私は自分のホームボタンを押すと、私のonstop()メソッドが実行され、私は何を目指しているのかを取得します。戻るボタンを押すと、アンドロイドのデフォルトアクションはアクティビティを破棄します。

アンドロイドデバイスのデフォルトの戻るボタンを押すと、アクティビティのfinish()メソッドが呼び出され、OnStop()メソッドが呼び出されるホームボタンを押してOnDestroy()を呼び出します。

関連する問題