Membuat Aplikasi Multimedia Pemutar Musik dan Video di Android Studio
Pada
kesempatan kali ini kita akan membuat aplikasi pemutar music dan pemutar video
di android, kita akan menggunakan dua activity yang pertama buat music player
dan yang kedua buat video player.
Untuk itu
langsung saja buat SoundActivity dan masukan seekbar beserta beberapa tombol di
xmlnya
<?xml
version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.giviews.soundandvideoplayer.SoundActivity">
<SeekBar
android:id="@+id/soundSeekBar"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="179dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play"
android:layout_below="@+id/soundSeekBar"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"/>
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pause"
android:layout_centerHorizontal="true"
android:layout_below="@+id/soundSeekBar"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"/>
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:layout_alignParentRight="true"
android:layout_below="@+id/soundSeekBar"
android:layout_marginRight="16dp"
android:layout_marginTop="38dp"/>
<Button
android:id="@+id/videoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See the Video"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.giviews.soundandvideoplayer.SoundActivity">
<SeekBar
android:id="@+id/soundSeekBar"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="179dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/playButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="play"
android:layout_below="@+id/soundSeekBar"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"/>
<Button
android:id="@+id/pauseButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pause"
android:layout_centerHorizontal="true"
android:layout_below="@+id/soundSeekBar"
android:layout_marginLeft="16dp"
android:layout_marginTop="38dp"/>
<Button
android:id="@+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="stop"
android:layout_alignParentRight="true"
android:layout_below="@+id/soundSeekBar"
android:layout_marginRight="16dp"
android:layout_marginTop="38dp"/>
<Button
android:id="@+id/videoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See the Video"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="36dp"/>
</RelativeLayout>
kemudian
untuk java di SoundActivity masukan kode dibawah
package com.giviews.soundandvideoplayer; import android.app.Activity; import android.content.Intent; import android.media.MediaPlayer; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.SeekBar; public class SoundActivity extends Activity implements Runnable { private Button startButton; private Button stopButton; private Button pauseButton; private Button videoButton; private SeekBar soundSeekBar; private MediaPlayer soundPlayer; private Thread soundThread; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sound); startButton = (Button) findViewById(R.id.playButton); pauseButton = (Button) findViewById(R.id.pauseButton); stopButton = (Button) findViewById(R.id.stopButton); videoButton = (Button) findViewById(R.id.videoButton); soundSeekBar = (SeekBar) findViewById(R.id.soundSeekBar); soundPlayer = MediaPlayer.create(this.getBaseContext(), R.raw.pomdeter); setupListeners(); soundThread = new Thread(this); soundThread.start(); } private void setupListeners() { startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { soundPlayer.start(); } }); pauseButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { soundPlayer.pause(); } }); stopButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View currentView) { soundPlayer.stop(); soundPlayer = MediaPlayer.create(getBaseContext(), R.raw.pomdeter); } }); videoButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View currentView) { Intent intent = new Intent(currentView.getContext(), VideoActivity.class); startActivityForResult(intent, 0); } }); soundSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { if (fromUser) { soundPlayer.seekTo(progress); } } @Override public void onStartTrackingTouch(SeekBar seekBar) { } @Override public void onStopTrackingTouch(SeekBar seekBar) { } }); } @Override public void run() { int currentPosition = 0; int soundTotal = soundPlayer.getDuration(); soundSeekBar.setMax(soundTotal); while (soundPlayer != null && currentPosition < soundTotal) { try { Thread.sleep(300); currentPosition = soundPlayer.getCurrentPosition(); } catch (InterruptedException soundException) { return; } catch (Exception otherException) { return; } soundSeekBar.setProgress(currentPosition); } } @Override public void onPointerCaptureChanged(boolean hasCapture) { } }
selanjutnya
buat VideoActivity, di Video Activity masukan VideoView dan sebuah button untuk
kembali ke activity sebelumnya
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.giviews.soundandvideoplayer.VideoActivity"> <VideoView android:id="@+id/videoView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:layout_marginTop="164dp"/> <Button android:id="@+id/homeButton" android:text="goto sound player" android:layout_marginTop="36dp" android:layout_width="match_parent" android:layout_height="wrap_content" /> </RelativeLayout>
untuk java
di VideoActivity masukan kode di bawah
package com.giviews.soundandvideoplayer; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.MediaController; import android.widget.VideoView; public class VideoActivity extends Activity { private VideoView myPlayer; private Button returnButton; private MediaController myVideoController; private Uri videoLocation; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_video); myPlayer = (VideoView) findViewById(R.id.videoView); returnButton = (Button) findViewById(R.id.homeButton); videoLocation = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.party); myVideoController = new MediaController(this); //prepare the video setupMedia(); setupListeners(); } private void setupListeners() { returnButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View currentView) { Intent returnIntent = new Intent(); setResult(RESULT_OK, returnIntent); finish(); } }); } private void setupMedia() { myPlayer.setMediaController(myVideoController); myPlayer.setVideoURI(videoLocation); } }
kemudian
untuk file music dan videonya buat directory raw di dalam folder res dan
masukan file mp3 dan mp4 untuk sample
Sekarang
jalankan aplikasinya di emulator / di hp android anda jika tampilanya seperti
gambar diatas maka berhasil, sekianlah tutorial kali ini selamat mencoba semoga
bermanfaat, jangan lupa share artikel
ini jika bermanfaat bila ada yang kurang jelas silakan di tanyakan di komentar
dibawah