Monday, 28 November 2016

Android AudioPlayer

The Android mixed media system incorporates bolster for playing assortment of regular media sorts, so you can without much of a stretch coordinate sound, video and pictures into your applications. You can play sound or video from media documents put away in your application's assets (crude assets), from standalone records in the filesystem, or from an information stream landing over a system association, all utilizing MediaPlayer APIs.

And here i am showing example for Android Audio player.

activity_main.xml :-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" 
 android:orientation="vertical">

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="PLAY" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="PAUSE" 
 android:id="@+id/button2" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="STOP" 
 android:id="@+id/button3" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements
                                                View.OnClickListener {
Button playbtn,pausebtn,stopbtn;
MediaPlayer mp;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 playbtn=(Button)findViewById(R.id.button);
 pausebtn=(Button)findViewById(R.id.button2);
 stopbtn=(Button)findViewById(R.id.button3);
 playbtn.setOnClickListener(this);
 pausebtn.setOnClickListener(this);
 stopbtn.setOnClickListener(this);
   }

@Override 
 public void onClick(View v) {
 switch (v.getId()){
 case R.id.button:
 mp.start();
 break;
 case R.id.button2:
 mp.pause();
 break;
 case R.id.button3:
 mp.stop();
 mp=MediaPlayer.create(getApplicationContext(),R.raw.dhruva);
    }
}

Output :-

Android Audio
Android Audio Player

No comments:

Post a Comment