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>

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

Membuat Aplikasi SMS di Android Studio

Walaupun di android sudah disediakan aplikasi perpesanan bawaan, tetapi tidak ada salahnya kita membuat aplikasi SMS ini, setidaknya untuk mengetahui bagaimana cara kerja dari aplikasi SMS ini, ataupun jika anda ingin membuat aplikasi SMS yang custom, misalnya membuat aplikasi SMS dengan gaya iMessage. Pada kesempatan kali ini akan dibahas cara membuat aplikasi SMS sederhana.



Pertama-tama buat desain tampilannya dulu
<?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"
   
android:id="@+id/activity_main"
   
android:padding="10dp"
   
tools:context="com.giviews.message.MainActivity">

    <
RelativeLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content">
        <
EditText
           
android:layout_width="match_parent"
            
android:layout_height="wrap_content"
           
android:inputType="phone"
           
android:ems="15"
           
android:id="@+id/tvNumber"
           
android:hint="Enter contact number"
           
android:singleLine="true" />
    </
RelativeLayout>

    <
LinearLayout
       
android:layout_width="match_parent"
       
android:layout_height="wrap_content"
       
android:orientation="vertical"
       
android:id="@+id/pushingUp"
       
android:layout_alignParentBottom="true"
       
android:layout_alignParentStart="true"
       
android:layout_alignParentLeft="true">
        <
LinearLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:orientation="vertical">
            <
ScrollView
               
android:layout_width="match_parent"
               
android:layout_height="match_parent"
               
android:layout_weight="1">
                <
LinearLayout
                   
android:layout_width="match_parent"
                    
android:layout_height="wrap_content"
                   
android:orientation="vertical">
                    <
TextView
                       
android:layout_width="match_parent"
                       
android:layout_height="match_parent"
                        
android:id="@+id/txtMessage"
                       
android:layout_weight="1"/>
                </
LinearLayout>
            </
ScrollView>
        </
LinearLayout>

        <
LinearLayout
           
android:layout_width="match_parent"
           
android:layout_height="wrap_content"
           
android:orientation="horizontal">
            <
EditText
               
android:layout_width="0dp"
               
android:layout_height="wrap_content"
               
android:ems="160"
                
android:layout_weight="5"
               
android:id="@+id/tvMessage"
               
android:hint="Enter Message"/>
            <
Button
               
android:layout_width="0dp"
               
android:layout_weight="1"
               
android:background="@android:drawable/ic_menu_send"
               
android:layout_height="wrap_content"
               
android:id="@+id/btnSend" />
        </
LinearLayout>
    </
LinearLayout>

</
RelativeLayout>

Kemudian untuk di javanya masukan kode ini
package com.giviews.message;

import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
   
private Button btnSend;
   
private EditText tvMessage;
   
private EditText tvNumber;
    IntentFilter
intentFilter;

   
private BroadcastReceiver intentReceiver = new BroadcastReceiver() {
       
@Override
       
public void onReceive(Context context, Intent intent) {
            
//display the message in the textview
           
TextView inTxt = (TextView) findViewById(R.id.txtMessage);
            inTxt.setText(intent.getExtras().getString(
"message"));
        }
    };

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
        setContentView(R.layout.
activity_main);

       
//intent to filter for SMS message received
       
intentFilter = new IntentFilter();
       
intentFilter.addAction("SMS_RECEIVED_ACTION");

       
btnSend = (Button) findViewById(R.id.btnSend);
       
tvNumber = (EditText) findViewById(R.id.tvNumber);
       
tvMessage = (EditText) findViewById(R.id.tvMessage);

       
btnSend.setOnClickListener(new View.OnClickListener() {
            
@Override
           
public void onClick(View view) {
                String myMsg =
tvMessage.getText().toString();
                String txtNumber =
tvNumber.getText().toString();
                sendMsg(txtNumber, myMsg);
            }
        });
    }

   
private void sendMsg(String txtNumber, String myMsg) {
        String SENT =
"Message Send";
        String DELIVERED =
"Message Delivered";

        PendingIntent sentPi = PendingIntent.getBroadcast(
this, 0, new Intent(SENT), 0);
        PendingIntent deliveredPi = PendingIntent.getBroadcast(
this, 0, new Intent(DELIVERED), 0);

        SmsManager sms = SmsManager.getDefault();
        sms.sendTextMessage(txtNumber,
null, myMsg, sentPi, deliveredPi);
    }

   
@Override
   
protected void onResume() {
       
//register the receiver
       
registerReceiver(intentReceiver, intentFilter);
       
super.onResume();
    }

   
@Override
   
protected void onPause() {
       
//unregister the receiver
       
unregisterReceiver(intentReceiver);
        
super.onPause();
    }
}

lalu buatlah class baru di java dengan nama MessageReceiver dan masukan kode ini
package com.giviews.message;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.gsm.SmsMessage;
import android.widget.Toast;

/** * Created by asus on 04/10/2017. */
public class MessageReceiver extends BroadcastReceiver {
    @Override    public void onReceive(Context context, Intent intent) {
        //get message passed in        Bundle bundle = intent.getExtras();
        SmsMessage[] messages;
        String str = "";

        if (bundle != null) {
            Object[] pdus = (Object[]) bundle.get("pdus");
            messages = new SmsMessage[pdus != null ? pdus.length : 0];
            for (int i=0; i<messages.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) (pdus != null ? pdus[i] : null));
                str += messages[i].getOriginatingAddress();
                str += ": ";
                str += messages[i].getMessageBody();
                str += "\n";
            }

            //display the message            Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

            //Send a broadcast intent to update the SMS received in a TextView            Intent broadcastIntent = new Intent();
            broadcastIntent.setAction("SMS_RECEIVED_ACTION");
            broadcastIntent.putExtra("messages", str);
            context.sendBroadcast(broadcastIntent);
        }
    }
}

Terakhir tambahkan permission di AndroidManifest untuk mengirim
dan menerima SMS

<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.RECEIVE_SMS"/>

dan daftarkan class MessageReceiver
<receiver android:name=".MessageReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
</receiver>

Sekarang coba jalankan di smartphone android anda / di emulator, jika berhasil tampil seperti pada gambar diatas anda sudah berhasil. Ok sekianlah tutorial kali ini Selamat mencoba Semoga bermanfaat kurang dan lebihnya mohon maaf jika ada yang kurang

jelas silakan ditanyakan di komentar