Playing a file in android mediaplayer

3.7k views Asked by At

I want to play a video file recorded to be played in the mediaplayer of android.I want to call the media player through the intent and want to play the corresponding file of the passed uri.When I was trying I am getting an exception ActivityNotFound can anyone help me with a code.

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        if (resultCode == RESULT_OK) {
            if (requestCode == REQUEST_VIDEO_CAPTURED) {
                uriVideo = data.getData();
                Toast.makeText(VedioRecording.this, uriVideo.getPath(),
                        Toast.LENGTH_LONG).show();
            }
        } else if (resultCode == RESULT_CANCELED) {
            uriVideo = null;
            Toast.makeText(VedioRecording.this, "Cancelled!", Toast.LENGTH_LONG)
                    .show();
        }
        if (requestCode == 2) {
            selectedImageUri = data.getData();

            // OI FILE Manager
            filemanagerstring = selectedImageUri.getPath();

            // MEDIA GALLERY
            selectedImagePath = getPath(selectedImageUri);

            Intent intent1 = new Intent(android.provider.MediaStore.INTENT_ACTION_MUSIC_PLAYER).setData(selectedImageUri);

            startActivityForResult(intent1, 3);
//          videoviewPlay.setVideoURI(selectedImageUri);
//          videoviewPlay.start();
        }
        if (requestCode == 3) {

        }
    }


    private String getPath(Uri uri) {
        String[] projection = { MediaStore.Video.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if (cursor != null) {
            // HERE YOU WILL GET A NULLPOINTER IF CURSOR IS NULL
            // THIS CAN BE, IF YOU USED OI FILE MANAGER FOR PICKING THE MEDIA
            int column_index = cursor
                    .getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        } else {
            return null;
        }
    }
}

this is my code i am geting activitynotfound exception

2

There are 2 answers

2
Rich On BEST ANSWER

The most common scenario in which you get an ActivityNotFound exception is when you attempt to launch an Activity you have created without declaring it in the manifest.

Post your code that you use to launch the Activity to be sure. If you're trying to use an activity that should be provided by the framework externally from your application, you may just be setting up the Intent incorrectly

Update after code posted...

Your code seems to be using the intent action INTENT_ACTION_MUSIC_PLAYER and passing an image url as data (is it the path to an image or are your variables just misnamed?). You get an ActivityNotFoundException because the system doesn't have any intent receivers registered to handle that scenario. Also, if you look at the documentation for this constant, you'll see that they marked it deprecated at some point:

http://developer.android.com/reference/android/provider/MediaStore.html#INTENT_ACTION_MUSIC_PLAYER

I would normally use Intent.ACTION_VIEW and pass a mime type along with the data. Something like the following...

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(pathToVideoFile), "video/*");
startActivity(intent);

By passing the mime type of "video/*" to setDataAndType, you're being more specific with your request to the system.

If you want to query the system to find out if an Intent can be handled (meaning that the user's device running your code has an Activity registered that can handle the Intent), you can use the PackageManager.queryIntentActivities method:

queryIntentActivities

0
Sreedev On
String extension = MimeTypeMap
                        .getFileExtensionFromUrl(selectedImagePath);
                String mimeType = MimeTypeMap.getSingleton()
                        .getMimeTypeFromExtension(extension);
                Intent mediaIntent = new Intent(Intent.ACTION_VIEW);
                mediaIntent.setDataAndType(Uri.parse(selectedImagePath),
                        mimeType);
                startActivity(mediaIntent);

this is the code that helped me