Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MediaPlayer
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mp = MediaPlayer.create(this, R.raw.color_brown);
Button b = (Button) findViewById(R.id.play);
Button b2= (Button) findViewById(R.id.pause);
Button b3= (Button) findViewById(R.id.volumedown);
Button b4= (Button) findViewById(R.id.volumeup);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "Start", Toast.LENGTH_SHORT).show();
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp) {
Toast.makeText(MainActivity.this, "Sfarsit de melodie", Toast.LENGTH_LONG).show();
}
});
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mp.pause();
}
});
b3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_LOWER, AudioManager.FLAG_SHOW_UI);
}
});
b4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
audio.adjustStreamVolume(AudioManager.STREAM_MUSIC,
AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
}
});}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.