2014-06-09 83 views
0

我試圖讓視頻在視頻完成後繼續進行下一個活動。我已經看到其他文章試圖做同樣的事情,當我試圖實現它們時,我繼續在我的其他代碼中發現錯誤。在嘗試添加最後一部分之前,我的視頻播放良好。有人看到我的代碼有任何問題嗎?先謝謝你。爲什麼我在嘗試將setOnCompletionListener與videoview一起使用時出現錯誤?

import android.app.Activity; 
import android.content.Intent; 
import android.media.MediaPlayer; 
import android.media.MediaPlayer.OnPreparedListener; 
import android.net.Uri; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ProgressBar; 
import android.widget.VideoView; 


public class ThirdActivity extends Activity { 

    ProgressBar mProgressBar; 
    VideoView mVideoView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.fragment_third); 

     String fileName = "android.resource://" + getPackageName() + "/" + R.raw.leftwrist; 

     mProgressBar = (ProgressBar) findViewById(R.id.Progressbar); 
     mProgressBar.setProgress(0); 
     mProgressBar.setMax(100); 

     mVideoView = (VideoView) findViewById(R.id.video_view); 
     mVideoView.setVideoURI(Uri.parse(fileName)); 
     new myAsync().execute(); 
    } 

    private class myAsync extends AsyncTask<Void, Integer, Void> 
    { 
     int duration = 0; 
     int current = 0; 
     @Override 
     protected Void doInBackground(Void... params) { 

      mVideoView.start(); 
      mVideoView.setOnPreparedListener(new OnPreparedListener() { 

       public void onPrepared(MediaPlayer mp) { 
        duration = mVideoView.getDuration(); 
       } 
      }); 

      do { 
       current = mVideoView.getCurrentPosition(); 
       System.out.println("duration - " + duration + " current- " 
         + current); 
       try { 
        publishProgress((int) (current * 100/duration)); 
        if(mProgressBar.getProgress() >= 100){ 
         break; 
        } 
       } catch (Exception e) { 
       } 
      } while (mProgressBar.getProgress() <= 100); 

      return null; 
     } 

     @Override 
     protected void onProgressUpdate(Integer... values) { 
      super.onProgressUpdate(values); 
      System.out.println(values[0]); 
      mProgressBar.setProgress(values[0]); 
     } 

     mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ 
      @Override 
      public void onCompletion(MediaPlayer mp){ 
       Intent intent = new Intent(this, FourthActivity.class); 
       startActivity(intent); 
      } 
     }); 

} 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.third, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

    /** 
    * A placeholder fragment containing a simple view. 
    */ 
    public static class PlaceholderFragment extends Fragment { 

     public PlaceholderFragment() { 
     } 

     @Override 
     public View onCreateView(LayoutInflater inflater, ViewGroup container, 
       Bundle savedInstanceState) { 
      View rootView = inflater.inflate(R.layout.fragment_third, container, false); 
      return rootView; 
     } 
    } 

} 

回答

0

變化

Intent intent = new Intent(this, FourthActivity.class); 

Intent intent = new Intent(ThirdActivity.this, FourthActivity.class); 

你的情況thisOnCompletionListener不活動。

移動繼起的AsyncTask到onCreate

mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ 
      @Override 
      public void onCompletion(MediaPlayer mp){ 
       Intent intent = new Intent(this, FourthActivity.class); 
       startActivity(intent); 
      } 
     }); 

爲前:

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment_third); 

    String fileName = "android.resource://" + getPackageName() + "/" + R.raw.leftwrist; 

    mProgressBar = (ProgressBar) findViewById(R.id.Progressbar); 
    mProgressBar.setProgress(0); 
    mProgressBar.setMax(100); 

    mVideoView = (VideoView) findViewById(R.id.video_view); 
    mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener(){ 
      @Override 
      public void onCompletion(MediaPlayer mp){ 
       Intent intent = new Intent(this, FourthActivity.class); 
       startActivity(intent); 
      } 
     }); 
    mVideoView.setVideoURI(Uri.parse(fileName)); 
    new myAsync().execute(); 
} 
+0

我仍然得到同樣的錯誤,很遺憾。 – dy19

+0

嘗試編輯後,它工作。非常感謝你!! – dy19

相關問題