Search code examples
androidandroid-mediaplayer

How to create the media controller for playing audio?


I have done code for playing audio by using the media controller. My code is working properly but some exceptions occurs while running the code.

  public class CallHistoryActivity extends Activity implements MediaPlayer.OnPreparedListener, MediaController.MediaPlayerControl{

    Button audioPlayer;

    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();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_call_history);
        audioPlayer = (Button)findViewById(R.id.audioPlayer);

    }

    public void audioplayer(View view)
    {

        String path =new String(Environment.getExternalStorageDirectory() + "/NewCallLogs/Yaendi Yaendi.mp3 ");
        Uri uri = Uri.parse(path);

        mediaPlayer = new MediaPlayer();

        mediaController = new MediaController(this);


        mediaPlayer.reset();
        try {
            mediaPlayer.setDataSource(String.valueOf(uri));

        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            mediaPlayer.setOnPreparedListener(this);
            mediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


public void stopPlayer(View view)
{
    mediaPlayer.stop();
    mediaController.hide();
    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;
    }

    @Override
    public int getAudioSessionId() {
        return 0;
    }
    //--------------------------------------------------------------------------------

    public void onPrepared(MediaPlayer mediaPlayer) {

        mediaPlayer.start();

        Log.d(TAG, "onPrepared");
        mediaController.setMediaPlayer((MediaController.MediaPlayerControl) this);
        mediaController.setAnchorView(findViewById(R.id.main_audio_view));

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

The exception shown in the logcat is shown below

    02-20 14:56:42.402 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 46
02-20 14:56:42.412 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 49
02-20 14:56:42.412 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-20 14:56:42.422 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-20 14:56:42.422 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 50
02-20 14:56:42.422 21410-21410/com.sey.newcalllogs E/IMGSRV: :0: PVRDRMOpen: TP3, ret = 52
02-20 14:56:45.442 21410-21410/com.sey.newcalllogs E/dalvikvm: Could not find class 'android.graphics.drawable.RippleDrawable', referenced from method android.support.v7.widget.AppCompatImageHelper.hasOverlappingRendering
02-20 14:56:53.402 21410-21421/com.sey.newcalllogs E/MediaPlayer: error (1, -2147483648)
02-20 14:56:56.072 21410-21421/com.sey.newcalllogs E/MediaPlayer: error (1, -2147483648)
02-20 14:56:57.602 21410-21421/com.sey.newcalllogs E/MediaPlayer: error (1, -2147483648)
02-20 14:56:58.792 21410-21410/com.sey.newcalllogs E/MediaPlayer: stop called in state 0
02-20 14:56:58.792 21410-21410/com.sey.newcalllogs E/MediaPlayer: error (-38, 0)

Then the layout design for the media controller is shown below

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_audio_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.seyali.newcalllogs.CallHistoryActivity">

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/audioPlayer"
    android:onClick="audioplayer"/>


<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/stopPlaying"
    android:onClick="stopPlayer"/>


<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Now playing:"
    android:textSize="25sp"
    android:textStyle="bold"
    />
<TextView
    android:id="@+id/now_playing_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dip"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:layout_gravity="center"
    android:text="Now playing.."
    android:textSize="16sp"
    android:textStyle="italic"/>

Please help me how to overcome this exception.I tried all the methods but my problem was not solved.Please help me.


Solution

  • MediaPlayer should follow a design. Please see : https://developer.android.com/reference/android/media/MediaPlayer.html

    mediaplayer.stop() should trigger from play state(after calling start).

    02-20 14:56:58.792 21410-21410/com.sey.newcalllogs E/MediaPlayer: stop called in state 0

    mediaPlayer.reset();
    mediaPlayer.setDataSource(String.valueOf(uri));
    mediaPlayer.prepare();
    
       public void onPrepared(MediaPlayer mediaPlayer) {
            mediaPlayer.start();
        }
    

    only after all these step you can call mediaPlayer.stop();

    The above error showing your calling stop from wrong state.Please check