sharing a surfaceview between 2 mediaplayer

All your problems with Audio, Video and Images.

sharing a surfaceview between 2 mediaplayer

Postby sbw.android » Mon Jun 22, 2009 5:24 pm

hi,
I want to play 2 videos one after one minimizing delay between each video.
so I created 2 mediaplayer, one playing the current video and the other preparing to play the following video.

the 1st video is played correctely but I couldn't get the video track of the second video. only sound without any error. can somebody help?
thanks.

this is my source code :
(note that here, I am using files on sdcard but the application will use files on a web server. that's why I need to prepare the second video while the 1st is playing)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.test.testvideo;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.media.MediaPlayer;
  11.  
  12. import android.view.SurfaceView;
  13.  
  14. import android.util.Log;
  15.  
  16. import android.view.SurfaceHolder;
  17.  
  18. import android.media.AudioManager;
  19.  
  20. import android.widget.Button;
  21.  
  22.  
  23.  
  24. public class Test extends Activity implements
  25.  
  26.         MediaPlayer.OnPreparedListener, SurfaceHolder.Callback,
  27.  
  28.         MediaPlayer.OnCompletionListener {
  29.  
  30.         SurfaceView sf = null;
  31.  
  32.         int currplayer = 0;
  33.  
  34.         MediaPlayer mp1 = null;
  35.  
  36.         MediaPlayer mp0 = null;
  37.  
  38.         int currvideo = 0;
  39.  
  40.         SurfaceHolder holder = null;
  41.  
  42.  
  43.  
  44.         @Override
  45.  
  46.         public void onCreate(Bundle savedInstanceState) {
  47.  
  48.                 super.onCreate(savedInstanceState);
  49.  
  50.                 setContentView(R.layout.main);
  51.  
  52.                 this.sf = (SurfaceView)findViewById(R.id.sf);
  53.  
  54.                 this.holder = sf.getHolder();
  55.  
  56.                 holder.addCallback(this);
  57.  
  58.                 holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  59.  
  60.         }
  61.  
  62.  
  63.  
  64.         public void onPrepared(MediaPlayer mp){
  65.  
  66.         Log.v("onPrepared()", "prepared...");
  67.  
  68.  
  69.  
  70.                 if(currplayer == 0) {
  71.  
  72.                         currvideo++;
  73.  
  74.                         String nexturl = "/sdcard/"+currvideo+".3gp";
  75.  
  76.                         try {
  77.  
  78.                                 mp1.setDataSource(nexturl);
  79.  
  80.                         }
  81.  
  82.                         catch(Exception e){
  83.  
  84.                         }
  85.  
  86.                         mp1.setDisplay(holder);
  87.  
  88.                         mp1.prepareAsync();
  89.  
  90.                         mp1.setOnPreparedListener(this);
  91.  
  92.                         mp1.setOnCompletionListener(this);
  93.  
  94.                         mp1.setAudioStreamType(AudioManager.STREAM_MUSIC);
  95.  
  96.                         currplayer=1;
  97.  
  98.                 }
  99.  
  100.         }
  101.  
  102.  
  103.  
  104.         public void onCompletion(MediaPlayer mp) {
  105.  
  106.                 Log.v("onCompletion()"+currplayer, "completed...");
  107.  
  108.                 if(currplayer == 1) {
  109.  
  110.                         mp0.stop();
  111.  
  112.                         mp0.release();
  113.  
  114.                         mp1.start();
  115.  
  116.                         currplayer = 0;
  117.  
  118.                 }
  119.  
  120.         }
  121.  
  122.  
  123.  
  124.         public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
  125.  
  126.                 Log.d("surfaceChanged()", "surfaceChanged...");
  127.  
  128.         }
  129.  
  130.  
  131.  
  132.         public void surfaceDestroyed(SurfaceHolder surfaceholder) {
  133.  
  134.                 Log.d("surfaceDestroyed()", "surfaceDestroyed...");
  135.  
  136.         }
  137.  
  138.  
  139.  
  140.         public void surfaceCreated(SurfaceHolder holder) {
  141.  
  142.                 Log.d("surfaceCreated()", "surfaceCreated...");
  143.  
  144.                 mp0 = new MediaPlayer();
  145.  
  146.                 mp1 = new MediaPlayer();
  147.  
  148.                 try {
  149.  
  150.                         mp0.setDataSource("/sdcard/0.3gp");
  151.  
  152.                         mp0.setDisplay(holder);
  153.  
  154.                         mp0.prepare();
  155.  
  156.                 }
  157.  
  158.                 catch(Exception e){
  159.  
  160.                 }
  161.  
  162.                 mp0.setOnPreparedListener(this);
  163.  
  164.                 mp0.setOnCompletionListener(this);
  165.  
  166.                 mp0.start();
  167.  
  168.  
  169.  
  170.                 mp0.setAudioStreamType(AudioManager.STREAM_MUSIC);
  171.  
  172.         }
  173.  
  174.  
  175.  
  176.         @Override
  177.  
  178.         protected void onDestroy() {
  179.  
  180.                 super.onDestroy();
  181.  
  182.                 if (mp0 != null) {
  183.  
  184.                         mp0.release();
  185.  
  186.                         mp0 = null;
  187.  
  188.                 }
  189.  
  190.                 if (mp1 != null) {
  191.  
  192.                         mp1.release();
  193.  
  194.                         mp1 = null;
  195.  
  196.                 }
  197.  
  198.         }
  199.  
  200. }
  201.  
  202.  
