How to play video file repeatly in VideoView?

All your problems with Audio, Video and Images.

How to play video file repeatly in VideoView?

Postby perrot » Tue Feb 09, 2010 5:51 am

The snippet code to play *.wmv video file:

VideoView vv = (VideoView) findViewById(R.id.videoview_main);
Log.e("debug","set video path...");
vv.setVideoPath(path);

Log.e("debug","set request focus...");
vv.requestFocus();
Log.e("debug","show context menu...");
vv.showContextMenu();
Log.e("debug","start...");
vv.start();

But there is no function to set repeat.
Does somebody know about this?
Please give me a hint. Thank you
perrot
Junior Developer
Junior Developer
 
Posts: 15
Joined: Fri Jan 22, 2010 3:21 am
Location: China

Top

Postby azraeal » Tue Feb 09, 2010 11:09 pm

Implement an onCompletionListener and have its action to restart the video.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3. myActivity implements onCompletionListener
  4.  
  5.  
  6.  
  7. public void onCreate(...){
  8.  
  9. ...
  10.  
  11. (your code here?)
  12.  
  13.  vv.setOnCompletionListener  (this)
  14.  
  15.  
  16.  
  17. ...
  18.  
  19. }
  20.  
  21. public void  onCompletion(MediaPlayer mp){
  22.  
  23. mp.start();
  24.  
  25. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby perrot » Wed Feb 10, 2010 3:17 am

azraeal wrote:Implement an onCompletionListener and have its action to restart the video.


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. myActivity implements onCompletionListener
  3.  
  4. public void onCreate(...){
  5. ...
  6. (your code here?)
  7.  vv.setOnCompletionListener  (this)
  8.  
  9. ...
  10. }
  11. public void  onCompletion(MediaPlayer mp){
  12. mp.start();
  13.  
  14.  
  15. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



Hi azraeal,
Thank you for your help.
I have solved the problem. But you must seek to 0 time position before start again.
The snippet code like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. vv.setOnCompletionListener(new OnCompletionListener(){
  3.  
  4.  
  5.  
  6.                 // @Override
  7.  
  8.                 public void onCompletion(MediaPlayer arg0) {
  9.  
  10.                      // File has ended !!!
  11.  
  12.                         try
  13.  
  14.                        
  15.  
  16.                         {
  17.  
  18.                                 Log.e("debug","MediaPlayer seek to 0...");
  19.  
  20.                                 arg0.seekTo(0);
  21.  
  22.                                 Log.e("debug","MediaPlayer start...");
  23.  
  24.                                 arg0.start();
  25.  
  26.                                 Log.e("debug","MediaPlayer started");
  27.  
  28.                         }
  29.  
  30.                         catch(Exception ex)
  31.  
  32.                         {
  33.  
  34.                                 Log.e("debug","MediaPlayer error: "+ex.toString());
  35.  
  36.                         }
  37.  
  38.                 }
  39.  
  40.            });
  41.  
  42.  
Parsed in 0.037 seconds, using GeSHi 1.0.8.4
perrot
Junior Developer
Junior Developer
 
Posts: 15
Joined: Fri Jan 22, 2010 3:21 am
Location: China

Postby nbagade » Wed Feb 10, 2010 10:30 am

I am trying as u describe but getting so many error .

can u provide details code as I am new to android dont have much idea on this


Thanks,
Narendra
nbagade
Experienced Developer
Experienced Developer
 
Posts: 58
Joined: Fri Nov 20, 2009 7:49 am
Location: India

Postby perrot » Thu Feb 11, 2010 2:17 am

nbagade wrote:I am trying as u describe but getting so many error .

can u provide details code as I am new to android dont have much idea on this


Thanks,
Narendra


Hi, nbagade

This is the full snippet:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.demo.android.autotest;
  2.  
  3.  
  4.  
  5. import android.app.Activity;
  6.  
  7. import android.content.Intent;
  8.  
  9. import android.media.AudioManager;
  10.  
  11. import android.media.MediaPlayer;
  12.  
  13. import android.media.MediaPlayer.OnCompletionListener;
  14.  
  15. import android.os.Bundle;
  16.  
  17. import android.util.Log;
  18.  
  19. import android.widget.MediaController;
  20.  
  21. import android.widget.TextView;
  22.  
  23. import android.widget.VideoView;
  24.  
  25.  
  26.  
  27. public class PlayMedia extends Activity {
  28.  
  29.        
  30.  
  31.         private TextView _tv=null;
  32.  
  33.         private String _moduleName="PlayMedia";
  34.  
  35.        
  36.  
  37.     /** Called when the activity is first created. */
  38.  
  39.     @Override
  40.  
  41.     public void onCreate(Bundle savedInstanceState) {
  42.  
  43.         super.onCreate(savedInstanceState);
  44.  
  45.         //_tv = new TextView(this);
  46.  
  47.        
  48.  
  49.         setContentView(R.layout.video);
  50.  
  51.         VideoView vv = (VideoView) findViewById(R.id.videoview_main);
  52.  
  53.         MediaController mc=new MediaController(this);
  54.  
  55.         mc.setEnabled(true);
  56.  
  57.         mc.show(0);
  58.  
  59.        
  60.  
  61.                 vv.setMediaController(mc);
  62.  
  63.         try
  64.  
  65.  
  66.  
  67.         {
  68.  
  69.                 String path="/storage/sample.wmv";
  70.  
  71.                
  72.  
  73.                
  74.  
  75.                
  76.  
  77.                 PlayMedia(vv,path);
  78.  
  79.                 vv.setOnCompletionListener(new OnCompletionListener(){
  80.  
  81.  
  82.  
  83.                 // @Override
  84.  
  85.                 public void onCompletion(MediaPlayer arg0) {
  86.  
  87.                      // File has ended !!!
  88.  
  89.                         try
  90.  
  91.                        
  92.  
  93.                         {
  94.  
  95.                                 Log.e("debug","MediaPlayer seek to 0...");
  96.  
  97.                                 arg0.seekTo(0);
  98.  
  99.                                 Log.e("debug","MediaPlayer start...");
  100.  
  101.                                 arg0.start();
  102.  
  103.                                 Log.e("debug","MediaPlayer started");
  104.  
  105.                         }
  106.  
  107.                         catch(Exception ex)
  108.  
  109.                         {
  110.  
  111.                                 Log.e("debug","MediaPlayer error: "+ex.toString());
  112.  
  113.                         }
  114.  
  115.                 }
  116.  
  117.            });
  118.  
  119.         }
  120.  
  121.         catch(Exception ex)
  122.  
  123.         {
  124.  
  125.                
  126.  
  127.                 _tv.append("Error:"+_moduleName+" ("+ex.toString()+")\r\n");
  128.  
  129.                 Log.e("debug",ex.toString());
  130.  
  131.         }
  132.  
  133.     }
  134.  
  135.  
  136.  
  137.     public void PlayMedia(VideoView vv,String path) throws Exception
  138.  
  139.         {
  140.  
  141.                 try
  142.  
  143.                 {
  144.  
  145.                         Log.e("debug","set video path...");
  146.  
  147.                         vv.setVideoPath(path);
  148.  
  149.                        
  150.  
  151.                         Log.e("debug","set request focus...");
  152.  
  153.                         vv.requestFocus();
  154.  
  155.                         Log.e("debug","show context menu...");
  156.  
  157.                         vv.showContextMenu();
  158.  
  159.                         Log.e("debug","start...");
  160.  
  161.                         vv.start();
  162.  
  163.                        
  164.  
  165.                 }
  166.  
  167.                 catch(Exception ex)
  168.  
  169.                 {
  170.  
  171.                         throw ex;
  172.  
  173.                 }
  174.  
  175.         }
  176.  
  177.    
  178.  
  179.    
  180.  
  181.    
  182.  
  183.      
  184.  
  185. }
  186.  
  187.  
Parsed in 0.047 seconds, using GeSHi 1.0.8.4


The layout file "video.xml" like this:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  4.  
  5.    android:orientation="vertical"
  6.  
  7.    android:layout_width="fill_parent"
  8.  
  9.    android:layout_height="fill_parent"
  10.  
  11.    >
  12.  
  13. <VideoView   android:id="@+id/videoview_main"
  14.  
  15.    android:layout_width="fill_parent"
  16.  
  17.    android:layout_height="wrap_content"
  18.  
  19.    android:text="@string/hello"
  20.  
  21.    />
  22.  
  23. </LinearLayout>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4



I hope I could help you.
Regards,
Perrot
perrot
Junior Developer
Junior Developer
 
Posts: 15
Joined: Fri Jan 22, 2010 3:21 am
Location: China

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 12 guests