Open Mp3 Using Intent.Action_Open_Document, Action_Get_Content As Well As Action_Pick, Alongside Checking As Well As Requesting Permission At Runtime.

Posted on

Android instance to opened upward mp3 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT together with ACTION_PICK, alongside checking together with requesting permission at runtime.

This video demo how it run on Android Emulator of Android 6.0, Marshmallo. It tin dismiss live on noticed that inwards instance of ACTION_OPEN_DOCUMENT, the mp3 tin dismiss live on constitute merely non openned; I don’t know why currently! And if run on Android 6.0, accept to cheque together with asking permission at runtime, otherwise the mp3 cannot live on played.


MainActivity.java

package com.blogspot.android_er.androidplayer;  import android.Manifest; import android.content.Intent; import android.content.pm.PackageManager; import android.media.MediaPlayer; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import android.widget.SeekBar; import android.widget.TextView; import android.widget.Toast;  import java.io.IOException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit;  world course of teaching MainActivity extends AppCompatActivity {      TextView info, state;     Button btnOpenDocument, btnGetContent, btnPick;     Button buttonPlay, buttonPause, buttonStop;     SeekBar timeLine;     LinearLayout timeFrame;     TextView timePos, timeDur;      terminal static int RQS_OPEN_DOCUMENT = 1;     terminal static int RQS_GET_CONTENT = 2;     terminal static int RQS_PICK = 3;     terminal static int RQS_PERMISSION_READ_EXTERNAL_STORAGE = 4;      MediaPlayer mediaPlayer;     Uri audioFileUri = null;      enum MP_State {         Idle, Initialized, Prepared, Started, Paused,         Stopped, PlaybackCompleted, End, Error, Preparing     }      MP_State mediaPlayerState;      @Override     world void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btnOpenDocument = (Button)findViewById(R.id.opendocument);         btnGetContent = (Button)findViewById(R.id.getcontent);         btnPick = (Button)findViewById(R.id.pick);         btnOpenDocument.setOnClickListener(btnOpenDocumentOnClickListener);         btnGetContent.setOnClickListener(btnGetContentOnClickListener);         btnPick.setOnClickListener(btnPickOnClickListener);          information = (TextView) findViewById(R.id.info);         position down = (TextView) findViewById(R.id.state);          buttonPlay = (Button) findViewById(R.id.play);         buttonPlay.setOnClickListener(buttonPlayOnClickListener);         buttonPause = (Button) findViewById(R.id.pause);         buttonPause.setOnClickListener(buttonPauseOnClickListener);         buttonStop = (Button) findViewById(R.id.stop);         buttonStop.setOnClickListener(buttonStopOnClickListener);          //         timeLine = (SeekBar) findViewById(R.id.seekbartimeline);         timeFrame = (LinearLayout) findViewById(R.id.timeframe);         timePos = (TextView) findViewById(R.id.pos);         timeDur = (TextView) findViewById(R.id.dur);          ScheduledExecutorService myScheduledExecutorService                 = Executors.newScheduledThreadPool(1);          myScheduledExecutorService.scheduleWithFixedDelay(                 novel Runnable() {                     @Override                     world void run() {                         monitorHandler.sendMessage(monitorHandler.obtainMessage());                     }                 },                 200, //initialDelay                 200, //delay                 TimeUnit.MILLISECONDS);      }      Handler monitorHandler = novel Handler() {          @Override         world void handleMessage(Message msg) {             mediaPlayerMonitor();         }      };      mortal void mediaPlayerMonitor() {         if (mediaPlayer == null) {             timeLine.setVisibility(View.INVISIBLE);             timeFrame.setVisibility(View.INVISIBLE);         } else {             if (mediaPlayer.isPlaying()) {                 timeLine.setVisibility(View.VISIBLE);                 timeFrame.setVisibility(View.VISIBLE);                  int mediaDuration = mediaPlayer.getDuration();                 int mediaPosition = mediaPlayer.getCurrentPosition();                 timeLine.setMax(mediaDuration);                 timeLine.setProgress(mediaPosition);                 timePos.setText(String.valueOf((float) mediaPosition / 1000) + "s");                 timeDur.setText(String.valueOf((float) mediaDuration / 1000) + "s");             } else {                 timeLine.setVisibility(View.INVISIBLE);                 timeFrame.setVisibility(View.INVISIBLE);             }         }     }      MediaPlayer.OnErrorListener mediaPlayerOnErrorListener             = novel MediaPlayer.OnErrorListener() {          @Override         world boolean onError(MediaPlayer mp, int what, int extra) {             // TODO Auto-generated method stub              mediaPlayerState = MP_State.Error;             showMediaPlayerState();              render false;         }     };       mortal void cmdReset() {         if (mediaPlayer == null) {             mediaPlayer = novel MediaPlayer();             mediaPlayer.setOnErrorListener(mediaPlayerOnErrorListener);         }         mediaPlayer.reset();         mediaPlayerState = MP_State.Idle;         showMediaPlayerState();     }      mortal void cmdSetDataSource(Uri uri) {         if (mediaPlayerState == MP_State.Idle) {             crusade {                 mediaPlayer.setDataSource(MainActivity.this, uri);                 mediaPlayerState = MP_State.Initialized;             } select stimulate got of (IllegalArgumentException e) {                 Toast.makeText(MainActivity.this,                         e.toString(), Toast.LENGTH_LONG).show();                 e.printStackTrace();             } select stimulate got of (IllegalStateException e) {                 Toast.makeText(MainActivity.this,                         e.toString(), Toast.LENGTH_LONG).show();                 e.printStackTrace();             } select stimulate got of (IOException e) {                 Toast.makeText(MainActivity.this,                         e.toString(), Toast.LENGTH_LONG).show();                 e.printStackTrace();             }         } else {             Toast.makeText(MainActivity.this,                     "Invalid State@cmdSetDataSource - skip",                     Toast.LENGTH_LONG).show();         }          showMediaPlayerState();     }      mortal void cmdPrepare() {          if (mediaPlayerState == MP_State.Initialized                 || mediaPlayerState == MP_State.Stopped) {             crusade {                 mediaPlayer.prepare();                 mediaPlayerState = MP_State.Prepared;             } select stimulate got of (IllegalStateException e) {                 Toast.makeText(MainActivity.this,                         e.toString(), Toast.LENGTH_LONG).show();                 e.printStackTrace();             } select stimulate got of (IOException e) {                 Toast.makeText(MainActivity.this,                         e.toString(), Toast.LENGTH_LONG).show();                 e.printStackTrace();             }         } else {             Toast.makeText(MainActivity.this,                     "Invalid State@cmdPrepare() - skip",                     Toast.LENGTH_LONG).show();         }          showMediaPlayerState();     }      mortal void cmdStart() {         if (mediaPlayerState == MP_State.Prepared                 || mediaPlayerState == MP_State.Started                 || mediaPlayerState == MP_State.Paused                 || mediaPlayerState == MP_State.PlaybackCompleted) {             mediaPlayer.start();             mediaPlayerState = MP_State.Started;         } else {             Toast.makeText(MainActivity.this,                     "Invalid State@cmdStart() - skip",                     Toast.LENGTH_LONG).show();         }          showMediaPlayerState();     }      mortal void cmdPause() {         if (mediaPlayerState == MP_State.Started                 || mediaPlayerState == MP_State.Paused) {             mediaPlayer.pause();             mediaPlayerState = MP_State.Paused;         } else {             Toast.makeText(MainActivity.this,                     "Invalid State@cmdPause() - skip",                     Toast.LENGTH_LONG).show();         }         showMediaPlayerState();     }      mortal void cmdStop() {          if (mediaPlayerState == MP_State.Prepared                 || mediaPlayerState == MP_State.Started                 || mediaPlayerState == MP_State.Stopped                 || mediaPlayerState == MP_State.Paused                 || mediaPlayerState == MP_State.PlaybackCompleted) {             mediaPlayer.stop();             mediaPlayerState = MP_State.Stopped;         } else {             Toast.makeText(MainActivity.this,                     "Invalid State@cmdStop() - skip",                     Toast.LENGTH_LONG).show();         }         showMediaPlayerState();      }      mortal void showMediaPlayerState() {          switch (mediaPlayerState) {             instance Idle:                 state.setText("Idle");                 break;             instance Initialized:                 state.setText("Initialized");                 break;             instance Prepared:                 state.setText("Prepared");                 break;             instance Started:                 state.setText("Started");                 break;             instance Paused:                 state.setText("Paused");                 break;             instance Stopped:                 state.setText("Stopped");                 break;             instance PlaybackCompleted:                 state.setText("PlaybackCompleted");                 break;             instance End:                 state.setText("End");                 break;             instance Error:                 state.setText("Error");                 break;             instance Preparing:                 state.setText("Preparing");                 break;             default:                 state.setText("Unknown!");         }     }      View.OnClickListener buttonPlayOnClickListener             = novel View.OnClickListener() {          @Override         world void onClick(View v) {              if (audioFileUri == null) {                 Toast.makeText(MainActivity.this,                         "No file selected",                         Toast.LENGTH_LONG).show();             } else {                 cmdPrepare();                 cmdStart();             }          }      };      View.OnClickListener buttonPauseOnClickListener             = novel View.OnClickListener() {          @Override         world void onClick(View v) {             cmdPause();         }      };      View.OnClickListener buttonStopOnClickListener             = novel View.OnClickListener() {          @Override         world void onClick(View v) {              cmdStop();          }      };      View.OnClickListener btnOpenDocumentOnClickListener = novel View.OnClickListener(){          @Override         world void onClick(View v) {             Intent intent = novel Intent();             intent.setAction(Intent.ACTION_OPEN_DOCUMENT);             intent.addCategory(Intent.CATEGORY_OPENABLE);             intent.setType("audio/mp3");             startActivityForResult(                     Intent.createChooser(intent, "ACTION_OPEN_DOCUMENT"),                     RQS_OPEN_DOCUMENT);         }     };      View.OnClickListener btnGetContentOnClickListener = novel View.OnClickListener(){          @Override         world void onClick(View v) {             Intent intent = novel Intent();             intent.setType("audio/mp3");             intent.setAction(Intent.ACTION_GET_CONTENT);             startActivityForResult(Intent.createChooser(                     intent, "ACTION_GET_CONTENT"), RQS_GET_CONTENT);         }     };      View.OnClickListener btnPickOnClickListener = novel View.OnClickListener(){          @Override         world void onClick(View v) {             Intent intent = novel Intent(Intent.ACTION_PICK,                     android.provider.MediaStore.Audio.Media.EXTERNAL_CONTENT_URI);             startActivityForResult(                     Intent.createChooser(intent, "ACTION_PICK"),                     RQS_PICK);         }     };      @Override     protected void onActivityResult(int requestCode, int resultCode, Intent data) {         if (resultCode == RESULT_OK) {             if(requestCode == RQS_OPEN_DOCUMENT                     || requestCode == RQS_GET_CONTENT                     || requestCode == RQS_PICK){                 audioFileUri = data.getData();                  info.setText(audioFileUri.toString());                  //Check together with asking Permission at runtime                 if (ActivityCompat.checkSelfPermission(this,                         Manifest.permission.READ_EXTERNAL_STORAGE)                         != PackageManager.PERMISSION_GRANTED) {                      ActivityCompat.requestPermissions(MainActivity.this,                             novel String[]{Manifest.permission.READ_EXTERNAL_STORAGE},                             RQS_PERMISSION_READ_EXTERNAL_STORAGE);                      return;                 }                  cmdReset();                 cmdSetDataSource(audioFileUri);             }         }     }      @Override     world void onRequestPermissionsResult(             int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {         switch (requestCode) {             instance RQS_PERMISSION_READ_EXTERNAL_STORAGE: {                 // If asking is cancelled, the lawsuit arrays are empty.                 if (grantResults.length > 0                         && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                     Toast.makeText(MainActivity.this,                             "permission was granted, :)",                             Toast.LENGTH_LONG).show();                      cmdReset();                     cmdSetDataSource(audioFileUri);                 } else {                     Toast.makeText(MainActivity.this,                             "permission denied, ...:(",                             Toast.LENGTH_LONG).show();                 }                 return;             }         }     } }  

layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical"     android:padding="16dp"     tools:context="com.blogspot.android_er.androidplayer.MainActivity">      <TextView         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center_horizontal"         android:autoLink="web"         android:text="https://rootandroid.org//"         android:textStyle="bold" />      <Button         android:id="@+id/opendocument"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="ACTION_OPEN_DOCUMENT" />      <Button         android:id="@+id/getcontent"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="ACTION_GET_CONTENT" />      <Button         android:id="@+id/pick"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="ACTION_PICK" />      <TextView         android:id="@+id/info"         android:layout_width="fill_parent"         android:layout_height="wrap_content" />      <Button         android:id="@+id/play"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="Play" />      <Button         android:id="@+id/pause"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="Pause" />      <Button         android:id="@+id/stop"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="Stop" />      <TextView         android:id="@+id/state"         android:layout_width="fill_parent"         android:layout_height="wrap_content" />      <SeekBar         android:id="@+id/seekbartimeline"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:visibility="invisible" />      <LinearLayout         android:id="@+id/timeframe"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:orientation="horizontal"         android:visibility="invisible">          <TextView             android:id="@+id/pos"             android:layout_width="wrap_content"             android:layout_height="wrap_content" />          <TextView             android:id="@+id/dur"             android:layout_width="match_parent"             android:layout_height="wrap_content"             android:gravity="right" />     </LinearLayout>  </LinearLayout>  

uses-permission of “android.permission.READ_EXTERNAL_STORAGE” is needed inwards AndroidManifest.xml.

 alongside checking together with requesting permission at runtime Open mp3 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT together with ACTION_PICK, alongside checking together with requesting permission at runtime.Download the files .

Related:
– Open mp4 using Intent.ACTION_OPEN_DOCUMENT, ACTION_GET_CONTENT together with ACTION_PICK, together with play inwards VideoView.