How to play mp3 continuosly when application starts and stop when user close app in background

后端 未结 4 1718
一整个雨季
一整个雨季 2021-01-26 02:14

I am writing an app in which i am allowing user to view images and select one of them to set an WALLPAPER, and in this i also want to play an mp3 when user starts an App and sto

4条回答
  •  隐瞒了意图╮
    2021-01-26 02:56

    Use services.. Do something like that-

    public class ServicesDemo extends Activity implements OnClickListener {
     private static final String TAG = "ServicesDemo";
     Button buttonStart, buttonStop,buttonNext;
    
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
    
      buttonStart = (Button) findViewById(R.id.buttonStart);
      buttonStop = (Button) findViewById(R.id.buttonStop);
      buttonNext = (Button) findViewById(R.id.buttonNext);
    
      buttonStart.setOnClickListener(this);
      buttonStop.setOnClickListener(this);
      buttonNext.setOnClickListener(this);
     }
    
     public void onClick(View src) {
      switch (src.getId()) {
      case R.id.buttonStart:
       Log.d(TAG, "onClick: starting srvice");
       startService(new Intent(this, MyService.class));
       break;
      case R.id.buttonStop:
       Log.d(TAG, "onClick: stopping srvice");
       stopService(new Intent(this, MyService.class));
       break;
      case R.id.buttonNext:
       Log.d(TAG, "onClick: next Page");
       Intent intent=new Intent(this,NextPage.class);
       startActivity(intent);
       break;
      }
     }
    }
    

    Look at this post for full code-Androidhub4you

提交回复
热议问题