Porting that old Media Example

Put your problem here if it does not fit any of the other categories.

Porting that old Media Example

Postby Questioneer » Mon Feb 18, 2008 1:38 am

Hello-

I recently downloaded the old Android media Example from:
http://davanum.wordpress.com/2007/12/29 ... mote-urls/

I've tried to run it in the new SDK with no luck.
Here are some of the errors:

Code: Select all
        holder.setCallback(this);
The Method SetCallback(VideoPlayer) is undefined for the type SurfaceHolder.


Code: Select all
mp.prepare();
Unhandled IOException


Code: Select all
   public boolean surfaceCreated(SurfaceHolder surfaceholder) {
The return type is incompatible with SurfaceHolder.Callback.surfacedCreated(SurfaceHolder)


Other then those 3 errors, it seems to be ok.(Unfortunately, they terminate the build).

I assume they are SDK compatibility issues. If someone could fix them up that would be great.
thanks,
Questioneer
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Top

Postby Questioneer » Mon Feb 18, 2008 1:38 pm

Here's all the code I used:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package org.apache.android;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.graphics.PixelFormat;
  9.  
  10. import android.media.MediaPlayer;
  11.  
  12. import android.media.MediaPlayer.OnBufferingUpdateListener;
  13.  
  14. import android.media.MediaPlayer.OnCompletionListener;
  15.  
  16. import android.media.MediaPlayer.OnErrorListener;
  17.  
  18. import android.os.Bundle;
  19.  
  20. import android.util.Log;
  21.  
  22. import android.view.SurfaceHolder;
  23.  
  24. import android.view.SurfaceView;
  25.  
  26. import android.view.View;
  27.  
  28. import android.webkit.URLUtil;
  29.  
  30. import android.widget.EditText;
  31.  
  32. import android.widget.ImageButton;
  33.  
  34.  
  35.  
  36. import java.io.File;
  37.  
  38. import java.io.FileOutputStream;
  39.  
  40. import java.io.IOException;
  41.  
  42. import java.io.InputStream;
  43.  
  44. import java.net.URL;
  45.  
  46. import java.net.URLConnection;
  47.  
  48.  
  49.  
  50. public class VideoPlayer extends Activity implements OnErrorListener,
  51.  
  52.         OnBufferingUpdateListener, OnCompletionListener,
  53.  
  54.         MediaPlayer.OnPreparedListener, SurfaceHolder.Callback {
  55.  
  56.     private static final String TAG = "VideoPlayer";
  57.  
  58.  
  59.  
  60.     private MediaPlayer mp;
  61.  
  62.     private SurfaceView mPreview;
  63.  
  64.     private EditText mPath;
  65.  
  66.     private SurfaceHolder holder;
  67.  
  68.     private ImageButton mPlay;
  69.  
  70.     private ImageButton mPause;
  71.  
  72.     private ImageButton mReset;
  73.  
  74.     private ImageButton mStop;
  75.  
  76.     private String current;
  77.  
  78.  
  79.  
  80.     /**
  81.  
  82.      * Called when the activity is first created.
  83.  
  84.      */
  85.  
  86.     public void onCreate(Bundle icicle) {
  87.  
  88.         super.onCreate(icicle);
  89.  
  90.  
  91.  
  92.         setContentView(R.layout.main);
  93.  
  94.  
  95.  
  96.         // Set up the play/pause/reset/stop buttons
  97.  
  98.         mPreview = (SurfaceView) findViewById(R.id.surface);
  99.  
  100.         mPath = (EditText) findViewById(R.id.path);
  101.  
  102.         mPlay = (ImageButton) findViewById(R.id.play);
  103.  
  104.         mPause = (ImageButton) findViewById(R.id.pause);
  105.  
  106.         mReset = (ImageButton) findViewById(R.id.reset);
  107.  
  108.         mStop = (ImageButton) findViewById(R.id.stop);
  109.  
  110.  
  111.  
  112.         mPlay.setOnClickListener(new View.OnClickListener() {
  113.  
  114.             public void onClick(View view) {
  115.  
  116.                 playVideo();
  117.  
  118.             }
  119.  
  120.         });
  121.  
  122.         mPause.setOnClickListener(new View.OnClickListener() {
  123.  
  124.             public void onClick(View view) {
  125.  
  126.                 if (mp != null) {
  127.  
  128.                     mp.pause();
  129.  
  130.                 }
  131.  
  132.             }
  133.  
  134.         });
  135.  
  136.         mReset.setOnClickListener(new View.OnClickListener() {
  137.  
  138.             public void onClick(View view) {
  139.  
  140.                 if (mp != null) {
  141.  
  142.                     mp.seekTo(0);
  143.  
  144.                 }
  145.  
  146.             }
  147.  
  148.         });
  149.  
  150.         mStop.setOnClickListener(new View.OnClickListener() {
  151.  
  152.             public void onClick(View view) {
  153.  
  154.                 if (mp != null) {
  155.  
  156.                     mp.stop();
  157.  
  158.                     mp.release();
  159.  
  160.                 }
  161.  
  162.             }
  163.  
  164.         });
  165.  
  166.  
  167.  
  168.         // Set the transparency
  169.  
  170.         getWindow().setFormat(PixelFormat.TRANSPARENT);
  171.  
  172.  
  173.  
  174.         // Set a size for the video screen
  175.  
  176.         holder = mPreview.getHolder();
  177.  
  178.         holder.setCallback(this);
  179.  
  180.         holder.setFixedSize(400, 300);
  181.  
  182.     }
  183.  
  184.  
  185.  
  186.     private void playVideo() {
  187.  
  188.         try {
  189.  
  190.             final String path = mPath.getText().toString();
  191.  
  192.             Log.v(TAG, "path: " + path);
  193.  
  194.  
  195.  
  196.             // If the path has not changed, just start the media player
  197.  
  198.             if (path.equals(current) && mp != null) {
  199.  
  200.                 mp.start();
  201.  
  202.                 return;
  203.  
  204.             }
  205.  
  206.             current = path;
  207.  
  208.  
  209.  
  210.             // Create a new media player and set the listeners
  211.  
  212.             mp = new MediaPlayer();
  213.  
  214.             mp.setOnErrorListener(this);
  215.  
  216.             mp.setOnBufferingUpdateListener(this);
  217.  
  218.             mp.setOnCompletionListener(this);
  219.  
  220.             mp.setOnPreparedListener(this);
  221.  
  222.             mp.setAudioStreamType(2);
  223.  
  224.  
  225.  
  226.             // Set the surface for the video output
  227.  
  228.             mp.setDisplay(mPreview.getHolder().getSurface());
  229.  
  230.  
  231.  
  232.             // Set the data source in another thread
  233.  
  234.             // which actually downloads the mp3 or videos
  235.  
  236.             // to a temporary location
  237.  
  238.             Runnable r = new Runnable() {
  239.  
  240.                 public void run() {
  241.  
  242.                     try {
  243.  
  244.                         setDataSource(path);
  245.  
  246.                     } catch (IOException e) {
  247.  
  248.                         Log.e(TAG, e.getMessage(), e);
  249.  
  250.                     }
  251.  
  252.                     mp.prepare();
  253.  
  254.                     Log.v(TAG, "Duration:  ===>" + mp.getDuration());
  255.  
  256.                     mp.start();
  257.  
  258.                 }
  259.  
  260.             };
  261.  
  262.             new Thread(r).start();
  263.  
  264.         } catch (Exception e) {
  265.  
  266.             Log.e(TAG, "error: " + e.getMessage(), e);
  267.  
  268.             if (mp != null) {
  269.  
  270.                 mp.stop();
  271.  
  272.                 mp.release();
  273.  
  274.             }
  275.  
  276.         }
  277.  
  278.     }
  279.  
  280.  
  281.  
  282.     /**
  283.  
  284.      * If the user has specified a local url, then we download the
  285.  
  286.      * url stream to a temporary location and then call the setDataSource
  287.  
  288.      * for that local file
  289.  
  290.      *
  291.  
  292.      * @param path
  293.  
  294.      * @throws IOException
  295.  
  296.      */
  297.  
  298.     private void setDataSource(String path) throws IOException {
  299.  
  300.         if (!URLUtil.isNetworkUrl(path)) {
  301.  
  302.             mp.setDataSource(path);
  303.  
  304.         } else {
  305.  
  306.             URL url = new URL(path);
  307.  
  308.             URLConnection cn = url.openConnection();
  309.  
  310.             cn.connect();
  311.  
  312.             InputStream stream = cn.getInputStream();
  313.  
  314.             if (stream == null)
  315.  
  316.                 throw new RuntimeException("stream is null");
  317.  
  318.             File temp = File.createTempFile("mediaplayertmp", "dat");
  319.  
  320.             String tempPath = temp.getAbsolutePath();
  321.  
  322.             FileOutputStream out = new FileOutputStream(temp);
  323.  
  324.             byte buf[] = new byte[128];
  325.  
  326.             do {
  327.  
  328.                 int numread = stream.read(buf);
  329.  
  330.                 if (numread <= 0)
  331.  
  332.                     break;
  333.  
  334.                 out.write(buf, 0, numread);
  335.  
  336.             } while (true);
  337.  
  338.             mp.setDataSource(tempPath);
  339.  
  340.             try {
  341.  
  342.                 stream.close();
  343.  
  344.             }
  345.  
  346.             catch (IOException ex) {
  347.  
  348.                 Log.e(TAG, "error: " + ex.getMessage(), ex);
  349.  
  350.             }
  351.  
  352.         }
  353.  
  354.     }
  355.  
  356.  
  357.  
  358.     public void onError(MediaPlayer mediaPlayer, int what, int extra) {
  359.  
  360.         Log.e(TAG, "onError--->   what:" + what + "    extra:" + extra);
  361.  
  362.         if (mediaPlayer != null) {
  363.  
  364.             mediaPlayer.stop();
  365.  
  366.             mediaPlayer.release();
  367.  
  368.         }
  369.  
  370.     }
  371.  
  372.  
  373.  
  374.     public void onBufferingUpdate(MediaPlayer arg0, int percent) {
  375.  
  376.         Log.d(TAG, "onBufferingUpdate called --->   percent:" + percent);
  377.  
  378.     }
  379.  
  380.  
  381.  
  382.     public void onCompletion(MediaPlayer arg0) {
  383.  
  384.         Log.d(TAG, "onCompletion called");
  385.  
  386.     }
  387.  
  388.  
  389.  
  390.     public void onPrepared(MediaPlayer mediaplayer) {
  391.  
  392.         Log.d(TAG, "onPrepared called");
  393.  
  394.     }
  395.  
  396.  
  397.  
  398.     public boolean surfaceCreated(SurfaceHolder surfaceholder) {
  399.  
  400.         Log.d(TAG, "surfaceCreated called");
  401.  
  402.         return true;
  403.  
  404.     }
  405.  
  406.  
  407.  
  408.     public void surfaceChanged(SurfaceHolder surfaceholder, int i, int j, int k) {
  409.  
  410.         Log.d(TAG, "surfaceChanged called");
  411.  
  412.     }
  413.  
  414.  
  415.  
  416.     public void surfaceDestroyed(SurfaceHolder surfaceholder) {
  417.  
  418.         Log.d(TAG, "surfaceDestroyed called");
  419.  
  420.     }
  421.  
  422. }
  423.  
  424.  
Parsed in 0.058 seconds, using GeSHi 1.0.8.4
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Postby Questioneer » Mon Feb 18, 2008 10:40 pm

Will several programs have to be ported to the new SDK?
How deep does this go?
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Postby Questioneer » Tue Feb 19, 2008 3:17 am

OK.
When I try and create a project using the original source code from the zip from the website, I get:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. No activity name defined in /home/alex/Android/VideoPlayer/AndroidManifest.xml.
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


This is weird....
I was able to fix most of the bugs, but now this is weird....
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Postby Questioneer » Tue Feb 19, 2008 3:27 am

Actually, if someone could just provide the minimum code to create a simple Media Player with no buttons, options, etc. that would be far more useful.
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Postby plusminus » Tue Feb 19, 2008 5:45 pm

Questioneer wrote:OK.
When I try and create a project using the original source code from the zip from the website, I get:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. No activity name defined in /home/alex/Android/VideoPlayer/AndroidManifest.xml.
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


This is weird....
I was able to fix most of the bugs, but now this is weird....


Hello questioneer,

upgrading from m3 to m5 you have to change some thing (mostly simplifications) in the AndroidManifest.xml.
:arrow: viewtopic.php?t=879

Also have a look at this:
MediaPlayer.prepare() is now called from MediaPlayer.create() and you will get an exception if you call MediaPlayer.prepare() after using create().


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Postby Questioneer » Sat Feb 23, 2008 3:48 pm

hello-
I changed the manifest to the below:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3.  
  4.    package="org.apache.android">
  5.  
  6.     <application>
  7.  
  8.         <activity android:name=".VideoPlayer" android:label="VideoPlayer">
  9.  
  10.             <intent-filter>
  11.  
  12.                 <action android:name="android.intent.action.MAIN" />
  13.  
  14.                 <category android:name="android.intent.category.LAUNCHER" />
  15.  
  16.             </intent-filter>
  17.  
  18.         </activity>
  19.  
  20.     </application>
  21.  
  22. </manifest>
  23.  
  24.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


And I changed the actual VideoPlayer.java in the following places:

--changed holder.createCallback to holder.addCallback
--Commented out mp.create()
--changed public boolean surfaceCreated(...) to a public void.

Now, the code has no errors according to eclipse.

However, when Android loads, I get the following error message.
I'm not quite sure what is wrong.
Attachments
Screenshot2.png
Screenshot2.png (48.51 KiB) Viewed 2011 times
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Postby Questioneer » Sun Feb 24, 2008 2:07 pm

I have all the other neccesary files in the correct places.(I think... I copied directly from the .ZIP)
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Postby plusminus » Sun Feb 24, 2008 10:19 pm

Hello Questioneer,

did you debug your app :?:
Or is the Exception thrown before even the onCreate() is touched :?:

Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Postby Questioneer » Mon Feb 25, 2008 9:56 pm

hmmm... if someone could provide me with the code for an extremely basic media player(just video, nothing else) that would be great.
Questioneer
Junior Developer
Junior Developer
 
Posts: 12
Joined: Thu Feb 14, 2008 11:23 am

Postby iurp » Tue Apr 01, 2008 9:15 am

Questioneer wrote:hello-
I changed the manifest to the below:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2.    package="org.apache.android">
  3.     <application>
  4.         <activity android:name=".VideoPlayer" android:label="VideoPlayer">
  5.             <intent-filter>
  6.                 <action android:name="android.intent.action.MAIN" />
  7.                 <category android:name="android.intent.category.LAUNCHER" />
  8.             </intent-filter>
  9.         </activity>
  10.     </application>
  11. </manifest>
  12.  
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


And I changed the actual VideoPlayer.java in the following places:

--changed holder.createCallback to holder.addCallback
--Commented out mp.create()
--changed public boolean surfaceCreated(...) to a public void.

Now, the code has no errors according to eclipse.

However, when Android loads, I get the following error message.
I'm not quite sure what is wrong.


I encountered the same problem. Solved by loading the video file from the android file system instead of raw directory.
iurp
Freshman
Freshman
 
Posts: 6
Joined: Tue Apr 01, 2008 8:42 am

Postby iurp » Tue Apr 01, 2008 9:26 am

My current problem on this thread is that the video can only be played for a few seconds and then the application shuts down.
iurp
Freshman
Freshman
 
Posts: 6
Joined: Tue Apr 01, 2008 8:42 am

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 12 guests