Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

android MediaController audio example

import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

import android.media.MediaPlayer.OnPreparedListener;
import android.view.MotionEvent;
import android.widget.MediaController;
import android.widget.TextView;

import java.io.IOException;

public class AudioPlayer extends Activity implements OnPreparedListener, MediaController.MediaPlayerControl{
  private static final String TAG = "AudioPlayer";

  public static final String AUDIO_FILE_NAME = "audioFileName";

  private MediaPlayer mediaPlayer;
  private MediaController mediaController;
  private String audioFile;

  private Handler handler = new Handler();

  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.audio_player);

    audioFile = this.getIntent().getStringExtra(AUDIO_FILE_NAME);
    ((TextView)findViewById(R.id.now_playing_text)).setText(audioFile);

    mediaPlayer = new MediaPlayer();
    mediaPlayer.setOnPreparedListener(this);

    mediaController = new MediaController(this);

    try {
      mediaPlayer.setDataSource(audioFile);
      mediaPlayer.prepare();
      mediaPlayer.start();
    } catch (IOException e) {
      Log.e(TAG, "Could not open file " + audioFile + " for playback.", e);
    }

  }

  @Override
  protected void onStop() {
    super.onStop();
    mediaController.hide();
    mediaPlayer.stop();
    mediaPlayer.release();
  }

  @Override
  public boolean onTouchEvent(MotionEvent event) {
    //the MediaController will hide after 3 seconds - tap the screen to make it appear again
    mediaController.show();
    return false;
  }

  //--MediaPlayerControl methods----------------------------------------------------
  public void start() {
    mediaPlayer.start();
  }

  public void pause() {
    mediaPlayer.pause();
  }

  public int getDuration() {
    return mediaPlayer.getDuration();
  }

  public int getCurrentPosition() {
    return mediaPlayer.getCurrentPosition();
  }

  public void seekTo(int i) {
    mediaPlayer.seekTo(i);
  }

  public boolean isPlaying() {
    return mediaPlayer.isPlaying();
  }

  public int getBufferPercentage() {
    return 0;
  }

  public boolean canPause() {
    return true;
  }

  public boolean canSeekBackward() {
    return true;
  }

  public boolean canSeekForward() {
    return true;
  }
  //--------------------------------------------------------------------------------

  public void onPrepared(MediaPlayer mediaPlayer) {
    Log.d(TAG, "onPrepared");
    mediaController.setMediaPlayer(this);
    mediaController.setAnchorView(findViewById(R.id.main_audio_view));

    handler.post(new Runnable() {
      public void run() {
        mediaController.setEnabled(true);
        mediaController.show();
      }
    });
  }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: convert string or string array to object mongoose object id 
Javascript :: regex not before 
Javascript :: javascript perms 
Javascript :: @testing-library/react-native switch 
Javascript :: JS uramy8e7jth6klryes8hrd8utduf6kgiyes48w7iy6rdjfcghe49u 
Javascript :: Uncaught SyntaxError: Unexpected end of JSON input at JSON.parse 
Javascript :: firebase iterate object 
Javascript :: birth day quote 
Javascript :: concatenate state with previous state in react redux 
Javascript :: editable pre input react 
Javascript :: https - node load testing- 
Javascript :: js convert charcode to char 
Javascript :: puppeeter pdf desktop 
Javascript :: reduce example 
Javascript :: nuxt js set background color of body 
Javascript :: for range a, z javascript 
Javascript :: extending classes javascript 
Javascript :: illegal start of expression spring boot 
Javascript :: truty values in javascript 
Javascript :: New year chaos solution 
Javascript :: minus converter 
Javascript :: detect paste in textarea 
Javascript :: Return Early Pattern for Functions 
Javascript :: what is fn extend 
Javascript :: New Syntax of Router Routes 
Javascript :: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace 
Javascript :: This shorthand syntax is also known as the concise method syntax. It’s valid to have spaces in the property name. 
Javascript :: How to Define a Function using Function Declaration in javascript 
Javascript :: createTextRange code example 
Javascript :: _.extend can be used to attach functions to a prototype like this 
ADD CONTENT
Topic
Content
Source link
Name
2+5 =