How to Show a Mediacontroller While Playing Audio in Android

How to create the media controller for playing audio?

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

How to add MediaController to list of music with MediaPlayer?

You didn't attach MediaController!
try this:

//you can use any view like ListView (I mean listSongs) in this case instead of mView
controller.setAnchorView(mView);

MediaController doesn't seem to show up

Please check the below code for your refrence

In your code you have passed the TextView in the mediaController need to pass the SurfaceView instead of TextView and you need to set the MediaPlayer in the MediaController

JAVA file.

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

XML file-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main_audio_view" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_gravity="center"
android:orientation="vertical"> <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" /> </LinearLayout>

Android MediaController End Of song

So it turns out that show(0) after the audio has ended, re-displays the UI correctly. Thanks Cylon for the help!

MediaController play button not updating when song is playing

Figured it out. I need to call show again after start to update the buttons

mMediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
@Override
public void onPrepared(MediaPlayer mp) {
mp.start();
mMediaController.show(0);
}
});

How to implement an audio player for Android using MediaPlayer And MediaController?

you can look at those links :

http://www.helloandroid.com/tutorials/musicdroid-audio-player-part-i

Hope it will help.

[EDIT]

You have also some example on the official android developer website :

http://developer.android.com/guide/topics/media/index.html



Related Topics



Leave a reply



Submit