I wrote a Media Player in my app and wrote this code below:
mediaPlayer = MediaPlayer.create(this, R.raw.file);
I want to give my mediaplayer this String path instead of a uri:
String path = "http://example.org/file.mp3";
How can I do that?
Guys Please answer my question with some code!!!
Thank you so much!
Uri uri = Uri.parse("http://example.org/file.mp3");
player = new MediaPlayer();
player.setDataSource(context, uri);
player.start();
Related
My app package name is com.example.app.a, I try to play the mp4 resource from application b com.example.app.b like this:
VideoView videoView = (VideoView) findViewById(R.id.picker);
videoView.setVideoURI(Uri.parse("android.resource://com.example.app.b/raw/keyboard_anim_theme"));
videoView.requestFocus();
videoView.start();
but the Can't play this video dialog shows. How can I play the mp4 in another application. Thanks a lot.
Have you tried with getIdentifier ?
check below code and check.
int rawId = getResources().getIdentifier("keyboard_anim_theme", "raw", "com.example.app.b");
String path = "android.resource://com.example.app.b/" + rawId;
videoView.requestFocus();
videoView.start();
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.
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.
Please tell me why this would work on with MediaPLayer and not in videoView? And how to make it work with a video view?
Videos are downloaded form an API and saved in this folder I created:
File mediadir = cw.getDir("tvr", Context.MODE_PRIVATE);
VideoView
final Uri uri = Uri.parse(path);
// path = /data/data/com.foo.app/tvr/video.mp4
videoView = (VideoView) findViewById(R.id.videoView);
videoView.setVisibility(View.VISIBLE);
videoView.setOnCompletionListener(this);
videoView.setVideoURI(Uri.parse(path));
videoView.start();
Error VideoView Sorry, this Video cannot be player and error (1, -2...)
MediaPlayer --- THIS WORKS
FileInputStream fileInputStream = new FileInputStream(path);
MediaPlayer pl = new MediaPlayer();
pl.setDataSource(fileInputStream.getFD());
pl.prepare();
pl.start();
The basic reason is the MODE_PRIVATE, which disallow VideoView and MediaPlayer from playing non-World Readable files, unless you pass the FD as you have.
Here's a more detailed explanation
I'm using MediaRecorder class to record an audio file and I used
final String MEDIA_OUTPUT_FILE = "MyOutPutFile";
mediaRecorder.setOutputFile(MEDIA_OUTPUT_FILE);'
to give a name to my output file.
I also know how to Play an Audio File from Resources:
MediaPlayer mpRes = MediaPlayer.create(getApplicationContext(), R.raw.audiofile);
mpRes.start();'
My Question Is, Now, how to play the recorded file while I dont know where my MEDIA_OUTPUT_FILE is saved?
Instead of using constant as argument for mediaRecorder.setOutputFile() use a variable like below.
private String filePath;
filePath = Environment.getExternalStorageDirectory().getAbsolutePath();
filePath += "/myrecording.mpeg";
Then pass the filePath variable as the argument as follow;
mediaRecorder.setOutputFile(filePath);
Then you can play the recorded file as follows;
mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource(filePath);
mediaPlayer.start();
That's it.