Playing Video from remote server playing sound with an image

All your problems with Audio, Video and Images.

Playing Video from remote server playing sound with an image

Postby skasliw » Mon Sep 28, 2009 5:23 am

Hi
I am new to android
I was trying to play a video file (.wmv or .mp4)
but it is playing the sound and a still image instead of video.
Can anyone help me to solve this issue.

Here is my code



import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.content.res.AssetFileDescriptor;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnBufferingUpdateListener;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaPlayer.OnErrorListener;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.URLUtil;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.VideoView;

public class testVideo extends Activity implements OnErrorListener,
OnBufferingUpdateListener, OnCompletionListener,
MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
private final String TAG = "test";
private MediaPlayer mp;
private SurfaceView mPreview;
private EditText mPath;
private SurfaceHolder holder;
private ImageButton mPlay;
private ImageButton mPause;
private ImageButton mReset;
private ImageButton mStop;
private String current;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Set up the play/pause/reset/stop buttons
mPreview = (SurfaceView) findViewById(R.id.surface);
// mPath = (EditText) findViewById(R.id.btnleftarrow);
mPlay = (ImageButton) findViewById(R.id.image1);
mPause = (ImageButton) findViewById(R.id.btnleftarrow);
mReset = (ImageButton) findViewById(R.id.image2);
mStop = (ImageButton) findViewById(R.id.btnrightarrow);

mPlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
playVideo();
}
});
mPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.pause();
}
}
});
mReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.seekTo(0);
}
}
});
mStop.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mp != null) {
mp.stop();
mp.release();
}
}
});

// Set the transparency
getWindow().setFormat(PixelFormat.TRANSPARENT);

// Set a size for the video screen
holder = mPreview.getHolder();
holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
holder.addCallback(this);
holder.setFixedSize(400, 300);

}

private void playVideo() {
try {
final String path = "//data//data//1.mp4";
Log.v(TAG, "path: " + path);

// If the path has not changed, just start the media player
if (path.equals(current) && mp != null) {
mp.start();
return;
}
current = path;

// Create a new media player and set the listeners
mp = new MediaPlayer();
mp.setOnErrorListener(this);
mp.setOnBufferingUpdateListener(this);
mp.setOnCompletionListener(this);
mp.setOnPreparedListener(this);
mp.setAudioStreamType(2);

mp.setDisplay(holder);
Runnable r = new Runnable() {
public void run() {
try {
setDataSource(path);
mp.prepare();
Log.d(TAG, "mMediaPlayer.start is to be called");
mp.start();

// Log.v(TAG, "Duration: ===>" + mp.getDuration());

} catch (IOException e) {
Log.e(TAG, e.getMessage(), e);
}
}
};
new Thread(r).start();
} catch (Exception e) {
Log.e(TAG, "error: " + e.getMessage(), e);
if (mp != null) {
mp.stop();
mp.release();
}
}
}

/**
* If the user has specified a local url, then we download the url stream to
* a temporary location and then call the setDataSource for that local file
*
* @param path
* @throws IOException
*/
private void setDataSource(String path) throws IOException {

if (!URLUtil.isNetworkUrl(path)) {
mp.setDataSource(path);
} else {
URL url = new URL(path);
URLConnection cn = url.openConnection();
cn.connect();
InputStream stream = cn.getInputStream();
if (stream == null)
throw new RuntimeException("stream is null");
File temp = File.createTempFile("mediaplayertmp", "dat");
String tempPath = temp.getAbsolutePath();
FileOutputStream out = new FileOutputStream(temp);
byte buf[] = new byte[128];
do {
int numread = stream.read(buf);
if (numread <= 0)
break;
out.write(buf, 0, numread);
} while (true);

mp.setDataSource(tempPath);

try {
stream.close();
} catch (IOException ex) {
Log.e(TAG, "error: " + ex.getMessage(), ex);
}
}

}

public boolean onError(MediaPlayer mediaPlayer, int what, int extra) {
Log.e(TAG, "onError---> what:" + what + " extra:" + extra);
if (mediaPlayer != null) {
mediaPlayer.stop();
mediaPlayer.release();
}
return false;
}

public void onBufferingUpdate(MediaPlayer arg0, int percent) {
Log.d(TAG, "onBufferingUpdate called ---> percent:" + percent);
}

public void onCompletion(MediaPlayer arg0) {
Log.d(TAG, "onCompletion called");
}

public void onPrepared(MediaPlayer mediaplayer) {
Log.d(TAG, "onPrepared called");
}

public void surfaceCreated(SurfaceHolder surfaceholder) {
Log.d(TAG, "surfaceCreated called");

}

public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
Log.d(TAG, "surfaceChanged called");
}

public void surfaceDestroyed(SurfaceHolder surfaceholder) {
Log.d(TAG, "surfaceDestroyed called");
}
}
skasliw
Freshman
Freshman
 
Posts: 3
Joined: Sun Jan 18, 2009 4:01 pm

Top

Postby konic » Sun Oct 04, 2009 1:41 pm

XML file is needed to be able to run it.
BTW, there are buttons .java and <> (code) to post code in readable form.
konic
Developer
Developer
 
Posts: 32
Joined: Mon Aug 24, 2009 3:16 pm

Postby sudheer1437 » Sat Oct 17, 2009 8:31 pm

this problem will b only in emulator..
but in real device... video also plays...
dont worry...
sudheer1437
Experienced Developer
Experienced Developer
 
Posts: 79
Joined: Sat Aug 08, 2009 7:42 am
Location: India

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: Exabot [Bot] and 17 guests