cannot play video

All your problems with Audio, Video and Images.

cannot play video

Postby meg@tron » Sun Jun 13, 2010 6:02 am

Hi

I am new to android . I am trying to make a video player that run files both from the sdcard as well as the URL .
The video is not being played . I am using android 2.1

The code is as follows :-

package com.android.MyVideoPlayer;

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.Context;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.os.Bundle;
import android.util.Log;
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.Toast;
import android.widget.VideoView;

public class MyVideoPlayer extends Activity {
/** Called when the activity is first created. */
private static final String TAG="MyVideoPlayer";
private VideoView mVideoView;
private EditText mpath;
private Button mplay;
private Button mpause;
private Button mstop;
private Button mreset;
private String current;
private Context context;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
setContentView(R.layout.main);
mVideoView=(VideoView)findViewById(R.id.VideoView01);
mpath=(EditText)findViewById(R.id.EditText01);
mpath.setText("http://daily3gp.com/vids/747.3gp");
mplay=(Button)findViewById(R.id.Button01);
mstop=(Button)findViewById(R.id.Button04);
mreset=(Button)findViewById(R.id.Button03);
mpause=(Button)findViewById(R.id.Button02);

mplay.setOnClickListener(new OnClickListener () {
public void onClick(View view)
{
playVideo();
}
});

mpause.setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
if(mVideoView!=null)
mVideoView.pause();
}
});

mreset.setOnClickListener(new OnClickListener(){
public void onClick(View view)
{
if(mVideoView!=null)
{
mVideoView.seekTo(0);
}

}
});

mstop.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
if(mVideoView!=null)
{
current=null;
mVideoView.stopPlayback();
}
}
});

runOnUiThread(new Runnable()
{
public void run()
{
playVideo();
}
});

}

private void playVideo()
{
try
{
final String path=mpath.getText().toString();
Log.v(TAG,"path: "+path);

//AssetFileDescriptor afd=
// context.getResources().openRawResourceFd(resId);

// set.DataSource(afd.getFileDescriptor(),
// afd.getStartOffset(),
// afd.getLength());

// afd.close();


if(path==null || path.length()==0)
{
Toast.makeText(MyVideoPlayer.this, "File URL/path is empty", Toast.LENGTH_LONG).show();
}
else
{
if(path.equals(current) & mVideoView!=null)
{
mVideoView.start();
mVideoView.requestFocus();
return;
}
current=path;
mVideoView.setVideoPath(getDataSource(path));
mVideoView.start();
mVideoView.requestFocus();
}
}catch(Exception f)
{
Log.e(TAG,"error: "+f.getMessage(),f);
if(mVideoView!=null)
mVideoView.stopPlayback();
}
}

private String getDataSource(String path) throws IOException
{
if(!URLUtil.isNetworkUrl(path))
{
return path;
}
else
{
URL url=new URL(path);
URLConnection cn=url.openConnection();
cn.connect();
InputStream stream=cn.getInputStream();
if(stream==null)
{
throw new RuntimeException("string is null");
}
File temp=File.createTempFile("mediaplayertmp", "dat");
temp.deleteOnExit();
String tempPath=temp.getAbsolutePath();
FileOutputStream out=new FileOutputStream(temp);
byte buff[]=new byte[128];
do
{
int numread=stream.read(buff);
if(numread<=0)
{
break;
}
out.write(buff,0,numread);
}while(true);
try
{
stream.close();
}catch(IOException ex)
{
Log.e(TAG,"error : "+ex.getMessage(),ex);
}
return tempPath;
}
}
}




The XML file is :-

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+id/RelativeLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"><EditText android:text="@+id/EditText01" android:id="@+id/EditText01" android:layout_height="wrap_content" android:layout_width="fill_parent"></EditText>
<VideoView android:id="@+id/VideoView01" android:layout_below="@+id/EditText01" android:layout_width="wrap_content" android:layout_height="300px"></VideoView>

