2013-08-07 34 views
5
import java.io.BufferedReader; 

public class Main extends Activity implements SurfaceHolder.Callback, 
     MediaPlayer.OnCompletionListener, View.OnClickListener, OnInitListener { 

    String SrcPath = ""; 
    MediaPlayer mp; 
    SurfaceView mSurfaceView; 
    private SurfaceHolder holderrrr; 
    Boolean play = false; 
    String t_alarm1 = "alarm.xml", t_alarm2 = "alarm2.xml", text; 

    // TextToSpeach 
    private TextToSpeech mText2Speech; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 

     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
       .permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     addListenerOnButton(); 
     mText2Speech = new TextToSpeech(Main.this, Main.this); 

    } 

    // menü 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 

     case R.id.action_settings: { 
      Intent myIntent = new Intent(this, menu.class); 
      Main.this.startActivity(myIntent); 
      return true; 
     } 

     } 

     return true; 
    } 

    // kilépésfigyelő 

    private static final long DOUBLE_PRESS_INTERVAL = 2000000000;// 2 másodperc 
    private long lastPressTime; 

    @Override 
    public void onBackPressed() { 
     Toast.makeText(Main.this, getString(R.string.kilepes_dupla), 
       Toast.LENGTH_SHORT).show(); 
     long pressTime = System.nanoTime(); 
     if (pressTime - lastPressTime <= DOUBLE_PRESS_INTERVAL) { 
      // this is a double click event 
      System.exit(0); 

     } 
     lastPressTime = pressTime; 

    } 

    public void onInit(int status) { 

     if (status == TextToSpeech.SUCCESS) { 
      mText2Speech.setLanguage(Locale.getDefault()); // alaértelmezett 
                  // nyelv a TtS-hez 
     } 

    } 

    private void addListenerOnButton() { 
     final Button button5 = (Button) findViewById(R.id.button5); 


        button5.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       // Perform action on click 
       if (play) { 
        try{ 
         mp.stop(); 
         mp.release(); 
         mp = null; 
        }catch(Exception e){ 

        } 

        button5.setText("Start"); 
        play = false; 

       } else { 
        try { 
         mp = new MediaPlayer(); 
         mSurfaceView = (SurfaceView) findViewById(R.id.surface); 
         holderrrr = mSurfaceView.getHolder(); 
         play = true; 
         button5.setText("Stop"); 
         surfaceCreated(holderrrr); 
        } catch (IllegalArgumentException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (SecurityException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } catch (IllegalStateException e) { 
         // TODO Auto-generated catch block 
         e.printStackTrace(); 
        } 
       } 

      } 
     }); 

    }// addlistener vége 


    public void surfaceCreated(SurfaceHolder holder) { 

     mp.setDisplay(holder); 

     holder = mSurfaceView.getHolder(); 
     holder.addCallback(this); 
     holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
     try { 
      mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() { 
       @Override 
       public void onCompletion(MediaPlayer mp) { 

        mp.stop(); 
        mp.release(); 
        Toast.makeText(Main.this, 
          "A videó lejátszás befejeződött!", 
          Toast.LENGTH_SHORT).show(); 
        // button5.setText("Start"); 
        //play = false; 


       } 
      }); 
      mp.setDataSource(SrcPath); 
      mp.prepare(); 

     } catch (IllegalArgumentException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } catch (SecurityException 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(); 
     } 

     // Get the dimensions of the video 
     // int videoWidth = mp.getVideoWidth(); 
     // int videoHeight = mp.getVideoHeight(); 

     // Get the width of the screen 
     // int screenWidth = getWindowManager().getDefaultDisplay().getWidth(); 

     // Get the SurfaceView layout parameters 
     android.view.ViewGroup.LayoutParams lp = mSurfaceView.getLayoutParams(); 

     // Set the width of the SurfaceView to the width of the screen 
     // lp.width = screenWidth; 
     lp.width = 420; 

     // Set the height of the SurfaceView to match the aspect ratio of the 
     // video 
     // be sure to cast these as floats otherwise the calculation will likely 
     // be 0 
     // lp.height = (int) (((float) videoHeight/(float) videoWidth) * 
     // (float) screenWidth); 
     lp.height = 390; 

     // Commit the layout parameters 
     mSurfaceView.setLayoutParams(lp); 

     // Start video 
     mp.start(); 
    } 


      } 



     } 

hoạt động tốt trên android 4.1.2 trên galaxy s3 nhưng nó cho tôi thông báo lỗi có trong tiêu đề. và nó không hiển thị video đầu tiên 3 giây ... xin vui lòng cho tôi một số lời khuyên hoặc giải pháp som với điều này vì tôi không có ý tưởng làm thế nào để thoát khỏi loại lỗi nàyĐã bỏ qua các khung hình. Ứng dụng có thể đang hoạt động quá nhiều trên lỗi chính của nó ”trên android 4.3 nexus 7

+0

Bạn có thấy tại sao điều đó xảy ra không? Tôi cũng phải đối mặt với một vấn đề tương tự chỉ trên vài thiết bị –

Trả lời

0

Trong điều khoản đơn giản, lỗi này có nghĩa là bạn đang yêu cầu hệ thống Android của bạn thực hiện quá nhiều công việc trên luồng chính của ứng dụng cụ thể này. Có một số câu trả lời chung tốt về lỗi này, một trong những tốt dụ phúc:

https://stackoverflow.com/a/21126690/334402

Ví dụ cụ thể của bạn, bạn có thể chỉ đơn giản là yêu cầu nó phải làm quá nhiều video liên quan làm việc, mà là rất xử đói, trên chủ đề chính. Nó sẽ là giá trị xem xét phương pháp 'chuẩn bị' của bạn ví dụ và nếu bạn đang sử dụng một nguồn xem trực tiếp, xem xét sử dụng prepareAsynch - xem dưới đây từ các tài liệu Android:

public void prepareAsync()

gia tăng trong Cấp API 1 Chuẩn bị trình phát để phát lại, không đồng bộ. Sau khi thiết lập nguồn dữ liệu và bề mặt hiển thị, bạn cần phải hoặc gọi chuẩn bị() hoặc PrepareAsync(). Đối với luồng, bạn nên gọi prepareAsync(), trả về ngay lập tức, thay vì chặn cho đến khi đủ dữ liệu đã được lưu vào bộ đệm.

Một lý do khiến bạn gặp sự cố trên Nexus 7 chứ không phải trên Galaxy 3 là Nexus có màn hình lớn hơn đáng kể và nguồn video của bạn có thể cung cấp mã hóa video khác nhau cho các thiết bị có kích thước khác nhau rất có thể yêu cầu xử lý nhiều hơn để giải mã và hiển thị.