Create a new Service and name it MyFirstBgmServic
package com.example.cyberwoodenfish;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.IBinder;
public class MyFirstBgmService extends Service {
public MyFirstBgmService() {
}
MediaPlayer mp;
@Override
public void onCreate() { // call
super .onCreate() when starting the service;
mp = MediaPlayer.create( this ,R.raw.dabeizhou);
mp.start();
}
@Override
public void onDestroy() { // Stop music playback when the service is destroyed
if (mp != null ){
mp.stop();
mp.release();
}
super.onDestroy ();
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented" );
}
}
Then start package com.example.cyberwoodenfish in MainActivity ;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intentbgm;
intentbgm = new Intent(MainActivity.this , MyFirstBgmService.class ) ;
startService(intentbgm); //. . . Code unrelated to bgm was eaten by me again
}
}
Read More:
- Android: How to get the size of font you set (Example Code)
- MySQL Batch Add Data and Store Example
- Opentelemetry + Jaeger Python Version Cross Service Call Example
- Electron: How to Use BrowserWindow to Create a Window
- How to Use Printf in HAL Library
- C#: How to Get details of the directory where the currently running program is located
- Docker: How to build a rabbitmq image cluster
- torch.max Example (How to Use)
- Windows Core Audio APIs: How to Progress Loopback Recording and Generate WAV File
- Canvas: How to Implement Video Screenshot Function
- Python: How to Create List by Comprehension (Example Codes)
- Hutool Excel Import & Export Example
- Jquery use queue to implement Ajax request queue Simple Example
- File class details (get the file name, size, path, create, etc.)
- Flutter & Dart Enumeration Example
- Echarts-for-React Example (Install, Import and Effect)
- Flutter & Dart every() Method Example
- Base64 Image Compression Example
- Flutter & Dart Regular Expression Examples
- Open CASCADE Technology 7.7.0 released