views:

517

answers:

3

I want media controls such as play/pause for streaming audio that I am playing in my app. I am using MediaPlayer to stream and play the audio.

Can someone provide a code snippet on how to use MediaController with MediaPlayer?

Thanks Chris

A: 

As easy as this code extracted from here. They use VideoView instead of a MediaPlayer, which saves you a few lines of code:

Layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout android:id="@+id/LinearLayout01"
    android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"
    android:paddingLeft="2px" android:paddingRight="2px"
    android:paddingTop="2px" android:paddingBottom="2px"
    android:layout_width="fill_parent" android:orientation="vertical">

    <VideoView android:layout_height="fill_parent"
        android:layout_width="fill_parent" android:id="@+id/VideoView"></VideoView>

</LinearLayout>

And code:

public class VideoPlayerController extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.video);

        VideoView videoView = (VideoView) findViewById(R.id.VideoView);
        MediaController mediaController = new MediaController(this);
        mediaController.setAnchorView(videoView);
// Set video link (mp4 format )
        Uri video = Uri.parse("mp4 video link");
        videoView.setMediaController(mediaController);
        videoView.setVideoURI(video);
        videoView.start();

    }
}

That's all. Just replace mp4 video link with a streaming video url.

Fernando Miguélez
Use VideoView for audio???
Chris
A: 

It actually worked. I created a VideoView and gave it the path of the audio file in setVideoPath. And added the MediaController. :-)

Chris
A: 

VideoView can use mediaplayer but Videoview has problem about full screen video MediaPlayer has full screen but dont have mediacontroller I dont now what can I do

ummani