Im using Eclipse Android using MediaPlayer class to play video a file but im getting error on my and

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
The error on my phone say " Cannot play video" and under it " Sorry, this video cannot be played."
Now on my phone i had .3gp files so i used ap rogram to convert one of them to .mp4 but still im getting this message.
On my phone the directory to the files to the media is: F:DCIM100MEDIA and the file name is: VIDEO0030.mp4

This is the code main code PlayingVideo.java


<div style="color:Black;background-color:White; <pre>
package com.lightcone.playingvideo;

import java.io.File;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.os.Environment;
import android.view.MotionEvent;
import android.widget.VideoView;

<span style="color:Blue; public <span style="color:Blue; class PlayingVideo extends Activity implements OnCompletionListener, OnPreparedListener {

<span style="color:Blue; static <span style="color:Blue; private final String pathToFile = <span style="color:#A31515; "DCIM/100MEDIA/VIDEO0030.mp4"; <span style="color:Green; // Video source file
<span style="color:Blue; private VideoView videoPlayer;

<span style="color:Green; /** Called when the activity is first created. */
@Override
<span style="color:Blue; public <span style="color:Blue; void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

<span style="color:Green; // Find the root of the external storage file system. We assume the file system is
<span style="color:Green; // mounted and writable (see the project WriteSDCard for ways to check this).

File root = Environment.getExternalStorageDirectory();

<span style="color:Green; // Assign a VideoView object to the video player and set its properties. It
<span style="color:Green; // will be started by the onPrepared(MediaPlayer vp) callback below when the
<span style="color:Green; // file is ready to play.

videoPlayer = (VideoView) findViewById(R.id.videoPlayer);
videoPlayer.setOnPreparedListener(<span style="color:Blue; this);
videoPlayer.setOnCompletionListener(<span style="color:Blue; this);
videoPlayer.setKeepScreenOn(<span style="color:Blue; true);
videoPlayer.setVideoPath(root + <span style="color:#A31515; "/" + pathToFile);
}

<span style="color:Green; /** This callback will be invoked when the file is ready to play */
@Override
<span style="color:Blue; public <span style="color:Blue; void onPrepared(MediaPlayer vp) {

<span style="color:Green; // Dont start until ready to play. The arg of seekTo(arg) is the start point in
<span style="color:Green; // milliseconds from the beginning. In this example we start playing 1/5 of
<span style="color:Green; // the way through the video if the player can do forward seeks on the video.

<span style="color:Blue; if(videoPlayer.canSeekForward()) videoPlayer.seekTo(videoPlayer.getDuration()/5);
videoPlayer.start();
}

<span style="color:Green; /** This callback will be invoked when the file is finished playing */
@Override
<span style="color:Blue; public <span style="color:Blue; void onCompletion(MediaPlayer mp) {
<span style="color:Green; // Statements to be executed when the video finishes.
<span style="color:Blue; this.finish();
}

<span style="color:Green; /** Use screen touches to toggle the video between playing and paused. */
@Override
<span style="color:Blue; public boolean onTouchEvent (MotionEvent ev){
<span style="color:Blue; if(ev.getAction() == MotionEvent.ACTION_DOWN){
<span style="color:Blue; if(videoPlayer.isPlaying()){
videoPlayer.pause();
} <span style="color:Blue; else {
videoPlayer.start();
}
<span style="color:Blue; return <span style="color:Blue; true;
} <span style="color:Blue; else {
<span style="color:Blue; return <span style="color:Blue; false;
}
}
}

[/code]

Now i tried to change in the code some stuff with the root and with the pathtoFile string and change the directory but it didnt help.

The prokject is for android 2.2 wich what i have im using vs 2010 pro windows 7.

Cant figure out why it dosent play the video on my phone.


Thanks for help. <hr class="sig danieli

View the full article
 
Back
Top