Monday, 5 March 2012

Start activity to play MediaStore.Video.Media by intent with action of Intent.ACTION_VIEW

In last exercise, we can "Retrieve playable Uri from MediaStore". In this exercise, we change target media source from MediaStore.Audio.Media.EXTERNAL_CONTENT_URI to MediaStore.Video.Media.EXTERNAL_CONTENT_URI, to retrieve media of video. We can play it using build-in media play app by starting activity with intent of Intent.ACTION_VIEW, with the retrieved playable Uri.


package com.exercise.AndroidListMedia;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class AndroidListMediaActivity extends ListActivity {

SimpleCursorAdapter adapter;

final Uri mediaSrc = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] from = {
MediaStore.MediaColumns.TITLE};
int[] to = {
android.R.id.text1};

Cursor cursor = managedQuery(
mediaSrc,
null,
null,
null,
MediaStore.Audio.Media.TITLE);

adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1, cursor, from, to);
setListAdapter(adapter);
}

@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(position);

String _id = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media._ID));

Uri playableUri
= Uri.withAppendedPath(mediaSrc, _id);

Toast.makeText(this, "Play: " + playableUri.toString(), Toast.LENGTH_LONG).show();

Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(playableUri);
startActivity(intent);
}


}


Download the files.


Next:
- Play media on surfaceview using android.media.MediaPlayer

Borneo08

About Borneo08

Author Description here.. Nulla sagittis convallis. Curabitur consequat. Quisque metus enim, venenatis fermentum, mollis in, porta et, nibh. Duis vulputate elit in elit. Mauris dictum libero id justo.

Subscribe to this Blog via Email :

More links

Related Posts Plugin for WordPress, Blogger...