Twitter Facebook Delicious Digg Stumbleupon Favorites More

Tuesday, 22 May 2018

basic music play from saved mp3 android app

package com.example.viraj.mplay;

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

public class MainActivity extends AppCompatActivity {
    private MediaPlayer mediaPlayer;
    private Button playButton;
    private SeekBar MseekBar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MseekBar=(SeekBar)findViewById(R.id.id_seekbar);
        mediaPlayer = new MediaPlayer();
        mediaPlayer = MediaPlayer.create(MainActivity.this, R.raw.free);
MseekBar.setMax(mediaPlayer.getDuration());
        MseekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (fromUser)
{
    mediaPlayer.seekTo(progress);
}
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                int duration=mp.getDuration();
                String mDuration=String.valueOf(duration/1000);
                Toast.makeText(getApplicationContext(),"Duration is"+mDuration,Toast.LENGTH_LONG).show();
            }
        });

        playButton = (Button) findViewById(R.id.id_button);
        playButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mediaPlayer.isPlaying()) {
                    //stop and give option
                    pausemusic();
                }
                else {
                    startmusic();
                }
            }
        });
    }

    public void pausemusic() {
        if (mediaPlayer != null) {
            mediaPlayer.pause();
            playButton.setText("play");
        }
    }

    public void startmusic() {
        if (mediaPlayer != null) {
            mediaPlayer.start();
            playButton.setText("PAUSE");
        }
    }

    @Override
    protected void onDestroy() {
        if (mediaPlayer!=null && mediaPlayer.isPlaying())
        {
mediaPlayer.stop();
mediaPlayer.release();
mediaPlayer=null;
        }
        super.onDestroy();
    }
}



xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".MainActivity"
    android:orientation="vertical"
    android:gravity="center">

    <Button
        android:id="@+id/id_button"
        android:layout_width="wrap_content"

        android:layout_height="wrap_content"
        android:text="@string/Buttonplay" />

    <SeekBar
        android:id="@+id/id_seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Share:

0 comments:

Post a Comment

Blogger Tutorials

Blogger Templates

Sample Text

Copyright © ANDROID TUTORIAL CODE | Powered by Blogger
Design by SimpleWpThemes | Blogger Theme by NewBloggerThemes.com & Distributed By Protemplateslab