Git Product home page Git Product logo

android-audioplayer's Introduction

Android-AudioPlayer

Read Songs From Device and Play

Play local raw resource (application's res/raw/ directory)

MediaPlayer mediaPlayer = MediaPlayer.create(context, R.raw.sound_file_1);
mediaPlayer.start(); // no need to call prepare(); create() does that for you

Play from URI available in locally system(obtained through a Content Resolver)

Uri myUri = ....; // initialize Uri here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(getApplicationContext(), myUri);
mediaPlayer.prepare();
mediaPlayer.start();

Play from Remote URL via HTTP

String url = "http://........"; // your URL here
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
mediaPlayer.setDataSource(url);
mediaPlayer.prepare(); // might take long! (for buffering, etc)
mediaPlayer.start();

Releasing MediaPlayer

A MediaPlayer can consume valuable system resources. Therefore, you should always take extra precautions to make sure you are not hanging on to a MediaPlayer instance longer than necessary.

mediaPlayer.release();
mediaPlayer = null;

Create Handler for Timer

Handler mHandler=new Handler();

if(mediaPlayer==null)
{
	...
	updateProgressBar();
	...
}
...
public void updateProgressBar()
    {
        mHandler.postDelayed(mUpdateTimerTask,1000);
    }

 private Runnable mUpdateTimerTask=new Runnable() {
        @Override
        public void run() {
            long totalDuration=mediaPlayer.getDuration();
            long currentDuration=mediaPlayer.getCurrentPosition();

            songTotalDurationLabel.setText(milliSecondsToTimer(totalDuration));
            songCurrentDurationLabel.setText(milliSecondsToTimer(currentDuration));

            mHandler.postDelayed(this,1000);

        }
    };

private String milliSecondsToTimer(String songDuration) {
        int duration=Integer.parseInt(songDuration);
        int hour=(int)(duration/(1000*60*60));
        int minute=(int)((duration%(1000*60*60))/(1000*60));
        int seconds=(int)(((duration%(1000*60*60))%(1000*60))/(1000));
        String finalString="";
        if(hour<10)
            finalString+="0";
        finalString+=hour+":";
        if(minute<10)
            finalString+="0";
        finalString+=minute+":";
        if(seconds<10)
            finalString+="0";
        finalString+=seconds;

        return finalString;
    }

Adding SeekBar to Show Song ProgressBar

if(mediaPlayer==null)
{
	...
	songProgressBar.setProgress(0);
        songProgressBar.setMax(100);
	...
}

 private Runnable mUpdateTimerTask=new Runnable() {
        @Override
        public void run() {

	...
            songProgressBar.setProgress(getProgressPercentage(totalDuration,currentDuration));

	...
        }
    };


    private int getProgressPercentage(int totalDuration, int currentDuration) {
        return (int)(currentDuration*100)/totalDuration;
    }

Handle Seekbar event

public class MediaPlayerDemo extends AppCompatActivity implements SeekBar.OnSeekBarChangeListener{
	...
}

 @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
	      if(fromUser) {
            mMediaPlayer.seekTo(getTimeFromProgress(seekBar.getProgress(), mMediaPlayer.getDuration()));
        }

    }

    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        
    }

    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    }

    private int getTimeFromProgress(int progress, int duration) {
        int songDuration=(int)((duration*progress)/100);
        return songDuration;
    }

screenshot_audioplayer

android-audioplayer's People

Contributors

kotlintpoint avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.