<RelativeLayout android:layout_below="@+id/VideoView01" android:layout_height="wrap_content" android:id="@+id/RelativeLayout02" android:layout_width="fill_parent"><Button android:layout_below="@+id/VideoView01" android:layout_height="wrap_content" android:id="@+id/Button01" android:text="Play" android:textStyle="bold" android:layout_width="70px" android:layout_marginTop="20px"></Button><Button android:layout_below="@+id/VideoView01" android:layout_height="wrap_content" android:id="@+id/Button02" android:layout_toRightOf="@+id/Button01" android:text="Pause" android:textStyle="bold" android:layout_width="70px" android:layout_marginTop="20px"></Button>
<Button android:layout_below="@+id/VideoView01" android:layout_height="wrap_content" android:id="@+id/Button03" android:layout_toRightOf="@+id/Button02" android:text="Reset" android:textStyle="bold" android:layout_width="70px" android:layout_marginTop="20px"></Button>
<Button android:layout_below="@+id/VideoView01" android:layout_height="wrap_content" android:id="@+id/Button04" android:layout_toRightOf="@+id/Button03" android:textStyle="bold" android:text="Stop" android:layout_width="70px" android:layout_marginTop="20px"></Button>
</RelativeLayout>

</RelativeLayout>



My Manifest file is as follows :-


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.MyVideoPlayer"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".MyVideoPlayer"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

</application>
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
</manifest>





Please some one help me out ...............


Regards ,

Abhijeet
meg@tron
Freshman
Freshman
 
Posts: 2
Joined: Sun Jun 13, 2010 5:00 am

Top

Re: cannot play video

Postby shubh » Mon Nov 15, 2010 1:17 pm

File (http://daily3gp.com/vids/747.3gp) which you want to play doesn't exist.

try with some else.
Thank in Advance
User avatar
shubh
Experienced Developer
Experienced Developer
 
Posts: 83
Joined: Tue May 18, 2010 7:17 am
Location: India

Re: cannot play video

Postby ramu_57 » Wed Nov 17, 2010 12:55 pm

hi
package voice.recorder;
import java.io.IOException;
import android.app.Activity;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class VoiceRecord extends Activity
{
Button b1,b2,b3,b4;
MediaRecorder recorder;
String path;
MediaPlayer mp;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

b1=(Button)this.findViewById(R.id.record);
b2=(Button)this.findViewById(R.id.stop);
b3=(Button)this.findViewById(R.id.pause);
b4=(Button)this.findViewById(R.id.play);
b1.setOnClickListener(recliste);
b2.setOnClickListener(stopliste);
b3.setOnClickListener(pauseliste);
b4.setOnClickListener(playliste);
}

private OnClickListener recliste = new OnClickListener()
{
@Override
public void onClick(View v1)
{
// TODO Auto-generated method stub

try {


Log.i("voice test", "start");
recorder = new MediaRecorder();
path="/sdcard/hello.wav";

Toast.makeText(getBaseContext(), "recording",Toast.LENGTH_SHORT).show();
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
recorder.setOutputFile(path);
recorder.prepare();
recorder.start();
// Toast.makeText(getBaseContext(), "recording",Toast.LENGTH_SHORT).show();

} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

};
private OnClickListener stopliste = new OnClickListener()
{
public void onClick(View v) {

// TODO Auto-generated method stub
try{
recorder.stop();
recorder.release();
Toast.makeText(getBaseContext(), "stoping", Toast.LENGTH_SHORT).show();
}
catch(Exception e){}
}
};
private OnClickListener playliste = new OnClickListener()
{
public void onClick(View v)
{

try{
mp = new MediaPlayer();
mp.setDataSource("sdcard/hello.wav");
mp.setAudioStreamType(AudioManager.MODE_NORMAL);
mp.prepare();
mp.start();
}
catch(Exception e){}
}
};

private OnClickListener pauseliste = new OnClickListener()
{
public void onClick(View v)
{

try{
mp.pause();
mp.setDataSource("sdcard/hello.wav");
mp.prepare();
mp.start();
Toast.makeText(getBaseContext(), "Reset the Audio", Toast.LENGTH_SHORT).show();
}
catch(Exception e){}
}
};

}



for this we have to create the 4 buttons
in our main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_marginTop="5px"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="AUDIO RECORDING"
android:layout_marginBottom="10px"/>
<Button
android:layout_marginLeft="10px"
android:id="@+id/record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Record Audio "
android:layout_marginBottom="10px"/>
<Button
android:layout_marginLeft="10px"
android:id="@+id/play"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Play Recorded "
android:layout_marginBottom="10px" />
<Button
android:layout_marginLeft="10px"
android:id="@+id/pause"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pause Audio "
android:layout_marginBottom="15px" />
<Button
android:layout_marginLeft="10px"
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop Record " />
</LinearLayout>
this program create a file in sdcard with name hello.wav when press the play buuton u will get the what ever u recorded voice.
ramu_57
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Nov 02, 2010 10:10 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 17 guests