Video playing in gallery but not app - android

I'm using this piece of code to try and open a .mp4 video:
VideoView videoView=(VideoView)findViewById(R.id.videoView1);
MediaController mediaController=new MediaController(this);
mediaController.setAnchorView(videoView) ;
videoView.setVideoPath("R.raw.videoname");
videoView.setMediaController(mediaController) ;
videoView.start();
However, whenever I try to run the app, I get a message saying "Can't play this video". I am using a new Nexus 7 tablet if that means anything.
Also, when I try to open the same file that I have stored in with my movies, the video runs perfectly normally when I use Gallery or Video Player to open it.
Any help is much appreciated.

File file = new File(getFilePath());//file path of your video
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);
if (type == null)
type = "*/*";
Uri uri = Uri.parse("www.google.com");
Intent type_intent = new Intent(Intent.ACTION_VIEW, uri);
Uri data = Uri.fromFile(file);
type_intent.setDataAndType(data, type);
startActivity(type_intent);

Make sure there will be no space in video name, otherwise it will not
be played by android VideoView.
If not solved then let me know.

Related

Exoplayer local path with Question Mark

I am developing music application. I have been using exoplayer to play music. I have lived a problem and couldn't find any solution. My problem is that ı have downloaded song file from internet. This song name contains "?" mark. I create path for this song file and use its name. After song downloaded successfully. I play with exoplayer. I create path for this song and set to exoplayer but it couldn't be played.
Song path = /storage/emulated/0/mymusic/downloads/How am I?_009.mp3
Uri uri = Uri.parse(path);
MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(uri, null, null);
exoPlayer.prepare(mediaSource);
I don't want replace "?" with any character.
How can I play?
Anybody help?
Thanks
you might also want to use something like this if youre planning on playing both offline and online music:
MediaSource mediaSource = new ExtractorMediaSource
.Factory(dataSourceFactory)
.createMediaSource(
isOnlinePath(path) ? Uri.parse(path) : Uri.fromFile(new File(path))
, null, null);
exoPlayer.prepare(mediaSource);
boolean isOnlinePath(String path) {
return path != null &&
(path.contains("http") ||
path.contains(".m3u") ||
path.contains(".pls"));
}

Android check for valid video file

How to check a video file is valid or not without checking its extension(.mp3 or .3gp etc). Means how to check the video file on SD card is supported by device or not?
Is there any api to validate video file in android 4.0 and above?
My Scenario: I am playing video on VideoView after downloading it and play it from local SD card after download success. Next time when a request for same video, then checks in SD card, if found then start playing it(No download in this case). But sometimes network error or app kill interrupt the downloading(in this case the video file is not completely downloaded) so the downloaded file is corrupted and VideoView is unable to play this file. So how to detect this corrupted file.
Here is the code that worked for me:
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
retriever.setDataSource(context, Uri.fromFile(fileToTest));
String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
boolean isVideo = "yes".equals(hasVideo);
#Alex had given right answer but still some problems are there as like #Kirill mention in comment that setDataSource often throws java.lang.RuntimeException: setDataSource failed exception. So Here is the function check for valid video file
private boolean videoFileIsCorrupted(String path){
MediaMetadataRetriever retriever = new MediaMetadataRetriever();
try {
retriever.setDataSource(myContext, Uri.parse(path));
}catch (Exception e){
e.printStackTrace();
return false;
}
String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
return "yes".equals(hasVideo);
}
I think this will be useful, The method is an older one that i used for other purpose just modified a bit for your use, Try it ,May work
private void checkAndLoadFile(
File currentFile) {
String path = currentFile.getAbsolutePath();
String extension = Utility.getExtension(path);
MimeTypeMap mimeMap = MimeTypeMap.getSingleton();
if(mimeMap.hasExtension(extension))
{
String mimeType = mimeMap.getMimeTypeFromExtension(extension);
Intent viewFile = new Intent(Intent.ACTION_VIEW);
viewFile.setDataAndType(Uri.fromFile(currentFile), mimeType);
PackageManager pm = getPackageManager();
List<ResolveInfo> apps =
pm.queryIntentActivities(viewFile, PackageManager.MATCH_DEFAULT_ONLY);
if (apps.size() > 0)
startActivity(viewFile); //This video is supported and there are apps installed in this device to open the video
else
showAsNotSupported();
} else
showAsNotSupported();
}

How to play videos in sd card

Am trying to play videos which i have in my sd card but it is showing an error that video cannot be played.. My code is
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
mVideoView = (VideoView) findViewById(R.id.surface_view);
mPath = (EditText) findViewById(R.id.path);
mPath.setText("mnt/sdcard/music/jelly jamm.3gp");
Can anyone help me.. I tried all the posts in stack overflow but still getting same error
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri imgUri = Uri.parse("file://mnt/sdcard/music/jelly jamm.3gp");
intent.setDataAndType(imgUri, "video/mp4");
startActivity(intent);
try like this. Hope this will give you some solution.
Use this code to play video from sdcard
VideoView vv = (VideoView)findViewById(R.id.videoView1);
File root;
root = Environment.getExternalStorageDirectory();
Log.d("FILEPATH1", root.toString());
String path =root.toString()+"/music/jelly jamm.3gp";
MediaController mc= new MediaController(getApplicationContext());
vv.setVideoPath(path);
vv.setMediaController(mc);
vv.requestFocus();
vv.start();
If you post the error you are getting we can help you...
May be your video is incompatible with Android. Try it with a different video. like any .mp4 video definitely works with Android. If that video works, and yours does not, then your video is not compatible with Android.
and do test this on a device. Video playback on the emulator requires too much power.

Recorded Audio using Native Audio Recorder Issue

I am recording audio using Audio recorder (native app) of android.Using following code :
final Intent audioIntent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
String path = Environment.getExternalStorageDirectory()
.toString();
File AudioFolder = new File(path + "/MyFolder/",
"RecordedAudio");
File audioFile = new File(AudioFolder, "myaudio.mp4");
Uri audioFileUri = Uri.fromFile(audioFile);
audioIntent.putExtra(MediaStore.EXTRA_OUTPUT, audioFileUri);
startActivityForResult(audioIntent, 0);
Audio recording is working fine with me. After successful recording It has been stored default in sdcard. I am not able to store it with my particular folder "MyFolder" that is located in sdcard so It might be store in /sdcard/Myfolder/RecordedAudio/myaudio.mp4.
What is going wrong with my above code.?
Please help me with your suggestions.
Thanks

How to know the length of an audio recording in Android

I am using the following code to record audio:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("audio/*");
startActivityForResult(Intent.createChooser(intent, "Select audio source"), CardList.ACTIVITY_RECDAUDIO);
When the result comes back I do the following:
Uri u = intent.getData();
String audioUri = u.getPath();
InputStream in = new BufferedInputStream(this.getContentResolver().openInputStream(u));
I would like to know how long the recording is in seconds. Is it possible to query this somehow? If all else fails I can play the clip programatically and time it, but I would prefer a more direct method if possible. Thanks!
I don't know if this is fast enough for you. But in case you don't know - you don't have to actually play it. It is enough to create MediaPlayer instance and set the path of the file and the call getDuration().
MediaPlayer mp = MediaPlayer.create(yourActivity, Uri.parse(path));
int duration = mp.getDuration();
I do this by using my input stream: is.available() on InputStream is gives me the length.

Categories

Resources