Sabtu, 03 Oktober 2020

BackSound

 package com.me;


import android.app.Service;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.IBinder;
import android.preference.PreferenceManager;

import androidx.annotation.Nullable;

public class MyService extends Service {

private MediaPlayer player;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getBaseContext());//customer.this
Boolean on= sharedPref.getBoolean("on", true);
if (on){
player = MediaPlayer.create(this, R.raw.backsound);
player.setLooping(true);
player.start();
}

return START_NOT_STICKY;

}

@Override
public void onDestroy() {
super.onDestroy();
try {
player.stop();
player.release();
}
catch (Exception ee){}
}
}

=======================
  <activity
android:name=".Video_Thumbnail2"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>


<service android:name=".MyService"/>
</application>

</manifest>
============================
public void login(View view){
MediaPlayer klik = new MediaPlayer().create(this, R.raw.clik);
klik.start();

Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
startService(new Intent(getApplicationContext(), MyService.class));
}


@Override
public void onBackPressed() {
super.onBackPressed();
moveTaskToBack(true);
stopService(new Intent(getApplicationContext(), MyService.class));
}


hasil   = findViewById(R.id.hasil);
nilai = findViewById(R.id.nilai);
Typeface typeface = Typeface.createFromAsset(getAssets(),"fonts/FredokaOneRegular.ttf");
hasil.setTypeface(typeface);
nilai.setTypeface(typeface);


Reff: https://android-coffee.com/tutorial-how-to-play-youtube-videos-in-android-with-videoview/

Tidak ada komentar:

Posting Komentar