2010-12-05 130 views
0

全部 Android上的真正初學者。我正在編寫一個應用程序,該應用程序顯示全屏jpg,其中包含一個讀取的文件,其中的觸摸區域以20 x 20像素的粒度定義。這些觸摸區域被解碼爲視頻播放。 jpg和觸摸區域解碼工作得很好,直到我將play_video()註釋掉,那麼模擬器只顯示一個空白屏幕並掛起。可能的視頻編號是0到7,其中8用於指示無效觸摸。添加代碼後,視頻無法啓動並且不再顯示JPEG

我拉我的頭髮剩下的這個。

這裏是Java代碼

package org.example.video_4; 

import java.io.File; 
import java.io.FileInputStream; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.media.MediaPlayer; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.widget.ImageView; 

public class video_4 extends Activity { 
    byte[] videos_to_play = new byte[960]; 
    boolean video_playing = false; 
    int video_to_play = 8; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     // now display the adBook background screen this is a full screen 
     // due to the declaration in manifest.xml under activity 
     // android:theme="@android:style/Theme.NoTitleBar.Fullscreen" 
     setContentView(R.layout.main); 
     ImageView jpgView = (ImageView) findViewById(R.id.jpgview); 
     String myJpgPath = "/sdcard/AdBook.JPG"; //UPDATE WITH YOUR OWN JPG FILE 
     BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inSampleSize = 1; 
     Bitmap bm = BitmapFactory.decodeFile(myJpgPath, options); 
     jpgView.setImageBitmap(bm); 
     // now we have to wait for a touch and then fire of the video play 
     // we also return here when the video is complete so as we wait for 
     // touch demand for a video to play 
     set_touch_areas(); 

     /******************************************************* 
     Problem area all works fine until I uncomment this code 
     ********************************************************/ 

     /* play_video();*/ 
    } 


    @Override 
    public boolean onTouchEvent(MotionEvent event) { 

     int xpos, ypos; 

     if (event.getAction() != MotionEvent.ACTION_DOWN) { 
      return super.onTouchEvent(event); 
     } 
     xpos = (int) event.getX()/20; 
     ypos = (int) event.getY()/20; 
     if (xpos < 0 || xpos > 39 || ypos < 0 || ypos > 23) { 
      video_to_play = 8; 
      return true; 
     } 
     video_to_play = videos_to_play[(ypos * 40) + xpos]; 
     return true; 
    } 

    public void set_touch_areas() { 
     // read in the touch file 
     File file = new File("/sdcard/adbook.tch"); 
     FileInputStream is; 
     int c, v, x; 

     try { 
      is = new FileInputStream(file); 

      // Get the size of the file 
      long length = file.length(); 
      if (length != 960) { 
       finish(); 
      } 
      // Read in the bytes 
      int offset = 0; 
      int numRead = 0; 
      while (offset < videos_to_play.length && (numRead = is.read(videos_to_play, offset, videos_to_play.length - offset)) >= 0) { 
       offset += numRead; 
      } 
      // Ensure all the bytes have been read in 
      if (offset < videos_to_play.length) { 
       finish(); 
      } 
     } catch (FileNotFoundException e1) { 
      // just bomb if anything is bad with the file read 
      finish(); 
     } catch (IOException e) { 
      // just bomb if anything is bad with the file read 
      finish(); 
     } 
     // we have read the adbook.tch file so now decode the entries based 
     // on my algorithm of invert, valid & video_num/random data 
     for (c = 0; c < 960; c++) { 
      v = (videos_to_play[c] & 0xc0) >> 6; // mask and align the bits for checking 
      if (v == 0x02) { // inverted and video valid 
       x = videos_to_play[c]; // java can only invert integer 
       x = ~x; // so invert an integer, what a pain!! 
       videos_to_play[c] = (byte)(x & 0x07); // mask to the valid number 
      } else if (v == 0x01) { // non inverted and video valid 
       videos_to_play[c] &= (byte) 0x07; // so just isolate the video number 
      } else { // non valid entry who cares, I do 
       videos_to_play[c] = (byte) 0x08; // it's not valid so store the fact 
      } 
     } 
    } 

    public void play_video() { 
     String myMp4Path = "/sdcard/v1.mp4"; 

     video_to_play = 8; 
     while (true) { 
      if ((video_to_play < 8) && (video_playing == false)) { 
       switch (video_to_play) { 
       case 0: 
        myMp4Path = "/sdcard/v0.mp4"; 
        break; 
       case 1: 
        myMp4Path = "/sdcard/v1.mp4"; 
        break; 
       case 2: 
        myMp4Path = "/sdcard/v2.mp4"; 
        break; 
       case 3: 
        myMp4Path = "/sdcard/v3.mp4"; 
        break; 
       case 4: 
        myMp4Path = "/sdcard/v4.mp4"; 
        break; 
       case 5: 
        myMp4Path = "/sdcard/v5.mp4"; 
        break; 
       case 6: 
        myMp4Path = "/sdcard/v6.mp4"; 
        break; 
       case 7: 
        myMp4Path = "/sdcard/v7.mp4"; 
        break; 
       default: 
        myMp4Path = "/sdcard/v4.mp4"; 
        break; 
       } 
       video_playing = true; 
       MediaPlayer mp = new MediaPlayer(); 
       try { 
        mp.setDataSource(myMp4Path); 
       } catch (IllegalArgumentException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       try { 
        mp.prepare(); 
       } catch (IllegalStateException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 
       mp.start(); 
       while (mp.isPlaying()); 
       video_playing = false; 
       video_to_play = 8; 
      } 
     } 
    } 
} 
+0

以供參考:http://stackoverflow.com/editing-help – 2010-12-05 14:54:29

+0

據我所知,你不能在模擬器上播放視頻。你需要有一個設備來測試這個。 – Varun 2010-12-05 15:03:54

+0

當我嘗試使用已知良好的代碼在模擬器上播放視頻時,我沒有得到任何圖片,但音頻確實播放(最終)。 – 2010-12-05 19:14:18

回答

1

看來,你的play_video()將不會返回,直到視頻

while (mp.isPlaying());

結束但你從的onCreate稱之爲()。 onCreate在UI線程上運行,因此必須很快返回,否則應用程序將無響應 - 您所請求的某些內容甚至可能在onCreate()返回後纔會開始發生。你需要開始視頻然後返回。設置其他事件以在其完成時激活,或者具有監視它的單獨線程。

1

如果我沒看錯的,

video_to_play = 8; 
    while (true) { 
     if ((video_to_play < 8) && (video_playing == false)) { 

video_to_play始終是8,所以它永遠不會播放視頻以外/sdcard/v1.mp4

如果設置了此變量,用於測試你的應用程序,你應該已經刪除swith() - 發佈您的問題中的聲明。