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();
    }


            }



        }

它在 Galaxy s3 上的 4.1.2 android 上运行良好,但它给了我标题中的错误消息。它没有显示前 3 秒的视频...请给我一些建议或解决方案,因为我不知道如何摆脱这种错误

4

1 回答 1

0

简单来说,这个错误意味着你要求你的 Android 系统在这个特定应用程序的主线程上做太多的工作。关于这个错误有一些很好的一般答案,一个很好的例子是:

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

对于您的具体示例,您可能只是要求它在主线程上做太多与视频相关的工作,这对处理器来说非常消耗。例如,值得查看您的“准备”方法,如果您使用的是流式源,请考虑使用 prepareAsynch - 请参阅下面的 Android 文档:

公共无效准备异步()

在 API 级别 1 中添加 异步准备播放器进行播放。设置数据源和显示表面后,您需要调用 prepare() 或 prepareAsync()。对于流,您应该调用prepareAsync(),它会立即返回,而不是阻塞直到缓冲足够的数据。

您可能会在 Nexus 7 而不是 Galaxy 3 上看到问题的一个原因是 Nexus 的屏幕明显更大,并且您的视频源可能为不同尺寸的设备提供不同的视频编码 - 较大的设备很可能需要更多处理才能解码和渲染。

于 2014-04-14T16:36:23.080 回答