2011-07-05 106 views
2

如何在Android模擬器中使用遠程URL播放mp4視頻?我使用了下面的代碼,但是這段代碼給了我錯誤「對不起,這個視頻無法播放」。如何在Android模擬器中使用遠程URL播放mp4視頻?

 
07-05 16:58:19.525: INFO/AwesomePlayer(34): mConnectingDataSource->connect() returned -1007 
07-05 16:58:19.525: ERROR/MediaPlayer(1242): error (1, -1007) 
07-05 16:58:19.525: ERROR/MediaPlayer(1242): Error (1,-1007) 
07-05 16:58:19.525: DEBUG/VideoView(1242): Error: 1,-1007 

我的代碼是: -

 
public class VideoPlayerController extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
       super.onCreate(savedInstanceState); 
       setContentView(R.layout.main);   
       VideoView videoView = (VideoView) findViewById(R.id.VideoView); 
       MediaController mediaController = new MediaController(this); 
       mediaController.setAnchorView(videoView); 
       String Video="http://s509.photobucket.com/albums/s338/eveanthony/?action=view&current=Video013.mp4"; 
       videoView.setMediaController(mediaController); 
       videoView.setVideoURI(Uri.parse(Video)); 
       videoView.start(); 
      } 
} 
+0

模擬器並沒有爲我播放MP4視頻無論是。我使用設備進行測試,並且運行良好。通過我粘貼視頻網址在我的瀏覽器和視頻鏈接似乎被破壞。 – Varun

+0

Mp4視頻工作在模擬器但不是從遠程URL只從本地文件。 –

+0

**你是否在設備上試過**。對我而言,只有3gp在模擬器上播放,並且在設備上我也可以播放mp4。正如我所提到的,這個問題中的視頻網址似乎被打破了。 – Varun

回答

8

你需要原來的設備,而不是仿真器上執行的應用程序,因爲它不支持播放的視頻文件。在極少數情況下,它可能會取決於您的系統配置。

2

Android 4.1.2版似乎在youtube應用程序中的模擬器中播放mp4視頻,而不是其他地方。我測試了它。英特爾和非英特爾版本均可使用。 4.0.3沒有播放它們。

0
private VideoView myVideoView; 
private int position = 0; 
private ProgressDialog progressDialog; 
private MediaController mediaControls; 

@Override 
protected void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    private static final String Videos_URL = "*Your_URI*"; 

    // Get the layout from video_main.xml 
    setContentView(R.layout.activity_main); 

    // Find your VideoView in your video_main.xml layout 
    myVideoView = (VideoView) findViewById(R.id.videoView); 

    // Create a progressbar 
    progressDialog = new ProgressDialog(this); 
    // Set progressbar title 
    progressDialog.setTitle("Anything u Want"); 
    // Set progressbar message 
    progressDialog.setMessage("Loading..."); 

    progressDialog.setCancelable(false); 
    // Show progressbar 
    progressDialog.show(); 

    try { 
     Uri video = Uri.parse(Videos_URL); 
     myVideoView.setVideoURI(video); 
     myVideoView.setMediaController(mediaControls); 
    } catch (Exception e) { 
     Log.e("Error", e.getMessage()); 
     e.printStackTrace(); 
    } 

    myVideoView.requestFocus(); 
    myVideoView.setOnPreparedListener(new OnPreparedListener() { 
     // Close the progress bar and play the video 
     public void onPrepared(MediaPlayer mp) { 
      progressDialog.dismiss(); 
      myVideoView.seekTo(position); 
      if (position == 0) { 
       myVideoView.start(); 
      } else { 
       myVideoView.pause(); 
      } 
     } 
    }); 

} 

@Override 
public void onSaveInstanceState(Bundle savedInstanceState) { 
    super.onSaveInstanceState(savedInstanceState); 
    savedInstanceState.putInt("Position", myVideoView.getCurrentPosition()); 
    myVideoView.pause(); 
} 

@Override 
public void onRestoreInstanceState(Bundle savedInstanceState) { 
    super.onRestoreInstanceState(savedInstanceState); 
    position = savedInstanceState.getInt("Position"); 
    myVideoView.seekTo(position); 
} 

}