Parsed in 0.041 seconds, using GeSHi 1.0.8.4
sbw.android
Freshman
Freshman
 
Posts: 4
Joined: Mon Jun 22, 2009 5:17 pm

Top

Postby yuvalkesten » Mon Jun 29, 2009 8:51 pm

This is the way I am doing it:
I created two surface views, one takes most of the screen and the other takes only a really small size (So small the user won't notice it). I create the first media player and play it on the large surfaceview, I create the second on the small view, prepare it, when it is done preparing and I want to switch to it I simply change the size of the view such that the small view will become the large and vice versa.
Hope it helped...
yuvalkesten
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jun 29, 2009 8:44 pm
Location: Tel Aviv

Postby sbw.android » Tue Jun 30, 2009 11:38 am

thanks.
that's something I didn't expect to work since google devs recommends not using 2 surfaceview
http://groups.google.com/group/android- ... 24a2e48bb3

I will test it.
sbw.android
Freshman
Freshman
 
Posts: 4
Joined: Mon Jun 22, 2009 5:17 pm

Emulator VS G2

Postby yuvalkesten » Tue Jun 30, 2009 11:44 am

Hi,
What I told didn't work that well on the emulator - green spots appeared on the screen and stuff. When I moved to device debugging on my Google ION (HTC Magic - G2) it worked just great!
I guess that the difference probably relies on the fact that the devices have an hardware video decoder and the emulator obviously don't...
yuvalkesten
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jun 29, 2009 8:44 pm
Location: Tel Aviv

Postby yuvalkesten » Tue Jun 30, 2009 11:46 am

BTW - I managed, using two separate surface views, to run two videos simultaneously... But it was kind of "jumpy"...
yuvalkesten
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jun 29, 2009 8:44 pm
Location: Tel Aviv

Postby sbw.android » Tue Jun 30, 2009 5:14 pm

I tested your solution. it looks much better than using on mediaplayer on one surfaceview but I still have a little video interruption (tested on a G1) .
I will do some other tests later. depending on where I start/stop the videos (on video completion / on surface change / ...), I get different behaviours. I may have some tweaking possibilities.
thanks.
sbw.android
Freshman
Freshman
 
Posts: 4
Joined: Mon Jun 22, 2009 5:17 pm

Top

Postby yuvalkesten » Thu Jul 09, 2009 4:13 pm

Just tested displaying two videos (streaming) one next to each other, on the G2 it is working great...
yuvalkesten
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jun 29, 2009 8:44 pm
Location: Tel Aviv

Postby eidolon » Thu Dec 31, 2009 6:52 am

yuvalkesten wrote:This is the way I am doing it:
I created two surface views, one takes most of the screen and the other takes only a really small size (So small the user won't notice it). I create the first media player and play it on the large surfaceview, I create the second on the small view, prepare it, when it is done preparing and I want to switch to it I simply change the size of the view such that the small view will become the large and vice versa.
Hope it helped...




Hi,I'm beginner for android.Can u tell me how to do that??
eidolon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Dec 31, 2009 6:18 am

Postby yuvalkesten » Thu Dec 31, 2009 8:45 am

I will post my code during the weekend but I am sure that you can learn how to create surface views with different sizes using the android dev tutorials...
Try MiKiZaZa... An interactive content player and editor. Transform youtube movies or your family movies to an android game with just a couple of clicks...
Search for MiKiZaZa @ market or go to http://mikizaza.wordpress.com
yuvalkesten
Junior Developer
Junior Developer
 
Posts: 12
Joined: Mon Jun 29, 2009 8:44 pm
Location: Tel Aviv

Postby eidolon » Thu Dec 31, 2009 8:51 am

yuvalkesten wrote:I will post my code during the weekend but I am sure that you can learn how to create surface views with different sizes using the android dev tutorials...



Hi yuvalkesten,thanks very much.Could you sent your code to me??My email is eidolonStudio@gmail.com
eidolon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Dec 31, 2009 6:18 am

Postby eidolon » Tue Jan 05, 2010 4:12 am

yuvalkesten wrote:I will post my code during the weekend but I am sure that you can learn how to create surface views with different sizes using the android dev tutorials...



take it up
eidolon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Dec 31, 2009 6:18 am

Postby eidolon » Fri Apr 16, 2010 7:54 am

take it up.
eidolon
Junior Developer
Junior Developer
 
Posts: 11
Joined: Thu Dec 31, 2009 6:18 am

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 11 guests